wiser_trails 1.1.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +31 -4
- data/lib/generators/wiser_trails/migration/templates/migration.rb +1 -0
- data/lib/generators/wiser_trails/model/model_generator.rb +2 -2
- data/lib/wiser_trails/actions/creation.rb +3 -2
- data/lib/wiser_trails/actions/destruction.rb +2 -1
- data/lib/wiser_trails/actions/update.rb +8 -1
- data/lib/wiser_trails/common.rb +7 -1
- data/lib/wiser_trails/orm/active_record/activity.rb +2 -1
- data/lib/wiser_trails/orm/active_record/adapter.rb +3 -1
- data/lib/wiser_trails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
Y2E1MmRmZmNmYjVmZWU1MWY4MjNlNWI3MzlkOTk0MjcxYTFhNWZiZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGVjZTU3ZWYxNTk3NjE2ODFiMzFkNDgxYjBmYjA2NmM4NGNkYTZmNw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZThiNWU0MGI3OGQ1NTM3M2JiYTk0NWY5OTJlNWY4NzgzMDQyOGIwMjNlZmQx
|
10
|
+
MmM4OTMxZGMwMzJkZTE5YWM0OTE4MWY0Njc2YjA1NjBkMzk2MDg1ZGYzZDZk
|
11
|
+
NWIyZTJhMmVhYWVkMGQ0YzZkMzMzZGM2Njk4YjJjZjFlNGVlZGM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGFjMTE4OTVhZTQ5ZTY3YTI4OWE1ZDQ5ZWJmOWJkN2I2ZDg3ZTU3ZDNjMjQ2
|
14
|
+
ZjIxMmQwN2FhNDBkMDhhNzFiYmU3MDI3NTg5MWIwYWIxNTViZDhkNGQ5ZTUy
|
15
|
+
ZGE5ODhkMmIxYTdiNWVmMjBlZTI0YjNmYTcyNmMwNWU0NjI1MjA=
|
data/README.md
CHANGED
@@ -17,12 +17,14 @@ You can do normal gem installation for `wiser_trails`:
|
|
17
17
|
or in your Gemfile:
|
18
18
|
|
19
19
|
```ruby
|
20
|
-
gem 'wiser_trails'
|
20
|
+
gem 'wiser_trails', ~> '2.0.0'
|
21
21
|
```
|
22
22
|
|
23
|
+
Then restart your application.
|
24
|
+
|
23
25
|
### Database
|
24
26
|
|
25
|
-
|
27
|
+
Generate a migration for trails and migrate the database (in your Rails project):
|
26
28
|
|
27
29
|
rails g wiser_trails:migration
|
28
30
|
rake db:migrate
|
@@ -68,9 +70,33 @@ class Notes < ActiveRecord::Base
|
|
68
70
|
end
|
69
71
|
```
|
70
72
|
|
73
|
+
This will automatically record all the changes for trailed models.
|
74
|
+
|
75
|
+
#### Good Thing
|
76
|
+
|
77
|
+
_Wiser Trails_ will saved **ONLY** the changed attributes during the `update` action.
|
78
|
+
|
79
|
+
For example:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
@note = Note.create(title: "New Note", content: "This is my first note.")
|
83
|
+
# @note.old_value = {}
|
84
|
+
# @note.new_value = {"title" => "New Note", "content" => "This is my first note."}
|
85
|
+
```
|
86
|
+
|
87
|
+
Then when you update the record:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
@note.update_attribute(:title, "New Title")
|
91
|
+
# @note.old_value = {"title" => "New Note"}
|
92
|
+
# @note.new_value = {"title" => "New Note"}
|
93
|
+
```
|
94
|
+
|
95
|
+
It will not save any unchanged attributes, and you can get the updated fields for displaying.
|
96
|
+
|
71
97
|
### Monitoring Custom Actions
|
72
98
|
|
73
|
-
To record custom actions, you can manually trigger the `create_activity` method on the model record
|
99
|
+
To record custom actions, you can manually trigger the `create_activity` method on the model record but **this will not save the `old_value`**:
|
74
100
|
|
75
101
|
```ruby
|
76
102
|
@note.create_activity(key: 'note.published', owner: current_user)
|
@@ -114,11 +140,12 @@ def show
|
|
114
140
|
end
|
115
141
|
```
|
116
142
|
|
117
|
-
You can also get the new
|
143
|
+
You can also get the old and new object value by calling `old_value` or `new_value`:
|
118
144
|
|
119
145
|
```ruby
|
120
146
|
def show
|
121
147
|
@trail = WiserTrails::Activity.find(params[:id])
|
148
|
+
# @trail.old_value => exact Note attributes before the trail was recorded
|
122
149
|
# @trail.new_value => exact Note attributes after the trail was recorded
|
123
150
|
end
|
124
151
|
```
|
@@ -1,10 +1,10 @@
|
|
1
|
-
require 'generators/
|
1
|
+
require 'generators/wiser_trails'
|
2
2
|
require 'rails/generators/active_record'
|
3
3
|
|
4
4
|
module WiserTrails
|
5
5
|
module Generators
|
6
6
|
# Activity generator that creates activity model file from template
|
7
|
-
class
|
7
|
+
class ModelGenerator < ActiveRecord::Generators::Base
|
8
8
|
extend Base
|
9
9
|
|
10
10
|
argument :name, :type => :string, :default => 'wiser_trails'
|
@@ -9,7 +9,8 @@ module WiserTrails
|
|
9
9
|
private
|
10
10
|
# Creates activity upon creation of the tracked model
|
11
11
|
def activity_on_create
|
12
|
-
create_activity(:create)
|
12
|
+
activity = create_activity(:create, new_value: self.attributes.stringify_keys)
|
13
|
+
activity.update_attribute(:old_value, {}) if activity
|
13
14
|
end
|
14
15
|
end
|
15
|
-
end
|
16
|
+
end
|
@@ -9,7 +9,8 @@ module WiserTrails
|
|
9
9
|
private
|
10
10
|
# Records an activity upon destruction of the tracked model
|
11
11
|
def activity_on_destroy
|
12
|
-
create_activity(:destroy)
|
12
|
+
activity = create_activity(:destroy, old_value: self.attributes)
|
13
|
+
activity.update_attribute(:old_value, self.attributes.stringify_keys) if activity
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -4,12 +4,19 @@ module WiserTrails
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
+
before_update :initialize_activity
|
7
8
|
after_update :activity_on_update
|
8
9
|
end
|
9
10
|
private
|
10
11
|
# Creates activity upon modification of the tracked model
|
12
|
+
def initialize_activity
|
13
|
+
create_activity(:update) if self.changed_attributes.except("updated_at").count > 0
|
14
|
+
end
|
11
15
|
def activity_on_update
|
12
|
-
|
16
|
+
if self.changed_attributes.except("updated_at").count > 0
|
17
|
+
activity = Activity.where(trackable_id: self.id, trackable_type: self.class, key: "#{self.class.to_s.downcase}.update").last
|
18
|
+
activity.update_attribute(:new_value, activity.trackable.attributes.stringify_keys) if activity
|
19
|
+
end
|
13
20
|
end
|
14
21
|
end
|
15
22
|
end
|
data/lib/wiser_trails/common.rb
CHANGED
@@ -298,7 +298,12 @@ module WiserTrails
|
|
298
298
|
)
|
299
299
|
)
|
300
300
|
)
|
301
|
-
|
301
|
+
|
302
|
+
changes = Hash.new
|
303
|
+
self.changed_attributes.each do |attr, val|
|
304
|
+
changes[attr.to_sym] = val if attr != "updated_at"
|
305
|
+
end
|
306
|
+
options[:old_value] = changes.stringify_keys
|
302
307
|
options.delete(:params)
|
303
308
|
|
304
309
|
customs = self.class.activity_custom_fields_global.clone
|
@@ -324,6 +329,7 @@ module WiserTrails
|
|
324
329
|
# called from any other place, or from application code.
|
325
330
|
# @private
|
326
331
|
def reset_activity_instance_options
|
332
|
+
@activity_old_value = {}
|
327
333
|
@activity_new_value = {}
|
328
334
|
@activity_key = nil
|
329
335
|
@activity_owner = nil
|
@@ -14,10 +14,11 @@ module WiserTrails
|
|
14
14
|
# Define ownership to a resource targeted by this activity
|
15
15
|
belongs_to :account, :polymorphic => true
|
16
16
|
# Serialize parameters Hash
|
17
|
+
serialize :old_value, Hash
|
17
18
|
serialize :new_value, Hash
|
18
19
|
|
19
20
|
if ::ActiveRecord::VERSION::MAJOR < 4
|
20
|
-
attr_accessible :key, :owner, :
|
21
|
+
attr_accessible :key, :owner, :account, :trackable, :old_value, :new_value
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
@@ -8,7 +8,9 @@ module WiserTrails
|
|
8
8
|
class Adapter
|
9
9
|
# Creates the activity on `trackable` with `options`
|
10
10
|
def self.create_activity(trackable, options)
|
11
|
-
trackable.activities.create options
|
11
|
+
activity = trackable.activities.create options
|
12
|
+
activity.update_attribute(:new_value, trackable.attributes.stringify_keys) if activity.new_value == {}
|
13
|
+
return activity
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
data/lib/wiser_trails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wiser_trails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenneth John Balgos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|