Permalink
Browse files

Merge pull request #13 from caironoleto/master

Adding support to use custom parsers.
  • Loading branch information...
carlosantoniodasilva committed May 21, 2012
2 parents 259277a + d99837c commit 2d478150d258750269bf1b14af7ccb45268548a3
View
@@ -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
@@ -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
@@ -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
View
@@ -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
@@ -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"
View
@@ -3,6 +3,7 @@ en:
date:
formats:
default: "%m/%d/%Y"
+ custom: "%m/%Y"
time:
formats:
View
@@ -3,6 +3,7 @@ pt:
date:
formats:
default: "%d/%m/%Y"
+ custom: "%m/%Y"
time:
formats:
View
@@ -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
View
@@ -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

0 comments on commit 2d47815

Please sign in to comment.