Class: I18n::Alchemy::AssociationParser
- Inherits:
-
Object
- Object
- I18n::Alchemy::AssociationParser
- Defined in:
- lib/i18n_alchemy/association_parser.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#target_class ⇒ Object
readonly
Returns the value of attribute target_class.
Instance Method Summary collapse
-
#association_name_attributes ⇒ Object
Examples.
-
#initialize(target_class, association_name) ⇒ AssociationParser
constructor
A new instance of AssociationParser.
-
#parse(attributes) ⇒ Object
Parse nested attributes for one-to-one and collection association.
Constructor Details
#initialize(target_class, association_name) ⇒ AssociationParser
Returns a new instance of AssociationParser.
6 7 8 9 |
# File 'lib/i18n_alchemy/association_parser.rb', line 6 def initialize(target_class, association_name) @target_class = target_class @association_name = association_name end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
4 5 6 |
# File 'lib/i18n_alchemy/association_parser.rb', line 4 def association_name @association_name end |
#target_class ⇒ Object (readonly)
Returns the value of attribute target_class.
4 5 6 |
# File 'lib/i18n_alchemy/association_parser.rb', line 4 def target_class @target_class end |
Instance Method Details
#association_name_attributes ⇒ Object
Examples
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar
end
AssociationParser.new(Member, :avatar).association_name_attributes # => "avatar_attributes"
38 39 40 |
# File 'lib/i18n_alchemy/association_parser.rb', line 38 def association_name_attributes "#{association_name}_attributes" end |
#parse(attributes) ⇒ Object
Parse nested attributes for one-to-one and collection association
Examples
parse(:avatar_attributes => {:icon => 'sad_panda'})
parse(:posts_attributes => [{:title => 'Foo!'}, {:title => 'Bar!'}])
parse(:posts_attributes => { 0 => {:title => 'Foo!'}, 1 => {:title => 'Bar!'})
parse(:posts_attributes => { "81u21udjsndja" => {:title => 'Foo!'}, "akmsams" => {:title => 'Baz!'}})
20 21 22 23 24 25 26 27 |
# File 'lib/i18n_alchemy/association_parser.rb', line 20 def parse(attributes) if association.macro == :has_many attributes = attributes.is_a?(Hash) ? attributes.values : attributes attributes.map { |value_attributes| proxy.send(:parse_attributes, value_attributes) } else proxy.send(:parse_attributes, attributes) end end |