Module: StatusFor::StatusInstanceMethods

Defined in:
lib/status_for/acts_as_statusable_for.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *subject_ids, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/status_for/acts_as_statusable_for.rb', line 98

def method_missing(method_id, *subject_ids, &block)
  # This creates a method for instanced object called 'mark_as_status_for(subject)' that 
  # adds the subject id into relevant database column.
  # Example:  @message.mark_as_deleted_for(user)
  # Returns a message with the deleted_for containing the subject id
  if method_id.to_s =~ /^mark_as_([a-z]+)_for\!$/
    run_mark_as_status_for!($1, subject_ids)
  # This creates a method for instanced object called 'mark_as_not_ status_for(subject)' that 
  # removes the subject id into relevant database column.
  # Example:  @message.mark_as_not_deleted_for(user)
  # Returns a message with the subject_id in the deleted_for column removed.
  elsif method_id.to_s =~ /^mark_as_not_([a-z]+)_for\!$/
    run_mark_as_not_status_for!($1, subject_ids)
  # This creates a method for instanced object called 'check_status_for(subject)' that 
  # checks if the subject id is contained in the object.
  # Example:  @message.check_deleted_for(user)
  # Returns true or false
  elsif method_id.to_s =~ /^check_([a-z]+)_for\?$/
    run_check_status_for($1, subject_ids)
  else
    super
  end 
end

Instance Method Details

#respond_to?(method_id, include_private = false) ⇒ Boolean

Ensuring the method created for the instanced class exists

Returns:

  • (Boolean)


123
124
125
126
127
128
129
# File 'lib/status_for/acts_as_statusable_for.rb', line 123

def respond_to?(method_id, include_private = false)
  if method_id.to_s =~ /^mark_as_([a-z]+)_for\!$/ || method_id.to_s =~ /^mark_as_not_([a-z]+)_for\!$/ || method_id.to_s =~ /^check_([a-z]+)_for\?$/
    true
  else
    super
  end
end

#run_check_status_for(method_id, subject_id) ⇒ Object

The action performed when calling @object.check_status_for(subject). Uses a postgres-extension query



163
164
165
166
167
168
169
170
171
# File 'lib/status_for/acts_as_statusable_for.rb', line 163

def run_check_status_for(method_id, subject_id)
  if !self.class.column_names.include?(method_id + '_for')
    raise "need to include the #{method_id}_for column in #{self.class.table_name} table"
  end
  unless subject_id.first.is_a? Integer
    raise "subject_id must be an Integer!"
  end
  return status_for_psql_array_to_array(self.send ((method_id +'_for').to_sym)).include?(subject_id.first)
end

#run_mark_as_not_status_for!(method_id, subject_ids) ⇒ Object

The action performed when calling @object.mark_as_not_status_for(subject). Uses a postgres-extension query



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/status_for/acts_as_statusable_for.rb', line 148

def run_mark_as_not_status_for!(method_id, subject_ids)
  if !self.class.column_names.include?(method_id + '_for')
    raise "need to include the #{method_id}_for column in #{self.class.table_name} table"
  end
  if subject_ids.is_a? Integer
    subject_ids = [subject_ids]
  end
  status_for_subjects = status_for_psql_array_to_array(self.send ((method_id +'_for').to_sym))
  status_for_subjects = (status_for_subjects - subject_ids).flatten.uniq
  self.update_attribute(method_id + '_for', status_for_array_to_psql_array(status_for_subjects))
  self
end

#run_mark_as_status_for!(method_id, subject_ids) ⇒ Object

The action performed when calling @object.mark_as_status_for(subject). Uses a postgres-extension query



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/status_for/acts_as_statusable_for.rb', line 133

def run_mark_as_status_for!(method_id, subject_ids)
  if !self.class.column_names.include?(method_id + '_for')
    raise "need to include the #{method_id}_for column in #{self.class.table_name} table"
  end
  if subject_ids.is_a? Integer
    subject_ids = [subject_ids]
  end
  status_for_subjects = status_for_psql_array_to_array(self.send ((method_id +'_for').to_sym))
  status_for_subjects = (status_for_subjects + subject_ids).flatten.uniq
  self.update_attribute(method_id + '_for', status_for_array_to_psql_array(status_for_subjects))
  self
end