diff --git a/.rubocop.yml b/.rubocop.yml index 384ca58..5b1b4b4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -12,6 +12,9 @@ Style/SingleLineMethods: Style/StringLiterals: EnforcedStyle: single_quotes +Rails/Delegate: + Enabled: false + AllCops: NewCops: enable TargetRubyVersion: 2.7 diff --git a/README.md b/README.md index 6a035e3..e50ea8b 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,24 @@ gem 'active_dry_form' ### Under the hood ActiveDryForm uses [dry-validation](https://dry-rb.org/gems/dry-validation), [dry-monads](https://dry-rb.org/gems/dry-monads) +#### Initialize form ```ruby -form = ProductForm.new(record: Product.find(1), params: { product: { title: 'n', price: 120 } }) +ProductForm.new(params: { title: 'n', price: '120' }) +ProductForm.new(params: { product: { title: 'n', price: '120' } }) +ProductForm.new(record: Product.find(params[:id]), params:) +``` + +#### Attribute accessors +```ruby +form.title # => 'n' +form.price # => '120' +form.price = '119' # => '119' +form[:price] # => '119' +form[:price] = '121' # => '121' +``` +#### Methods +```ruby form.validate # => checks field validity form.validator # => #"n", :price=>120, errors={:title=>["minimum length 2"]}...> form.valid? # => false @@ -21,13 +36,14 @@ form.errors # => {:title=>["minimum length 2"]} form.base_errors = [] form.errors_full_messages # => ['Cannot be less than 2 words'] form.record # => # +# form.data - is hash, casted to types, after validate form.data # => {:title=>"n", :price=>120} form.data[:price] # => 120 -form.price # => '120' -form.title # => 'n' form.update # Failure(:invalid_form) ``` + + Methods `form.update` and `form.create` return [Result monad](https://dry-rb.org/gems/dry-monads/1.3/result/) ```ruby diff --git a/lib/active_dry_form/base_form.rb b/lib/active_dry_form/base_form.rb index 34c3640..ba9054e 100644 --- a/lib/active_dry_form/base_form.rb +++ b/lib/active_dry_form/base_form.rb @@ -140,6 +140,14 @@ def self.wrap(object) form end + def [](k) + attributes[k] + end + + def []=(k, v) + attributes[k] = _deep_transform_values_in_params!(v) + end + def self.define_methods const_set :NESTED_FORM_KEYS, [] @@ -148,10 +156,6 @@ def self.define_methods attributes[key] = _deep_transform_values_in_params!(v) end - define_method :'[]=' do |k, v| - attributes[k] = _deep_transform_values_in_params!(v) - end - sub_klass = if value[:properties] || value.dig(:items, :properties) Class.new(BaseForm).tap do |klass| diff --git a/spec/active_dry_form_spec.rb b/spec/active_dry_form_spec.rb index 6da26b0..c05b2f6 100644 --- a/spec/active_dry_form_spec.rb +++ b/spec/active_dry_form_spec.rb @@ -29,6 +29,13 @@ expect(form.name).to eq 'Vasya' end + it 'attribute accessors' do + form = UserForm.new(params: { name: 'Vasya' }) + expect(form[:name]).to eq 'Vasya' + expect { form[:second_name] = 'Petrov' }.to change(form, :second_name).to('Petrov') + expect(form[:second_name]).to eq 'Petrov' + end + it 'set hash with unknown key' do described_class.config.strict_param_keys = false