Module: Idioma::Concerns::Models::DateFlag::ClassMethods

Defined in:
lib/concerns/models/date_flag.rb

Overview

Usage:

class MyModel < ActiveRecord::Base date_flag :flagged_at, :action => :flag end

m = MyModel.new m.flagged? # => false m.flag! # Assigns flag_at to current time m.flag = true # Same as flag! m.flagged? # => true

Instance Method Summary collapse

Instance Method Details

#date_flag(field, options = { }) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/concerns/models/date_flag.rb', line 19

def date_flag(field, options = { })
  action = (options[:action] ? options[:action] : field.to_s.sub(/_at$/, '')).to_sym
  query = (options[:query] and options[:query].to_sym or action)

  scope_name =
    case (options[:scope])
    when nil
      action
    when false
      false
    else
      options[:scope].to_s.to_sym
    end

  case (scope_name)
  when false, nil
    # Skip this operation
  when :send, :id
    # TODO: Invalid names, should raise exception or warning
  else
    scope scope_name, lambda { |*flag|
      case (flag.first)
      when false
        where("#{table_name}.#{field} IS NULL")
      when true, nil
        where("#{table_name}.#{field} IS NOT NULL")
      else
        where("#{table_name}.#{field} <=?", flag.first)
      end
    }
  end

  define_method(:"#{action}=") do |value|
    # The action= mutator method is used to manipulate the trigger time.
    # Values of nil, false, empty string, '0' or 0 are presumed to be
    # false and will nil out the time. A DateTime, Date or Time object
    # will be saved as-is, and anything else will just assign the current
    # time.

    case (value)
    when nil, false, '', '0', 0
      write_attribute(field, nil)
    when DateTime, Date, Time
      write_attribute(field, value)
    else
      !read_attribute(field) and write_attribute(field, Time.zone.now.utc)
    end
  end

  define_method(:"is_#{action}?") do
    # The action? accessor method will return true if the date is defined
    # and is prior to the current time, or false otherwise.

    value = read_attribute(field)

    # value ? (value <= Time.zone.now.utc) : false
    value.present?
  end
  alias_method action, :"is_#{action}?"

  define_method(:"#{action}!") do |*at_time|
    # The action! method is used to set the trigger time. If the time is
    # already defined and is in the past, then the time is left unchanged.
    # If it is undefined or in the future, then the current time is
    # substituted.

    value = read_attribute(field)

    at_time = at_time.first || Time.zone.now.utc

    unless (value and (value <= at_time))
      write_attribute(field, at_time)
      save!
    end
  end
end