Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to custom association parser. #20

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions lib/i18n_alchemy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ def method_missing(*args, &block)
private

def active_record_compatible?
target_class = @target.class
target_class.respond_to?(:columns) && target_class.respond_to?(:nested_attributes_options)
end

def build_attributes
@target.class.columns.each do |column|
columns.each do |column|
column_name = column.name
next if column.primary || column_name.ends_with?("_id") || @localized_attributes.key?(column_name)

Expand All @@ -68,16 +67,16 @@ def build_attributes
end

def build_methods
@target.class.localized_methods.each_pair do |method, parser_type|
localized_methods.each_pair do |method, parser_type|
method = method.to_s
parser = detect_parser(parser_type)
build_attribute(method, parser)
end
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 @@ -87,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 @@ -121,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 @@ -129,10 +131,28 @@ def detect_parser(type_or_parser)
DateParser
when :datetime, :timestamp
TimeParser
when :association
AssociationParser
when ::Module
type_or_parser
end
end

def localized_methods
target_class.localized_methods
end

def columns
target_class.columns
end

def nested_attributes_options
target_class.nested_attributes_options
end

def target_class
@target.class
end
end
end
end
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