Created
August 25, 2016 00:06
-
-
Save cseeger/2d6cfe0a847d9c82449f3093221ed9e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module NestedMassAssignment | |
extend ActiveSupport::Concern | |
included do | |
include ActiveAttr::BasicModel | |
include ActiveAttr::BlockInitialization | |
include ActiveAttr::Logger | |
# include ActiveAttr::MassAssignment | |
include ActiveAttr::AttributeDefaults | |
include ActiveAttr::QueryAttributes | |
include ActiveAttr::TypecastedAttributes | |
include ActiveAttr::Serialization | |
def initialize(attributes=nil, options={}) | |
assign_attributes attributes, options | |
super | |
end | |
def assign_attributes(new_attributes, options={}) | |
sanitized_new_attributes = sanitize_for_mass_assignment_if_sanitizer new_attributes, options | |
sanitized_new_attributes.each do |name, value| | |
if self.class.attributes[name][:class] | |
value = self.class.attributes[name][:class].constantize.new(value) | |
end | |
writer = "#{name}=" | |
send writer, value if respond_to? writer | |
end if sanitized_new_attributes | |
end | |
end | |
end | |
class Whatever | |
include NestedMassAssignment | |
attribute :member, class: "Whatever::Member" | |
attribute :code | |
class Member | |
include NestedMassAssignment | |
attribute :some_id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment