Class: NotifyMe::Action

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/notify_me/action.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.process_by_identifier(identifier, *args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'app/models/notify_me/action.rb', line 38

def self.process_by_identifier(identifier, *args)
	action = self.where("response_identifier = ? AND has_been_processed = ?",
			   identifier, false).first

	if action
		{:action => action, :rval => action.run_action(*args)}
	else
		nil
	end
end

Instance Method Details

#run_action(*args) ⇒ Object



57
58
59
60
61
62
63
64
# File 'app/models/notify_me/action.rb', line 57

def run_action(*args)
	rval = run_command(*args)

	self.has_been_processed = true
	self.save!

	return rval
end

#run_command(*args) ⇒ Object



49
50
51
52
53
54
55
# File 'app/models/notify_me/action.rb', line 49

def run_command(*args)
	if self.commandable
		self.commandable.send(self.commandable_action, self, *args)
	else
		self.commandable_type.constantize.send(self.commandable_action, self, *args)
	end
end

#validate_pressence_of_actionObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/notify_me/action.rb', line 22

def validate_pressence_of_action
	if self.commandable
		unless self.commandable.respond_to?(self.commandable_action)
			errors.add(:base, "The commandable action #{self.commandable_action} does not exist on the commandable instance")
		end
	else
		begin
			unless self.commandable_type.constantize.respond_to?(self.commandable_action)
				errors.add(:base, "The commandable action #{self.commandable_action} does not exist on the commandable_type constant")
			end
		rescue NameError
			errors.add(:base, "The commandable_type does not refer to a valid constant")
		end
	end
end

#validate_uniqness_of_unprocessed_identifiersObject



13
14
15
16
17
18
19
20
# File 'app/models/notify_me/action.rb', line 13

def validate_uniqness_of_unprocessed_identifiers
	if self.response_identifier != nil
		if NotifyMe::Action.where("response_identifier = ? AND has_been_processed = ?",
								  self.response_identifier, false).count > 0
			errors.add(:base, "The response_identifier is not unique among unprocessed actions")
		end
	end
end