Module: ActiveRecordDateFormatted::Model

Defined in:
lib/active_record_date_formatted/model.rb

Instance Method Summary collapse

Instance Method Details

#add_date_formatted_methodsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_record_date_formatted/model.rb', line 11

def add_date_formatted_methods
  self.column_types.each do |attr_name, c|
    if c.type == :date
      define_method "#{attr_name}_formatted" do
        date_value = read_attribute(attr_name)
        date_value.nil? ? nil : date_value.strftime(I18n.t "date.formats.default")
      end

      define_method "#{attr_name}_formatted=" do |date_formatted|
        write_attribute(attr_name, date_formatted.blank? ? nil : Date.strptime(date_formatted, I18n.t("date.formats.default")))
      end
    end
  end
end

#inherited(subclass) ⇒ Object



6
7
8
9
# File 'lib/active_record_date_formatted/model.rb', line 6

def inherited(subclass)
  super
  subclass.add_date_formatted_methods unless subclass == ActiveRecord::SchemaMigration || subclass.to_s.ends_with?('Temp') # todo nasty bugfix for temporary migration classes with custom table names
end