Skip to content

Commit

Permalink
Adding support to use custom parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cairo Noleto committed May 19, 2012
1 parent 259277a commit d99837c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/i18n_alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ def localized(attributes=nil, *args)
class_attribute :localized_methods,
:instance_reader => false, :instance_writer => false
self.localized_methods = {}

class_attribute :customized_parsers,
:instance_reader => false, :instance_writer => false
self.customized_parsers = {}
end

module ClassMethods
def localize_methods(methods_hash)
self.localized_methods = self.localized_methods.merge(methods_hash)
end

def custom_parsers(parsers_hash)
self.customized_parsers = self.customized_parsers.merge(parsers_hash)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/i18n_alchemy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def build_attributes
@target.class.columns.each do |column|
next if column.primary || column.name.ends_with?("_id")

parser = detect_parser_from_column(column)
parser = @target.class.customized_parsers[column.name.to_sym] || detect_parser_from_column(column)
build_attribute(column.name, parser)
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/custom_parsers/my_custom_date_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module MyCustomDateParser
include I18n::Alchemy::DateParser
extend self

def localize(value)
I18n.localize value, :format => :custom
end

protected

def i18n_format
I18n.t(:custom, :scope => [:date, :formats])
end
end
1 change: 1 addition & 0 deletions test/db/test_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
t.decimal :price
t.integer :quantity
t.date :released_at
t.date :released_month
t.datetime :updated_at
t.timestamp :last_sale_at
t.references :supplier
Expand Down
15 changes: 15 additions & 0 deletions test/i18n_alchemy/proxy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ def test_localizes_date_attribute_before_type_cast_output
assert_equal "28/02/2011", @localized.released_at_before_type_cast
end

def test_parses_date_attribute_input_with_custom_parser
@localized.released_month = "02/2011"
assert_equal Date.new(2011, 2, 1), @product.released_month
end

def test_localizes_date_attribute_output_with_custom_parser
@product.released_month = Date.new(2011, 2, 1)
assert_equal "02/2011", @localized.released_month
end

def test_localizes_date_attribute_before_type_cast_output_with_custom_parser
@product.released_month = Date.new(2011, 2, 1)
assert_equal "02/2011", @localized.released_month_before_type_cast
end

# DateTime
def test_parses_datetime_attribute_input
@localized.updated_at = "28/02/2011 13:25:30"
Expand Down
1 change: 1 addition & 0 deletions test/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
date:
formats:
default: "%m/%d/%Y"
custom: "%m/%Y"

time:
formats:
Expand Down
1 change: 1 addition & 0 deletions test/locale/pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pt:
date:
formats:
default: "%d/%m/%Y"
custom: "%m/%Y"

time:
formats:
Expand Down
1 change: 1 addition & 0 deletions test/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Product < ActiveRecord::Base
include I18n::Alchemy
localize_methods :total => :number, :estimated_delivery_at => :date,
:estimated_last_comission_payment_at => :timestamp
custom_parsers :released_month => MyCustomDateParser

attr_protected :my_precious

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
I18n.load_path << Dir[File.expand_path("../locale/*.yml", __FILE__)]

require "db/test_schema"
Dir["test/custom_parsers/*.rb"].each { |file| require File.expand_path(file) }
Dir["test/models/*.rb"].each { |file| require File.expand_path(file) }

module I18n::Alchemy
Expand Down

0 comments on commit d99837c

Please sign in to comment.