acts_as_activitable 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,11 +12,13 @@ module ActsAsActivitable
12
12
 
13
13
  module ClassMethods
14
14
  def acts_as_activitable
15
- cattr_accessor :exclude_from_activities
16
- cattr_accessor :methods_for_activities
15
+
16
+ attr_accessor :skip_activity
17
17
  has_many :activities, :as => :activitable, :dependent => :destroy
18
18
 
19
- after_create :create_activity
19
+ after_create :create_create_activity
20
+ after_update :create_update_activity
21
+ after_destroy :create_destroy_activity
20
22
 
21
23
  include ActsAsActivitable::Extensions::InstanceMethods
22
24
  extend ActsAsActivitable::Extensions::SingletonMethods
@@ -24,7 +26,13 @@ module ActsAsActivitable
24
26
  end
25
27
 
26
28
  module SingletonMethods
29
+ def exclude_from_activities
30
+ []
31
+ end
27
32
 
33
+ def methods_for_activities
34
+ []
35
+ end
28
36
  end
29
37
 
30
38
  module InstanceMethods
@@ -32,21 +40,50 @@ module ActsAsActivitable
32
40
  def activity_owner
33
41
  user
34
42
  end
43
+
44
+ def create_activity?(data)
45
+ true
46
+ end
47
+
48
+ def create_create_activity
49
+ create_activity(:create)
50
+ end
51
+
52
+ def create_update_activity
53
+ create_activity(:update)
54
+ end
55
+
56
+ def create_destroy_activity
57
+ create_activity(:destroy)
58
+ end
35
59
 
36
- def create_activity
37
- self.activities.create(:user => activity_owner, :data => self.to_activity_data)
60
+ def create_activity(activity_action)
61
+ unless skip_activity
62
+ data = self.to_activity_data
63
+
64
+ if data
65
+ data.delete(:updated_at)
66
+ data.delete(:birth_date)
67
+ unless data.keys.empty? || !create_activity?(data)
68
+ data[:activity_action] = activity_action
69
+ self.activities.create(:user => activity_owner, :data => data) rescue nil
70
+ end
71
+ end
72
+ end
38
73
  end
39
74
 
40
75
  def to_activity_data
41
- attrs = self.attributes.symbolize_keys
42
- attrs.delete(:id)
43
- (self.class.exclude_from_activities||[]).each do |attribute|
44
- attrs.delete(attribute.to_sym)
45
- end
46
- (self.class.methods_for_activities||[]).each do |method_name|
47
- attrs[method_name.so_sym] = self.send(method_name.to_sym)
76
+ attrs = nil
77
+ if self.changed?
78
+ attrs = self.changes.symbolize_keys
79
+ #attrs.delete(:id)
80
+ (self.class.exclude_from_activities||[]).each do |attribute|
81
+ attrs.delete(attribute.to_sym)
82
+ end
83
+ (self.class.methods_for_activities||[]).each do |method_name|
84
+ attrs[method_name.to_sym] = self.send(method_name.to_sym)
85
+ end
48
86
  end
49
-
50
87
  attrs
51
88
  end
52
89
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsActivitable
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: acts_as_activitable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Gunner Technology, Cody Swann
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-06 00:00:00 -08:00
13
+ date: 2011-03-14 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16