Comments (8)
Any updates on this?
from active_attr.
The functionality for parsing this in ActiveRecord is hiding in Base, kicked off by assign_multiparameter_attributes.
from active_attr.
This functionality depends on typecasting, or at least knowing the type of the attribute.
from active_attr.
Any news on this? Would be awesome :)
from active_attr.
Has this been implemented yet?
from active_attr.
Are you looking for pull requests?
from active_attr.
FWIW here's an implementation from one of our apps:
# spec/form_objects/form_object_base_spec.rb
require 'rails_helper'
describe FormObjectBase do
class FooForm < FormObjectBase
attribute :name
attribute :cool_date, type: Date
attribute :something_else, type: Date
end
it 'handles multiparameter assignment' do
form = FooForm.new(
'cool_date(1i)' => '1993',
'cool_date(2i)' => '2',
'cool_date(3i)' => '27',
'name' => 'Doofy',
'something_else(1i)' => '2014',
'something_else(2i)' => nil,
'something_else(3i)' => ''
)
expect(form.cool_date).to eq Date.new(1993, 2, 27)
expect(form.name).to eq 'Doofy'
expect(form.something_else).to be_nil
end
end
# app/form_objects/form_object_base.rb
class FormObjectBase
include ActiveAttr::Attributes
include ActiveAttr::MassAssignment
include ActiveAttr::BasicModel
include ActiveAttr::TypecastedAttributes
def assign_attributes(new_attributes, options = {})
super(
expand_multiparameter_attributes(new_attributes),
options
)
end
private
def expand_multiparameter_attributes(attributes)
single_parameter_attributes = {}
multi_parameter_attributes = {}
attributes.each do |key, value|
matches = key.match(/^(?<key>[^\(]+)\((?<index>\d+)i\)$/)
unless matches
single_parameter_attributes[key] = value
next
end
args = (multi_parameter_attributes[matches['key']] ||= [])
args[matches['index'].to_i - 1] = (value.present? ? value.to_i : nil)
end
single_parameter_attributes.merge(
multi_parameter_attributes.inject({}) do |hash, (key, args)|
if args.all?(&:present?)
hash.merge(key => _attribute_type(key).new(*args))
else
hash
end
end
)
end
end
from active_attr.
Whoops, left a bug in there (single parameter attributes would be lost) fixed and updated my comment 😄
from active_attr.
Related Issues (20)
- Mass Assignment is slow HOT 2
- Add SymbolTypecaster HOT 2
- boolean typecaster returning false for empty strings HOT 1
- Project status? HOT 4
- documentation for a new Typecaster HOT 3
- Undefined methods from shoulda-matchers while testing models, which uses active_attr HOT 1
- ActiveModel 5 no longer includes ActiveModel::Serializers::Xml HOT 4
- Issue when using with responders HOT 3
- Need new version of active_attr for Rails 5.2.0.alpha HOT 3
- Typecasting re-typecasts at every method call
- Add support for Rails 5.2 HOT 1
- Memory leak with ActiveAttr::BasicModel and Class.new
- ActiveAttr#inspect should be filtered
- ActiveAttr::Model should have mass assignment security by default
- UUID typecasting HOT 1
- ReDoS vulnerability in ActiveAttr::Typecasting::BooleanTypecaster#call HOT 2
- Is defining "dynamic" attributes possible? HOT 1
- Need help with release management and issue/PR triage? HOT 1
- Frozen model still allows attribute changes
- @frontend on X/Twitter
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from active_attr.