Sha256: b50bfa1714a815a54b7b49dfb6d7147b127ea8b06f4ea76c59e5a4c515d91a67

Contents?: true

Size: 915 Bytes

Versions: 39

Compression:

Stored size: 915 Bytes

Contents

module DateExpander extend ActiveSupport::Concern
  included do
    cattr_accessor :date_expander_field
    expand_date_from :created_at

    before_create do
      x = self.class.date_expander_field
      x = self.send(x)

      if self.class.date_expander_field == :created_at && x.blank?
        x = DateTime.now
      end

      unless x.blank?
        self.hour  = x.change(:min => 0)
        self.day   = x.change(:hour => 0).to_date
        self.month = x.change(:day => 1, :hour => 0).to_date
      end
    end

    before_update do
      x = self.class.date_expander_field
      x = self.send(x)

      unless x.blank?
        self.hour  = x.change(:min => 0)
        self.day   = x.change(:hour => 0).to_date
        self.month = x.change(:day => 1, :hour => 0).to_date
      end
    end
  end

  module ClassMethods
    def expand_date_from(field)
      self.date_expander_field = field
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
smartkiosk-server-0.13.1 lib/date_expander.rb
smartkiosk-server-0.13 lib/date_expander.rb
smartkiosk-server-0.12.1 lib/date_expander.rb
smartkiosk-server-0.12 lib/date_expander.rb
smartkiosk-server-0.11.11 lib/date_expander.rb
smartkiosk-server-0.11.10 lib/date_expander.rb
smartkiosk-server-0.11.9 lib/date_expander.rb
smartkiosk-server-0.11.8 lib/date_expander.rb
smartkiosk-server-0.11.7 lib/date_expander.rb
smartkiosk-server-0.11.6 lib/date_expander.rb
smartkiosk-server-0.11.5 lib/date_expander.rb
smartkiosk-server-0.11.4 lib/date_expander.rb
smartkiosk-server-0.11.3 lib/date_expander.rb
smartkiosk-server-0.11.2 lib/date_expander.rb
smartkiosk-server-0.11.1 lib/date_expander.rb
smartkiosk-server-0.11 lib/date_expander.rb
smartkiosk-server-0.10.19 lib/date_expander.rb
smartkiosk-server-0.10.18 lib/date_expander.rb
smartkiosk-server-0.10.17 lib/date_expander.rb
smartkiosk-server-0.10.16 lib/date_expander.rb