3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
|
# File 'lib/review_and_approve/model_additions.rb', line 3
def review_and_approve(*args)
field = :publish
methods = [:as_json]
key_proc = Proc.new{|obj, method| "ReviewAndApprove_#{obj.class.name}_#{obj.id}_#{method}"}
args.each do |arg|
if arg.is_a? Hash
if !arg[:by].nil?
methods = arg[:by]
end
if !arg[:field].nil?
field = arg[:field]
end
if !arg[:cache_key].nil?
key_proc = arg[:cache_key]
end
end
end
attr_accessor field
send(:define_singleton_method, :_using_rev_app?) do
true
end
after_save :review_and_approve_after_save_callback
send(:define_method, :review_and_approve_after_save_callback) do |publish = false|
published = self.send(field) || publish
if published and (published==true or published=="true" or published=="on" or self.send(field).to_i>0 rescue false) methods.each do |method|
cr = CacheRecord.find_or_initialize_by_key("#{key_proc.call(self, method)}_published_version")
cr.cache_data = self.send(method)
cr.save
end
end
methods.each do |method|
cr = CacheRecord.find_or_initialize_by_key("#{key_proc.call(self, method)}_current_version")
cr.cache_data = self.send(method)
cr.save
end
true
end
send(:define_method, :published_version) do |method_name|
CacheRecord.find_by_key("#{key_proc.call(self, method_name)}_published_version").cache_data rescue nil
end
send(:define_method, :current_version) do |method_name|
CacheRecord.find_by_key("#{key_proc.call(self, method_name)}_current_version").cache_data rescue nil
end
send(:define_method, :mass_assignment_authorizer) do |role = :default|
super(role) + [field]
end
send(:define_method, :published?) do
CacheRecord.where("key = '#{key_proc.call(self, methods.first)}_published_version'").count>0
end
validates_each field do |record, attr, value|
able = Thread.current[:reviewAndApprove_current_ability].try(:can?, :publish, record)
if !able and value and (value==true or value=="true" or value=="on" or value.to_i>0 rescue false)
record.errors[attr] << "can not be marked as true by this user"
end
end
end
|