acts_as_statused 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/acts_as_statused.rb +57 -50
- metadata +2 -2
data/lib/acts_as_statused.rb
CHANGED
@@ -8,74 +8,81 @@ module ActiveRecord #:nodoc:
|
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def acts_as_statused(options = {}, &extension)
|
11
|
-
|
12
|
-
|
11
|
+
begin
|
12
|
+
# don't allow multiple calls
|
13
|
+
return if self.included_modules.include?(ActiveRecord::Acts::Statused::ActMethods)
|
13
14
|
|
14
|
-
|
15
|
+
send :include, ActiveRecord::Acts::Statused::ActMethods
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
valid_status = []
|
18
|
+
valid_statuses = options[:only] if options.is_a?(Hash)
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
class_name = options.to_s if options.is_a?(Symbol) || options.is_a?(String)
|
21
|
+
class_name = options[:class_name] if options.is_a?(Hash)
|
22
|
+
class_name = "Status" if class_name.blank?
|
22
23
|
|
23
|
-
|
24
|
+
#puts "!!!!!!!!!!!class_name = #{class_name}"
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
class_eval do
|
27
|
+
belongs_to :status
|
27
28
|
|
28
|
-
|
29
|
+
attr_accessor :previous_status
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def after_find
|
32
|
+
self.previous_status = self.status if respond_to?:status_id
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
eval %{
|
36
|
+
def status
|
37
|
+
#{class_name}.get(self.status_id)
|
38
|
+
end
|
39
|
+
}
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
def has_status_changed?
|
42
|
+
return self.previous_status != self.status
|
43
|
+
end
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
begin
|
46
|
+
rows = eval "#{class_name}.find(:all)"
|
47
|
+
rows.each do |status| #get all the statuses in the db.
|
48
|
+
# only define valid statuses
|
49
|
+
# EX: acts_as_statused :only => ['active', 'inactive', 'pending']
|
50
|
+
next unless valid_statuses.blank? || valid_statuses.include?(status.name)
|
48
51
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
+
define_method("status_is_#{status.name}?") do #define the method status_is_statusname?
|
53
|
+
return self.status_id == status.id
|
54
|
+
end
|
55
|
+
end
|
56
|
+
rescue
|
52
57
|
end
|
53
|
-
rescue
|
54
|
-
end
|
55
58
|
|
56
|
-
end
|
57
|
-
|
58
|
-
%w{save update create destroy}.each do |verb|
|
59
|
-
define_method("before_#{verb}_status_changed") do
|
60
|
-
#puts "before_#{verb}_status_changed"
|
61
59
|
end
|
60
|
+
|
61
|
+
%w{save update create destroy}.each do |verb|
|
62
|
+
define_method("before_#{verb}_status_changed") do
|
63
|
+
#puts "before_#{verb}_status_changed"
|
64
|
+
end
|
62
65
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
+
define_method("after_#{verb}_status_changed") do
|
67
|
+
#puts "after_#{verb}_status_changed"
|
68
|
+
end
|
66
69
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
+
define_method("do_before_#{verb}_status_changed") do
|
71
|
+
eval %{before_#{verb}_status_changed if self.has_status_changed?}
|
72
|
+
end
|
70
73
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
+
define_method("do_after_#{verb}_status_changed") do
|
75
|
+
eval %{after_#{verb}_status_changed if self.has_status_changed?}
|
76
|
+
end
|
74
77
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
78
|
+
eval %{
|
79
|
+
before_#{verb} :do_before_#{verb}_status_changed
|
80
|
+
after_#{verb} :do_after_#{verb}_status_changed
|
81
|
+
}
|
82
|
+
end
|
83
|
+
rescue => ex
|
84
|
+
puts "Error in acts_as_statused: #{ex.message}"
|
85
|
+
puts ex.backtrace
|
79
86
|
end
|
80
87
|
end
|
81
88
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: acts_as_statused
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-02-
|
6
|
+
version: 1.0.2
|
7
|
+
date: 2007-02-28 00:00:00 -05:00
|
8
8
|
summary: Acts as Statused
|
9
9
|
require_paths:
|
10
10
|
- lib
|