activitylog 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/VERSION +1 -1
- data/activitylog.gemspec +1 -1
- data/lib/activity_log/activity_log.rb +5 -21
- data/lib/activity_log/activity_logger.rb +7 -6
- data/lib/activitylog.rb +1 -0
- data/lib/generators/activity_log/templates/create_activity_logs.rb +8 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YjVmMmJjZWJjNzkyZTkzNzYzMzYzM2I5MjRmNDczYWZhNDBmZTFjYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzEwZjVjYzU3NDg4ZjJkZGYxZGZkZjk1MTBiY2M0ZjJjYjM0MGYzYg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODQ5YjFkMjhhOGIxMWU5ZTNmM2VlNGI5NzM2YzAxMjBhYTQ1YzIwZWZlOTU5
|
10
|
+
ZDk4ZDU0MGQ4MDhhYWNkNjMwMzU4MTcwMTQ0YzNkYmRkMzIyYTY5OTNkYjky
|
11
|
+
YzQ1OTE5ZGM4MmUxOTFkODEzZjQ2MDRhMDJhMjBhNWRlYWIzYzY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzRjNDA5ZWU5OGM0YjNjNzk3MmNhNTQwZTc1ZjBlNzBmM2EzNTI4NDdhMzY0
|
14
|
+
ODAxMTNhNWE3NjJjZmEzMzgzZjUwZWI0OTdjYzVjOTI2ZjQxMTgyYzE4YzJk
|
15
|
+
NGY1MjkzMmNiMjg4ZGM5YjkzOGZjYWE5NWFmOTllYmU2MDA0YTU=
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/activitylog.gemspec
CHANGED
@@ -2,26 +2,10 @@ class ActivityLog < ActiveRecord::Base
|
|
2
2
|
|
3
3
|
serialize :data, Hash
|
4
4
|
|
5
|
-
belongs_to :parent, :
|
6
|
-
has_many :children, :
|
5
|
+
belongs_to :parent, class_name: 'ActivityLog', foreign_key: 'parent_activity_id'
|
6
|
+
has_many :children, class_name: 'ActivityLog', foreign_key: 'parent_activity_id'
|
7
|
+
belongs_to :loggable, polymorphic: true
|
8
|
+
belongs_to :user
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
options = {
|
11
|
-
"user_id" => user.id,
|
12
|
-
"object_id" => object.id,
|
13
|
-
"object_type" => object.class.name,
|
14
|
-
"action" => action,
|
15
|
-
"data" => data,
|
16
|
-
"parent_activity_id" => parent_activity_id
|
17
|
-
}
|
18
|
-
log = ActivityLog.create(options)
|
19
|
-
|
20
|
-
if log.save
|
21
|
-
log
|
22
|
-
else
|
23
|
-
raise "ActivityLog failed to be saved" # TODO: this is just temporary
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
10
|
+
validates :type, inclusion: {in: %w(model controller)}
|
27
11
|
end
|
@@ -9,25 +9,26 @@ module ActivityLogger
|
|
9
9
|
self.stored_parent_activity_id = nil
|
10
10
|
end
|
11
11
|
|
12
|
-
def record_model_activity_log(
|
12
|
+
def record_model_activity_log(action)
|
13
13
|
# if current_user_id == nil, then the User is in the process of Logging In
|
14
|
-
current_id = defined?(current_user_id) ?
|
15
|
-
|
14
|
+
current_id = defined?(current_user_id) ? current_user_id : nil
|
15
|
+
activity_logs.create user_id: current_id, action: action, data: changes, type: 'model', stored_parent_activity_id: self.stored_parent_activity_id
|
16
16
|
end
|
17
17
|
|
18
18
|
def record_controller_activity_log
|
19
19
|
return unless params[:id].present?
|
20
20
|
return if request.get? #exit if it's a get
|
21
|
-
object = controller_name.classify.constantize.find_by_id(params[:id])
|
21
|
+
object = controller_name.classify.constantize.find_by_id(params[:id])
|
22
22
|
return unless object.present?
|
23
|
-
|
23
|
+
return unless object.respond_to? :activity_logs
|
24
|
+
object.activity_logs.create user_id: current_user.try(:id), action: "#{params[:controller]}/#{params[:action]}", data: params, type: 'controller'
|
24
25
|
self.stored_parent_activity_id = activity.id
|
25
26
|
end
|
26
27
|
|
27
28
|
# CLASS METHODS (self.)
|
28
29
|
module ClassMethods
|
29
30
|
def log_model_activity
|
30
|
-
self.has_many :
|
31
|
+
self.has_many :activity_logs, as: :loggable
|
31
32
|
self.after_create {record_model_activity_log("create")}
|
32
33
|
self.after_update {record_model_activity_log("update")}
|
33
34
|
self.before_destroy {record_model_activity_log("destroy")}
|
data/lib/activitylog.rb
CHANGED
@@ -2,12 +2,18 @@ class CreateActivityLogs < ActiveRecord::Migration
|
|
2
2
|
def change
|
3
3
|
create_table :activity_logs do |t|
|
4
4
|
t.integer :user_id
|
5
|
-
t.integer :
|
6
|
-
t.
|
5
|
+
t.integer :loggable_id
|
6
|
+
t.integer :loggable_type
|
7
7
|
t.string :action
|
8
8
|
t.integer :parent_activity_id
|
9
|
+
t.string :type
|
9
10
|
t.binary :data, :limit => 10.megabyte
|
10
11
|
t.timestamps
|
11
12
|
end
|
13
|
+
|
14
|
+
add_index :activity_logs, [:loggable_id, :loggable_type]
|
15
|
+
add_index :activity_logs, :user_id
|
16
|
+
add_index :activity_logs, :parent_activity_id
|
17
|
+
add_index :activity_logs, :type
|
12
18
|
end
|
13
19
|
end
|