Module: ActsAsStatusFor::ClassMethods

Defined in:
lib/acts_as_status_for.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *rest, &block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/acts_as_status_for.rb', line 46

def method_missing(method,*rest,&block)
  includes = status_including_method(method)
  if not includes.blank?
    statuses_including(includes.split('_and_'))
  else
    super
  end
end

Instance Method Details

#acts_as_status_for(*status_marks, &after_migrations) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/acts_as_status_for.rb', line 11

def acts_as_status_for(*status_marks, &after_migrations)
  @all_status_marks_exist = true
  @on_at_events  = status_marks
  @off_at_events = on_at_events.collect do | event |
    "not_#{event.to_s}".to_sym
  end
  install_scopes
  install_methods
  after_migrations.call(self) if @all_status_marks_exist && block_given?
end

#all_at_eventsObject



122
123
124
# File 'lib/acts_as_status_for.rb', line 122

def all_at_events
  @all_at_events ||= ( off_at_events | on_at_events )
end

#construct_have_state_arel_condition(state) ⇒ Object

having the state means that the state is not nil and the state_at is greater than all the other states not nil



59
60
61
62
63
64
65
66
67
# File 'lib/acts_as_status_for.rb', line 59

def construct_have_state_arel_condition(state)
  has_condition = self.arel_table["#{state}_at".to_sym].not_eq(nil)
  (on_at_events - [state]).each do | unstate |
    has_condition     = has_condition.and(self.arel_table["#{state}_at".to_sym].
                                          gt(self.arel_table["#{unstate}_at".to_sym]).
                                          or(self.arel_table["#{unstate}_at".to_sym].eq(nil)))
  end
  has_condition
end

#construct_not_have_state_arel_condition(state) ⇒ Object

not having the state means that the state is nil or the state_at is less or equal to all the other non nil states



73
74
75
76
77
78
79
80
81
# File 'lib/acts_as_status_for.rb', line 73

def construct_not_have_state_arel_condition(state)
  has_not_condition = self.arel_table["#{state}_at".to_sym].eq(nil)
  (on_at_events - [state]).each do | unstate |
    has_not_condition = has_not_condition.or(self.arel_table["#{state}_at".to_sym].
                                             lteq(self.arel_table["#{unstate}_at".to_sym]).
                                             and(self.arel_table["#{unstate}_at".to_sym].not_eq(nil)))
  end
  has_not_condition
end

#install_methodsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/acts_as_status_for.rb', line 96

def install_methods
  on_at_events.each do |state|
    if self.arel_table["#{state}_at".to_sym] then
      define_method "#{state}?" do
        !read_attribute("#{state}_at".to_sym).nil?
      end

      define_method "not_#{state}!" do
        self.send("#{state}_at=".to_sym,nil)
        self.save
      end

      define_method "#{state}!" do
        self.send("#{state}_at=".to_sym,Time.now)
        self.save
      end
    else
      log_error(state)
      @all_status_marks_exist = @all_status_marks_exist && false
    end
  end
end

#install_scopesObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/acts_as_status_for.rb', line 82

def install_scopes
  on_at_events.each do | state |
    if self.arel_table["#{state}_at".to_sym] then
      has_condition     = construct_have_state_arel_condition(state)
      has_not_condition = construct_not_have_state_arel_condition(state)
      scope "#{state}".to_sym, lambda { where(has_condition) }
      scope "not_#{state}".to_sym, lambda { where(has_not_condition) }
    else
      log_error(state)
      @all_status_marks_exist = @all_status_marks_exist && false
    end
  end
end

#log_error(state) ⇒ Object



22
23
24
# File 'lib/acts_as_status_for.rb', line 22

def log_error(state)
  STDERR.puts "Arel could not find #{state}_at in the database - skipping installation of acts_as_status"
end

#off_at_eventsObject



119
120
121
# File 'lib/acts_as_status_for.rb', line 119

def off_at_events
  @off_at_events
end

#on_at_eventsObject



125
126
127
# File 'lib/acts_as_status_for.rb', line 125

def on_at_events
  @on_at_events
end

#respond_to_missing?(method, include_private) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/acts_as_status_for.rb', line 34

def respond_to_missing?(method,include_private)
  ! status_including_method(method).nil?
end

#status_including_method(method) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/acts_as_status_for.rb', line 26

def status_including_method(method)
  if method =~ /status_including_(.+)/
    $1
  else
    nil
  end
end

#statuses_including(status_list) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/acts_as_status_for.rb', line 38

def statuses_including(status_list)
  has_condition = self
  status_list.each do |state|
    has_condition = has_condition.where(self.arel_table["#{state}_at".to_sym].not_eq(nil))
  end
  has_condition
end