Skip to content

Commit

Permalink
Adding support to custom association parser.
Browse files Browse the repository at this point in the history
With this, we fix carlosantoniodasilva#18.
  • Loading branch information
Cairo Noleto committed Jun 13, 2012
1 parent bbd8478 commit 6420853
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/i18n_alchemy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def build_methods
end

def build_associations
@target.class.nested_attributes_options.each_key do |association_name|
create_localized_association(association_name)
nested_attributes_options.each_key do |association_name|
create_localized_association(association_name, detect_parser_from_association(association_name))
end
end

Expand All @@ -86,9 +86,8 @@ def build_attribute(name, parser)
define_localized_methods(name)
end

def create_localized_association(association_name)
@localized_associations <<
AssociationParser.new(@target.class, association_name)
def create_localized_association(association_name, parser)
@localized_associations << parser.new(target_class, association_name)
end

def create_localized_attribute(column_name, parser)
Expand Down Expand Up @@ -120,6 +119,10 @@ def detect_parser_from_column(column)
detect_parser(column.number? ? :number : column.type)
end

def detect_parser_from_association(association_name)
detect_parser(localized_methods[association_name] ? localized_methods[association_name] : :association)
end

def detect_parser(type_or_parser)
case type_or_parser
when :number
Expand All @@ -128,6 +131,8 @@ def detect_parser(type_or_parser)
DateParser
when :datetime, :timestamp
TimeParser
when :association
AssociationParser
when ::Module
type_or_parser
end
Expand All @@ -141,6 +146,10 @@ def columns
target_class.columns
end

def nested_attributes_options
target_class.nested_attributes_options
end

def target_class
@target.class
end
Expand Down
13 changes: 13 additions & 0 deletions test/custom_parsers/my_custom_association_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class MyCustomAssociationParser < I18n::Alchemy::AssociationParser
def parse(attributes)
if association.options[:polymorphic]
@proxy = target_class.i18n_alchemy_proxy.localized
end

super
end

def self.localize(object)
object
end
end
10 changes: 10 additions & 0 deletions test/db/test_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@
t.string :account_number
t.decimal :total_money
end

create_table :people do |t|
t.string :name
t.integer :personable_id
t.string :personable_type
end

create_table :companies do |t|
t.string :register
end
end
8 changes: 8 additions & 0 deletions test/i18n_alchemy/proxy/attributes_parsing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def test_assign_attributes
end

def test_mass_assigning_invalid_attribute
@person = Person.new
@person_localized = @person.localized
assert_raises(ActiveRecord::UnknownAttributeError) do
@localized.assign_attributes('i_dont_even_exist' => 40)
end
Expand Down Expand Up @@ -128,6 +130,12 @@ def test_attributes_assignment_for_nested
assert_equal '88,12', @supplier_localized.account.localized.total_money
end

def test_should_assign_for_nested_attributes_for_polymorphic_association
@person_localized.assign_attributes(:personable_type => "Company", :personable_attributes => {:register => 10})
company = @person_localized.personable
assert_equal 10, company.register
end

private

def attributes_hash
Expand Down
3 changes: 3 additions & 0 deletions test/models/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Company < ActiveRecord::Base
include I18n::Alchemy
end
16 changes: 16 additions & 0 deletions test/models/person.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Person < ActiveRecord::Base
include I18n::Alchemy
localize :personable, :using => MyCustomAssociationParser

belongs_to :personable, :polymorphic => true

accepts_nested_attributes_for :personable

def build_personable(attributes, options = {})
self.personable = personable_type.constantize.new(attributes, options)
end

def self.i18n_alchemy_proxy
Company.new
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def setup
@supplier_localized = @supplier.localized
@user = User.new
@user_localized = @user.localized
@person = Person.new
@person_localized = @person.localized

I18n.locale = :pt
end
Expand Down

0 comments on commit 6420853

Please sign in to comment.