created-and-updated-by 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/created-and-updated-by.gemspec +1 -1
- data/lib/created-and-updated-by.rb +7 -21
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
@@ -1,34 +1,20 @@
|
|
1
|
-
module CreatedAndUpdatedBy
|
2
|
-
|
1
|
+
module CreatedAndUpdatedBy
|
3
2
|
class Stamper
|
4
3
|
cattr_accessor :stampable, :attribute
|
5
4
|
def self.attach(stamp_model = User, stamp_attribute = :current)
|
6
5
|
raise ArgumentError, "#{stamp_model.name} does not respond to #{stamp_attribute}" unless stamp_model.respond_to?(stamp_attribute)
|
7
6
|
self.stampable, self.attribute = stamp_model, stamp_attribute
|
8
|
-
ActiveRecord::Base.
|
9
|
-
ActiveRecord::Base.send(:acts_as_stampable)
|
7
|
+
ActiveRecord::Base.send(:include, CreatedAndUpdatedBy)
|
10
8
|
end
|
11
9
|
end
|
12
|
-
|
13
|
-
def acts_as_stampable
|
14
|
-
class_eval do
|
15
|
-
send :include, InstanceMethods
|
16
|
-
before_validation :set_stamps
|
17
|
-
belongs_to :updated_by, :class_name => CreatedAndUpdatedBy::Stamper.stampable.name
|
18
|
-
belongs_to :created_by, :class_name => CreatedAndUpdatedBy::Stamper.stampable.name
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
module InstanceMethods
|
10
|
+
class_eval do
|
23
11
|
def stamper
|
24
12
|
CreatedAndUpdatedBy::Stamper.stampable.send(CreatedAndUpdatedBy::Stamper.attribute)
|
25
13
|
end
|
14
|
+
def updated_by; CreatedAndUpdatedBy::Stamper.stampable.find(updated_by_id); end
|
15
|
+
def created_by; CreatedAndUpdatedBy::Stamper.stampable.find(updated_by_id); end
|
26
16
|
def set_stamps
|
27
|
-
if stamper && respond_to?(:created_by_id) && respond_to?(:updated_by_id)
|
28
|
-
self.created_by ||= stamper
|
29
|
-
self.updated_by = stamper
|
30
|
-
end
|
17
|
+
self.created_by_id ||= self.updated_by_id = stamper.id if stamper && respond_to?(:created_by_id) && respond_to?(:updated_by_id)
|
31
18
|
end
|
32
|
-
end
|
33
|
-
|
19
|
+
end
|
34
20
|
end
|