13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/active_record_date_formatted/model.rb', line 13
def add_date_formatted_methods
self.column_types.each do |attr_name, c|
if c.type == :date
attr_accessor "#{attr_name}_formatted"
validates "#{attr_name}_formatted", "active_record_date_formatted/date_format" => true
before_save "save_formatted_#{attr_name}"
define_method "#{attr_name}_formatted" do
if instance_variable_get("@#{attr_name}_formatted").nil?
date_value = read_attribute(attr_name)
date_value.nil? ? nil : date_value.strftime(I18n.t "date.formats.default")
else
instance_variable_get("@#{attr_name}_formatted")
end
end
define_method "save_formatted_#{attr_name}" do
write_attribute(attr_name, self.send("#{attr_name}_formatted").blank? ? nil : Date.strptime(self.send("#{attr_name}_formatted"), I18n.t("date.formats.default")))
end
end
end
end
|