Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def create_field(field, field_attributes)
# Multiple values
if field_attributes.is_a?Array or field_attributes.is_a?ActiveRecord::Associations::CollectionProxy
multiple_values = []
field_attributes.each do |atr|
atr = atr.attributes if field_attributes.is_a?ActiveRecord::Associations::CollectionProxy
atr.reject! {|k,v| k.eql?"id" }
atr.merge!({ :user_id => self.address_book.user.id })
multiple_values << field.singularize.camelize.constantize.new(atr)
end
self.contact.send "#{field}=", multiple_values
else
# Single values
field_attributes.reject! {|k,v| k.eql?"id" }
field_attributes.merge!({ :user_id => self.address_book.user.id })
self.contact.send "#{field}=", field.camelize.constantize.new(field_attributes)
end
end
@gil27

This comment has been minimized.

Copy link

@gil27 gil27 commented May 29, 2014

Uma outra abordagem: https://gist.github.com/gil27/ee734fdffbe2a2b61ac5 Assim tentamos respeitar o princípio de single responsability dos métodos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.