hound 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +2 -3
- data/README.md +30 -2
- data/lib/generators/hound/templates/create_hound_actions.rb +1 -0
- data/lib/hound/action.rb +2 -1
- data/lib/hound/model.rb +20 -4
- data/lib/hound/version.rb +1 -1
- data/test/dummy/db/migrate/20130310103454_add_changeset_to_hound_actions.rb +5 -0
- data/test/dummy/db/schema.rb +2 -1
- data/test/unit/model_test.rb +28 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbbb90e55615005651013a007a49add63ba47920
|
4
|
+
data.tar.gz: 6b477231c8ac9307b8e6b6a8a6aa0a3a0a66044c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc5d8f271aabddb48373cfcee959c9e3d0848c1f018719cb9dc3bc96d250b56d701e15ef939f7b06df9bf0a6f87fe3d695ea30e14231d66356e674b6568dd04c
|
7
|
+
data.tar.gz: 635412aa1c93cacef0b7ba2d687682b2827619da9a00b0e4f27f21b19c18166287a0ffa12b64b59ba0cb97c5c5aedafec695b65922def8860058b9f514caee97
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -65,7 +65,29 @@ Hound.config.user_class = 'CustomUser'
|
|
65
65
|
Hound.config.user_class = AdminUser # String or constant
|
66
66
|
```
|
67
67
|
|
68
|
-
|
68
|
+
You can also disable Hound on a model instance basis:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
article = Article.new title: 'Hello, World!'
|
72
|
+
article.hound? #=> true
|
73
|
+
article.hound = false # disable hound
|
74
|
+
article.save
|
75
|
+
article.actions #=> []
|
76
|
+
```
|
77
|
+
|
78
|
+
## Changes
|
79
|
+
|
80
|
+
Hound also tracks the changes made when updating your hounded records. You
|
81
|
+
can access the change updates through the `changeset` attribute:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
article = Article.create! title: 'Hello, World!'
|
85
|
+
article.update_attributes(title: 'Salut, World!')
|
86
|
+
article.actions.last.changeset
|
87
|
+
#=> {"title" => ["Hello, World!", "Salut, World!"]}
|
88
|
+
```
|
89
|
+
|
90
|
+
## Cleaning Up
|
69
91
|
|
70
92
|
With all this action creating we're doing, your database is bound to start
|
71
93
|
getting full quickly. You have two options for cleaning up after yourself,
|
@@ -90,4 +112,10 @@ configure this globally through `Hound.config.limit`, too. Do note though
|
|
90
112
|
that adding this functionality means whenever an action is tracked, Hound
|
91
113
|
will not only create a new action, it will check and destroy any actions
|
92
114
|
outside of this limit. This requires an extra call to the database, so if
|
93
|
-
that could be an issue, using a rake task might be a better idea.
|
115
|
+
that could be an issue, using a rake task might be a better idea.
|
116
|
+
|
117
|
+
## TODO
|
118
|
+
|
119
|
+
* Implement action grouping
|
120
|
+
* Generate a config initializer on install?
|
121
|
+
* Disable hound in test environment?
|
data/lib/hound/action.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module Hound
|
2
2
|
class Action < ActiveRecord::Base
|
3
3
|
self.table_name = 'hound_actions'
|
4
|
-
attr_accessible :action, :actionable_id, :actionable_type, :user_id
|
4
|
+
attr_accessible :action, :actionable_id, :actionable_type, :user_id, :changeset
|
5
5
|
belongs_to :actionable, polymorphic: true
|
6
6
|
belongs_to :user, class_name: Hound.config.user_class
|
7
|
+
serialize :changeset, Hash
|
7
8
|
|
8
9
|
scope :created, -> { where(action: 'create') }
|
9
10
|
scope :updated, -> { where(action: 'update') }
|
data/lib/hound/model.rb
CHANGED
@@ -23,6 +23,8 @@ module Hound
|
|
23
23
|
class_attribute :hound_options
|
24
24
|
self.hound_options = options.dup
|
25
25
|
|
26
|
+
attr_accessor :hound
|
27
|
+
|
26
28
|
# Add action hooks
|
27
29
|
after_create :hound_create if options[:actions].include?('create')
|
28
30
|
before_update :hound_update if options[:actions].include?('update')
|
@@ -32,27 +34,41 @@ module Hound
|
|
32
34
|
|
33
35
|
module InstanceMethods
|
34
36
|
|
37
|
+
# Return all actions in provided date.
|
38
|
+
def actions_for_date(date)
|
39
|
+
actions.where(created_at: date)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns true if hound is enabled on this instance.
|
43
|
+
def hound?
|
44
|
+
hound != false
|
45
|
+
end
|
46
|
+
|
35
47
|
private
|
36
48
|
|
37
49
|
def hound_create
|
50
|
+
return unless hound?
|
38
51
|
attributes = default_attributes.merge(action: 'create')
|
39
52
|
actions.create! attributes
|
40
|
-
|
53
|
+
enforce_limit
|
41
54
|
end
|
42
55
|
|
43
56
|
def hound_update
|
57
|
+
return unless hound?
|
44
58
|
attributes = default_attributes.merge(action: 'update')
|
59
|
+
attributes.merge!(changeset: changes)
|
45
60
|
actions.create! attributes
|
46
|
-
|
61
|
+
enforce_limit
|
47
62
|
end
|
48
63
|
|
49
64
|
def hound_destroy
|
65
|
+
return unless hound?
|
50
66
|
attributes = default_attributes.merge(action: 'destroy')
|
51
67
|
attributes.merge!(
|
52
68
|
actionable_id: self.id,
|
53
69
|
actionable_type: self.class.base_class.name)
|
54
70
|
Hound::Action.create(attributes)
|
55
|
-
|
71
|
+
enforce_limit
|
56
72
|
end
|
57
73
|
|
58
74
|
def default_attributes
|
@@ -61,7 +77,7 @@ module Hound
|
|
61
77
|
}
|
62
78
|
end
|
63
79
|
|
64
|
-
def
|
80
|
+
def enforce_limit
|
65
81
|
limit = self.class.hound_options[:limit]
|
66
82
|
limit ||= Hound.config.limit
|
67
83
|
if limit and actions.size > limit
|
data/lib/hound/version.rb
CHANGED
data/test/dummy/db/schema.rb
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(:version =>
|
14
|
+
ActiveRecord::Schema.define(:version => 20130310103454) do
|
15
15
|
|
16
16
|
create_table "articles", :force => true do |t|
|
17
17
|
t.string "title"
|
@@ -26,6 +26,7 @@ ActiveRecord::Schema.define(:version => 20130308110847) do
|
|
26
26
|
t.integer "actionable_id", :null => false
|
27
27
|
t.integer "user_id"
|
28
28
|
t.datetime "created_at"
|
29
|
+
t.text "changeset"
|
29
30
|
end
|
30
31
|
|
31
32
|
add_index "hound_actions", ["actionable_type", "actionable_id"], :name => "index_hound_actions_on_actionable_type_and_actionable_id"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ModelTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
@article = Article.create! title: 'Hello, World!', body: 'Lorem Ipsum'
|
7
|
+
end
|
8
|
+
|
9
|
+
test 'actions_for_date' do
|
10
|
+
@article.actions.create!(action: 'created').tap { |x| x.created_at = 3.days.ago; x.save }
|
11
|
+
@article.actions.create!(action: 'created').tap { |x| x.created_at = 5.days.from_now; x.save }
|
12
|
+
@article.actions.create!(action: 'created').tap { |x| x.created_at = Date.today; x.save }
|
13
|
+
assert_equal 2, @article.actions_for_date(2.days.ago..3.days.from_now).size
|
14
|
+
end
|
15
|
+
|
16
|
+
test 'avoid tracking actions on an instance' do
|
17
|
+
article = Article.new title: 'Hello, World!', body: 'Lorem Ipsum'
|
18
|
+
article.hound = false
|
19
|
+
article.save
|
20
|
+
assert_empty article.actions
|
21
|
+
end
|
22
|
+
|
23
|
+
test 'tracking model changes' do
|
24
|
+
@article.update_attributes(title: 'Salut, World!')
|
25
|
+
assert_equal({"title" => ["Hello, World!", "Salut, World!"]}, @article.actions.last.changeset)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hound
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lee Jarvis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- test/dummy/db/migrate/20130307165837_create_articles.rb
|
100
100
|
- test/dummy/db/migrate/20130307183451_create_posts.rb
|
101
101
|
- test/dummy/db/migrate/20130308110847_create_hound_actions.rb
|
102
|
+
- test/dummy/db/migrate/20130310103454_add_changeset_to_hound_actions.rb
|
102
103
|
- test/dummy/db/schema.rb
|
103
104
|
- test/dummy/lib/assets/.gitkeep
|
104
105
|
- test/dummy/log/.gitkeep
|
@@ -118,6 +119,7 @@ files:
|
|
118
119
|
- test/functional/controller_test.rb
|
119
120
|
- test/hound_test.rb
|
120
121
|
- test/test_helper.rb
|
122
|
+
- test/unit/model_test.rb
|
121
123
|
homepage: https://github.com/injekt/hound
|
122
124
|
licenses: []
|
123
125
|
metadata: {}
|
@@ -179,6 +181,7 @@ test_files:
|
|
179
181
|
- test/dummy/db/migrate/20130307165837_create_articles.rb
|
180
182
|
- test/dummy/db/migrate/20130307183451_create_posts.rb
|
181
183
|
- test/dummy/db/migrate/20130308110847_create_hound_actions.rb
|
184
|
+
- test/dummy/db/migrate/20130310103454_add_changeset_to_hound_actions.rb
|
182
185
|
- test/dummy/db/schema.rb
|
183
186
|
- test/dummy/lib/assets/.gitkeep
|
184
187
|
- test/dummy/log/.gitkeep
|
@@ -198,3 +201,4 @@ test_files:
|
|
198
201
|
- test/functional/controller_test.rb
|
199
202
|
- test/hound_test.rb
|
200
203
|
- test/test_helper.rb
|
204
|
+
- test/unit/model_test.rb
|