public_activity 0.5.4 → 1.0.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.
- data/Gemfile +19 -2
- data/README.md +103 -19
- data/UPGRADING +4 -13
- data/lib/generators/public_activity/migration/templates/migration.rb +4 -0
- data/lib/public_activity.rb +15 -11
- data/lib/public_activity/{creation.rb → actions/creation.rb} +0 -0
- data/lib/public_activity/{destruction.rb → actions/destruction.rb} +0 -0
- data/lib/public_activity/{update.rb → actions/update.rb} +0 -0
- data/lib/public_activity/common.rb +195 -18
- data/lib/public_activity/config.rb +49 -3
- data/lib/public_activity/orm/active_record.rb +5 -0
- data/lib/public_activity/orm/active_record/activist.rb +42 -0
- data/lib/public_activity/orm/active_record/activity.rb +23 -0
- data/lib/public_activity/orm/active_record/adapter.rb +14 -0
- data/lib/public_activity/orm/active_record/trackable.rb +11 -0
- data/lib/public_activity/orm/mongoid.rb +4 -0
- data/lib/public_activity/orm/mongoid/activist.rb +42 -0
- data/lib/public_activity/orm/mongoid/activity.rb +25 -0
- data/lib/public_activity/orm/mongoid/adapter.rb +14 -0
- data/lib/public_activity/orm/mongoid/trackable.rb +11 -0
- data/lib/public_activity/{activity.rb → renderable.rb} +40 -56
- data/lib/public_activity/roles/deactivatable.rb +39 -0
- data/lib/public_activity/roles/tracked.rb +183 -0
- data/lib/public_activity/{store_controller.rb → utility/store_controller.rb} +0 -0
- data/lib/public_activity/{view_helpers.rb → utility/view_helpers.rb} +0 -0
- data/lib/public_activity/version.rb +1 -1
- metadata +40 -154
- data/lib/public_activity/activist.rb +0 -37
- data/lib/public_activity/tracked.rb +0 -337
File without changes
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: public_activity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,56 +10,40 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2013-02-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: activerecord
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
|
-
requirements:
|
20
|
-
- - ! '>='
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: 3.0.0
|
23
|
-
type: :runtime
|
24
|
-
prerelease: false
|
25
|
-
version_requirements: !ruby/object:Gem::Requirement
|
26
|
-
none: false
|
27
|
-
requirements:
|
28
|
-
- - ! '>='
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: 3.0.0
|
31
15
|
- !ruby/object:Gem::Dependency
|
32
16
|
name: activesupport
|
33
17
|
requirement: !ruby/object:Gem::Requirement
|
34
18
|
none: false
|
35
19
|
requirements:
|
36
|
-
- -
|
20
|
+
- - ~>
|
37
21
|
- !ruby/object:Gem::Version
|
38
|
-
version: 3.0
|
22
|
+
version: '3.0'
|
39
23
|
type: :runtime
|
40
24
|
prerelease: false
|
41
25
|
version_requirements: !ruby/object:Gem::Requirement
|
42
26
|
none: false
|
43
27
|
requirements:
|
44
|
-
- -
|
28
|
+
- - ~>
|
45
29
|
- !ruby/object:Gem::Version
|
46
|
-
version: 3.0
|
30
|
+
version: '3.0'
|
47
31
|
- !ruby/object:Gem::Dependency
|
48
32
|
name: actionpack
|
49
33
|
requirement: !ruby/object:Gem::Requirement
|
50
34
|
none: false
|
51
35
|
requirements:
|
52
|
-
- -
|
36
|
+
- - ~>
|
53
37
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3.0
|
38
|
+
version: '3.0'
|
55
39
|
type: :runtime
|
56
40
|
prerelease: false
|
57
41
|
version_requirements: !ruby/object:Gem::Requirement
|
58
42
|
none: false
|
59
43
|
requirements:
|
60
|
-
- -
|
44
|
+
- - ~>
|
61
45
|
- !ruby/object:Gem::Version
|
62
|
-
version: 3.0
|
46
|
+
version: '3.0'
|
63
47
|
- !ruby/object:Gem::Dependency
|
64
48
|
name: i18n
|
65
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,103 +60,23 @@ dependencies:
|
|
76
60
|
- - ! '>='
|
77
61
|
- !ruby/object:Gem::Version
|
78
62
|
version: 0.5.0
|
79
|
-
- !ruby/object:Gem::Dependency
|
80
|
-
name: simplecov
|
81
|
-
requirement: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
|
-
requirements:
|
84
|
-
- - ! '>='
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
version: 0.6.4
|
87
|
-
type: :development
|
88
|
-
prerelease: false
|
89
|
-
version_requirements: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
|
-
requirements:
|
92
|
-
- - ! '>='
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
version: 0.6.4
|
95
|
-
- !ruby/object:Gem::Dependency
|
96
|
-
name: bundler
|
97
|
-
requirement: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
|
-
requirements:
|
100
|
-
- - ! '>='
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
version: '1.1'
|
103
|
-
type: :development
|
104
|
-
prerelease: false
|
105
|
-
version_requirements: !ruby/object:Gem::Requirement
|
106
|
-
none: false
|
107
|
-
requirements:
|
108
|
-
- - ! '>='
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '1.1'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: rake
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
none: false
|
115
|
-
requirements:
|
116
|
-
- - ! '>='
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
version: '0.9'
|
119
|
-
type: :development
|
120
|
-
prerelease: false
|
121
|
-
version_requirements: !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
|
-
requirements:
|
124
|
-
- - ! '>='
|
125
|
-
- !ruby/object:Gem::Version
|
126
|
-
version: '0.9'
|
127
|
-
- !ruby/object:Gem::Dependency
|
128
|
-
name: sqlite3
|
129
|
-
requirement: !ruby/object:Gem::Requirement
|
130
|
-
none: false
|
131
|
-
requirements:
|
132
|
-
- - ! '>='
|
133
|
-
- !ruby/object:Gem::Version
|
134
|
-
version: '0'
|
135
|
-
type: :development
|
136
|
-
prerelease: false
|
137
|
-
version_requirements: !ruby/object:Gem::Requirement
|
138
|
-
none: false
|
139
|
-
requirements:
|
140
|
-
- - ! '>='
|
141
|
-
- !ruby/object:Gem::Version
|
142
|
-
version: '0'
|
143
|
-
- !ruby/object:Gem::Dependency
|
144
|
-
name: mocha
|
145
|
-
requirement: !ruby/object:Gem::Requirement
|
146
|
-
none: false
|
147
|
-
requirements:
|
148
|
-
- - ! '>='
|
149
|
-
- !ruby/object:Gem::Version
|
150
|
-
version: 0.12.1
|
151
|
-
type: :development
|
152
|
-
prerelease: false
|
153
|
-
version_requirements: !ruby/object:Gem::Requirement
|
154
|
-
none: false
|
155
|
-
requirements:
|
156
|
-
- - ! '>='
|
157
|
-
- !ruby/object:Gem::Version
|
158
|
-
version: 0.12.1
|
159
63
|
- !ruby/object:Gem::Dependency
|
160
64
|
name: railties
|
161
65
|
requirement: !ruby/object:Gem::Requirement
|
162
66
|
none: false
|
163
67
|
requirements:
|
164
|
-
- -
|
68
|
+
- - ~>
|
165
69
|
- !ruby/object:Gem::Version
|
166
|
-
version: 3.0
|
167
|
-
type: :
|
70
|
+
version: '3.0'
|
71
|
+
type: :runtime
|
168
72
|
prerelease: false
|
169
73
|
version_requirements: !ruby/object:Gem::Requirement
|
170
74
|
none: false
|
171
75
|
requirements:
|
172
|
-
- -
|
76
|
+
- - ~>
|
173
77
|
- !ruby/object:Gem::Version
|
174
|
-
version: 3.0
|
175
|
-
description:
|
78
|
+
version: '3.0'
|
79
|
+
description: Easy activity tracking for your ActiveRecord models. Provides Activity
|
176
80
|
model with details about actions performed by your users, like adding comments,
|
177
81
|
responding etc.
|
178
82
|
email: piotrek@okonski.org
|
@@ -188,17 +92,27 @@ files:
|
|
188
92
|
- lib/generators/public_activity/migration_upgrade/migration_upgrade_generator.rb
|
189
93
|
- lib/generators/public_activity/migration_upgrade/templates/upgrade.rb
|
190
94
|
- lib/public_activity.rb
|
191
|
-
- lib/public_activity/
|
192
|
-
- lib/public_activity/
|
95
|
+
- lib/public_activity/actions/creation.rb
|
96
|
+
- lib/public_activity/actions/destruction.rb
|
97
|
+
- lib/public_activity/actions/update.rb
|
193
98
|
- lib/public_activity/common.rb
|
194
99
|
- lib/public_activity/config.rb
|
195
|
-
- lib/public_activity/
|
196
|
-
- lib/public_activity/
|
197
|
-
- lib/public_activity/
|
198
|
-
- lib/public_activity/
|
199
|
-
- lib/public_activity/
|
100
|
+
- lib/public_activity/orm/active_record.rb
|
101
|
+
- lib/public_activity/orm/active_record/activist.rb
|
102
|
+
- lib/public_activity/orm/active_record/activity.rb
|
103
|
+
- lib/public_activity/orm/active_record/adapter.rb
|
104
|
+
- lib/public_activity/orm/active_record/trackable.rb
|
105
|
+
- lib/public_activity/orm/mongoid.rb
|
106
|
+
- lib/public_activity/orm/mongoid/activist.rb
|
107
|
+
- lib/public_activity/orm/mongoid/activity.rb
|
108
|
+
- lib/public_activity/orm/mongoid/adapter.rb
|
109
|
+
- lib/public_activity/orm/mongoid/trackable.rb
|
110
|
+
- lib/public_activity/renderable.rb
|
111
|
+
- lib/public_activity/roles/deactivatable.rb
|
112
|
+
- lib/public_activity/roles/tracked.rb
|
113
|
+
- lib/public_activity/utility/store_controller.rb
|
114
|
+
- lib/public_activity/utility/view_helpers.rb
|
200
115
|
- lib/public_activity/version.rb
|
201
|
-
- lib/public_activity/view_helpers.rb
|
202
116
|
- Gemfile
|
203
117
|
- Rakefile
|
204
118
|
- README.md
|
@@ -206,38 +120,10 @@ files:
|
|
206
120
|
- UPGRADING
|
207
121
|
homepage: https://github.com/pokonski/public_activity
|
208
122
|
licenses: []
|
209
|
-
post_install_message: !
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
################################################
|
214
|
-
|
215
|
-
|
216
|
-
The 0.5 version of public_activity will be the
|
217
|
-
|
218
|
-
last to support versions older than 1.9.2. Please
|
219
|
-
|
220
|
-
update your Ruby if you wish to continue using
|
221
|
-
|
222
|
-
this gem.
|
223
|
-
|
224
|
-
|
225
|
-
--------------------------------------------------
|
226
|
-
|
227
|
-
| NOTE FOR UPGRADING FROM PRE-0.4.0 VERSION |
|
228
|
-
|
229
|
-
--------------------------------------------------
|
230
|
-
|
231
|
-
|
232
|
-
public_activity 0.4.0 brings major changes compared to 0.3.X versions,
|
233
|
-
|
234
|
-
please read https://github.com/pokonski/public_activity#upgrading
|
235
|
-
|
236
|
-
to learn about all the changes you need to apply to properly
|
237
|
-
|
238
|
-
upgrade your applications.
|
239
|
-
|
240
|
-
'
|
123
|
+
post_install_message: ! "##################################################\n# NOTE
|
124
|
+
FOR UPGRADING FROM PRE-0.4.0 VERSION #\n##################################################\n\npublic_activity
|
125
|
+
0.4.0 brings major changes compared to 0.3.X versions,\nplease read https://github.com/pokonski/public_activity#upgrading
|
126
|
+
\nto learn about all the changes you need to apply to properly\nupgrade your applications.\n"
|
241
127
|
rdoc_options: []
|
242
128
|
require_paths:
|
243
129
|
- lib
|
@@ -246,7 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
246
132
|
requirements:
|
247
133
|
- - ! '>='
|
248
134
|
- !ruby/object:Gem::Version
|
249
|
-
version: 1.
|
135
|
+
version: 1.9.2
|
250
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
251
137
|
none: false
|
252
138
|
requirements:
|
@@ -258,6 +144,6 @@ rubyforge_project:
|
|
258
144
|
rubygems_version: 1.8.24
|
259
145
|
signing_key:
|
260
146
|
specification_version: 3
|
261
|
-
summary:
|
147
|
+
summary: Easy activity tracking for ActiveRecord models
|
262
148
|
test_files: []
|
263
149
|
has_rdoc:
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module PublicActivity
|
2
|
-
# Module extending classes that serve as owners
|
3
|
-
module Activist
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
# Association of activities as their owner.
|
7
|
-
# @!method activities
|
8
|
-
# @return [Array<Activity>] Activities which self is the owner of.
|
9
|
-
|
10
|
-
# Association of activities as their recipient.
|
11
|
-
# @!method private_activities
|
12
|
-
# @return [Array<Activity>] Activities which self is the recipient of.
|
13
|
-
|
14
|
-
# Module extending classes that serve as owners
|
15
|
-
module ClassMethods
|
16
|
-
# Adds ActiveRecord associations to model to simplify fetching
|
17
|
-
# so you can list activities performed by the owner.
|
18
|
-
# It is completely optional. Any model can be an owner to an activity
|
19
|
-
# even without being an explicit activist.
|
20
|
-
#
|
21
|
-
# == Usage:
|
22
|
-
# In model:
|
23
|
-
#
|
24
|
-
# class User < ActiveRecord::Base
|
25
|
-
# activist
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
# In controller:
|
29
|
-
# User.first.activities
|
30
|
-
#
|
31
|
-
def activist
|
32
|
-
has_many :activities_as_owner, :class_name => "PublicActivity::Activity", :as => :owner
|
33
|
-
has_many :activities_as_recipient, :class_name => "PublicActivity::Activity", :as => :recipient
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,337 +0,0 @@
|
|
1
|
-
module PublicActivity
|
2
|
-
# Main module extending classes we want to keep track of.
|
3
|
-
module Tracked
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
class_attribute :activity_owner_global, :activity_recipient_global,
|
8
|
-
:activity_params_global, :activity_hooks, :public_activity_enabled_for_model
|
9
|
-
self.activity_owner_global = nil
|
10
|
-
self.activity_recipient_global = nil
|
11
|
-
self.activity_params_global = {}
|
12
|
-
self.activity_hooks = {}
|
13
|
-
self.public_activity_enabled_for_model = true
|
14
|
-
end
|
15
|
-
|
16
|
-
# @!group Global options
|
17
|
-
|
18
|
-
# @!attribute activity_owner_global
|
19
|
-
# Global version of activity owner
|
20
|
-
# @see #activity_owner
|
21
|
-
# @return [Model]
|
22
|
-
|
23
|
-
# @!attribute activity_recipient_global
|
24
|
-
# Global version of activity recipient
|
25
|
-
# @see #activity_recipient
|
26
|
-
# @return [Model]
|
27
|
-
|
28
|
-
# @!attribute activity_params_global
|
29
|
-
# Global version of activity parameters
|
30
|
-
# @see #activity_params
|
31
|
-
# @return [Hash<Symbol, Object>]
|
32
|
-
|
33
|
-
# @!attribute activity_hooks
|
34
|
-
# @return [Hash<Symbol, Proc>]
|
35
|
-
# Hooks/functions that will be used to decide *if* the activity should get
|
36
|
-
# created.
|
37
|
-
#
|
38
|
-
# The supported keys are:
|
39
|
-
# * :create
|
40
|
-
# * :update
|
41
|
-
# * :destroy
|
42
|
-
|
43
|
-
# @!endgroup
|
44
|
-
|
45
|
-
# @!group Instance options
|
46
|
-
|
47
|
-
# Set or get parameters that will be passed to {Activity} when saving
|
48
|
-
#
|
49
|
-
# == Usage:
|
50
|
-
#
|
51
|
-
# @article.activity_params = {:article_title => @article.title}
|
52
|
-
# @article.save
|
53
|
-
#
|
54
|
-
# This way you can pass strings that should remain constant, even when model attributes
|
55
|
-
# change after creating this {Activity}.
|
56
|
-
# @return [Hash<Symbol, Object>]
|
57
|
-
attr_accessor :activity_params
|
58
|
-
@activity_params = {}
|
59
|
-
# Set or get owner object responsible for the {Activity}.
|
60
|
-
#
|
61
|
-
# == Usage:
|
62
|
-
#
|
63
|
-
# # where current_user is an object of logged in user
|
64
|
-
# @article.activity_owner = current_user
|
65
|
-
# # OR: take @article.author association
|
66
|
-
# @article.activity_owner = :author
|
67
|
-
# # OR: provide a Proc with custom code
|
68
|
-
# @article.activity_owner = proc {|controller, model| model.author }
|
69
|
-
# @article.save
|
70
|
-
# @article.activities.last.owner #=> Returns owner object
|
71
|
-
# @return [Model] Polymorphic model
|
72
|
-
# @see #activity_owner_global
|
73
|
-
attr_accessor :activity_owner
|
74
|
-
@activity_owner = nil
|
75
|
-
|
76
|
-
# Set or get recipient for activity.
|
77
|
-
#
|
78
|
-
# Association is polymorphic, thus allowing assignment of
|
79
|
-
# all types of models. This can be used for example in the case of sending
|
80
|
-
# private notifications for only a single user.
|
81
|
-
# @return (see #activity_owner)
|
82
|
-
attr_accessor :activity_recipient
|
83
|
-
@activity_recipient = nil
|
84
|
-
# Set or get custom i18n key passed to {Activity}, later used in {Activity#text}
|
85
|
-
#
|
86
|
-
# == Usage:
|
87
|
-
#
|
88
|
-
# @article = Article.new
|
89
|
-
# @article.activity_key = "my.custom.article.key"
|
90
|
-
# @article.save
|
91
|
-
# @article.activities.last.key #=> "my.custom.article.key"
|
92
|
-
#
|
93
|
-
# @return [String]
|
94
|
-
attr_accessor :activity_key
|
95
|
-
@activity_key = nil
|
96
|
-
|
97
|
-
# @!visibility private
|
98
|
-
@@activity_hooks = {}
|
99
|
-
|
100
|
-
# @!endgroup
|
101
|
-
|
102
|
-
# A shortcut method for setting custom key, owner and parameters of {Activity}
|
103
|
-
# in one line. Accepts a hash with 3 keys:
|
104
|
-
# :key, :owner, :params. You can specify all of them or just the ones you want to overwrite.
|
105
|
-
#
|
106
|
-
# == Options
|
107
|
-
#
|
108
|
-
# [:key]
|
109
|
-
# See {#activity_key}
|
110
|
-
# [:owner]
|
111
|
-
# See {#activity_owner}
|
112
|
-
# [:params]
|
113
|
-
# See {#activity_params}
|
114
|
-
# [:recipient]
|
115
|
-
# Set the recipient for this activity. Useful for private notifications, which should only be visible to a certain user. See {#activity_recipient}.
|
116
|
-
# @example
|
117
|
-
#
|
118
|
-
# @article = Article.new
|
119
|
-
# @article.title = "New article"
|
120
|
-
# @article.activity :key => "my.custom.article.key", :owner => @article.author, :params => {:title => @article.title}
|
121
|
-
# @article.save
|
122
|
-
# @article.activities.last.key #=> "my.custom.article.key"
|
123
|
-
# @article.activities.last.parameters #=> {:title => "New article"}
|
124
|
-
#
|
125
|
-
# @param options [Hash] instance options to set on the tracked model
|
126
|
-
# @return [nil]
|
127
|
-
def activity(options = {})
|
128
|
-
self.activity_key = options[:key] if options[:key]
|
129
|
-
self.activity_owner = options[:owner] if options[:owner]
|
130
|
-
self.activity_params = options[:params] if options[:params]
|
131
|
-
self.activity_recipient = options[:recipient] if options[:recipient]
|
132
|
-
nil
|
133
|
-
end
|
134
|
-
|
135
|
-
# Module with basic +tracked+ method that enables tracking models.
|
136
|
-
module ClassMethods
|
137
|
-
# Adds required callbacks for creating and updating
|
138
|
-
# tracked models and adds +activities+ relation for listing
|
139
|
-
# associated activities.
|
140
|
-
#
|
141
|
-
# == Parameters:
|
142
|
-
# [:owner]
|
143
|
-
# Specify the owner of the {Activity} (person responsible for the action).
|
144
|
-
# It can be a Proc, Symbol or an ActiveRecord object:
|
145
|
-
# == Examples:
|
146
|
-
#
|
147
|
-
# tracked :owner => :author
|
148
|
-
# tracked :owner => {|o| o.author}
|
149
|
-
#
|
150
|
-
# Keep in mind that owner relation is polymorphic, so you can't just
|
151
|
-
# provide id number of the owner object.
|
152
|
-
# [:recipient]
|
153
|
-
# Specify the recipient of the {Activity}
|
154
|
-
# It can be a Proc, Symbol, or an ActiveRecord object
|
155
|
-
# == Examples:
|
156
|
-
#
|
157
|
-
# tracked :recipient => :author
|
158
|
-
# tracked :recipient => {|o| o.author}
|
159
|
-
#
|
160
|
-
# Keep in mind that recipient relation is polymorphic, so you can't just
|
161
|
-
# provide id number of the owner object.
|
162
|
-
# [:params]
|
163
|
-
# Accepts a Hash with custom parameters you want to pass to i18n.translate
|
164
|
-
# method. It is later used in {Activity#text} method.
|
165
|
-
# == Example:
|
166
|
-
# class Article < ActiveRecord::Base
|
167
|
-
# include PublicActivity::Model
|
168
|
-
# tracked :params => {
|
169
|
-
# :title => :title,
|
170
|
-
# :author_name => "Michael",
|
171
|
-
# :category_name => proc {|controller, model_instance| model_instance.category.name},
|
172
|
-
# :summary => proc {|controller, model_instance| truncate(model.text, :length => 30)}
|
173
|
-
# }
|
174
|
-
# end
|
175
|
-
#
|
176
|
-
# Values in the :params hash can either be an *exact* *value*, a *Proc/Lambda* executed before saving the activity or a *Symbol*
|
177
|
-
# which is a an attribute or a method name executed on the tracked model's instance.
|
178
|
-
#
|
179
|
-
# Everything specified here has a lower priority than parameters
|
180
|
-
# specified directly in {#activity} method.
|
181
|
-
# So treat it as a place where you provide 'default' values or where you
|
182
|
-
# specify what data should be gathered for every activity.
|
183
|
-
# For more dynamic settings refer to {Activity} model documentation.
|
184
|
-
# [:skip_defaults]
|
185
|
-
# Disables recording of activities on create/update/destroy leaving that to programmer's choice. Check {PublicActivity::Common#create_activity}
|
186
|
-
# for a guide on how to manually record activities.
|
187
|
-
# [:only]
|
188
|
-
# Accepts array of symbols, of which correct is any combination of the three:
|
189
|
-
# * _:create_
|
190
|
-
# * _:update_
|
191
|
-
# * _:destroy_
|
192
|
-
# Selecting one or more of these will make PublicActivity create activities
|
193
|
-
# automatically for the tracked model on selected actions.
|
194
|
-
#
|
195
|
-
# Resulting activities will have have keys assigned to, respectively:
|
196
|
-
# * _article.create_
|
197
|
-
# * _article.update_
|
198
|
-
# * _article.destroy_
|
199
|
-
# Since only three options are valid in this array,
|
200
|
-
# see _:except_ option for a shorter version
|
201
|
-
# [:except]
|
202
|
-
# Accepts array of symbols with values like in _:only_, above.
|
203
|
-
# Values provided will be subtracted from all default actions:
|
204
|
-
# (create, update, destroy).
|
205
|
-
#
|
206
|
-
# So, passing _create_ would track and automatically create
|
207
|
-
# activities on _update_ and _destroy_ actions.
|
208
|
-
# [:on]
|
209
|
-
# Accepts a Hash with key being the *action* on which to execute *value* (proc)
|
210
|
-
# Currently supported only for CRUD actions which are enabled in _:only_
|
211
|
-
# or _:except_ options on this method.
|
212
|
-
#
|
213
|
-
# Key-value pairs in this option define callbacks that can decide
|
214
|
-
# whether to create an activity or not. Procs have two attributes for
|
215
|
-
# use: _model_ and _controller_. If the proc returns true, the activity
|
216
|
-
# will be created, if not, then activity will not be saved.
|
217
|
-
#
|
218
|
-
# == Example:
|
219
|
-
# # app/models/article.rb
|
220
|
-
# tracked :on => {:update => proc {|model, controller| model.published? }}
|
221
|
-
#
|
222
|
-
# In the example above, given a model Article with boolean column _published_.
|
223
|
-
# The activities with key _article.update_ will only be created
|
224
|
-
# if the published status is set to true on that article.
|
225
|
-
# @param options [Hash] options
|
226
|
-
# @return [nil] options
|
227
|
-
def tracked(options = {})
|
228
|
-
include Common
|
229
|
-
|
230
|
-
all_options = [:create, :update, :destroy]
|
231
|
-
|
232
|
-
if !options.has_key?(:skip_defaults) && !options[:only] && !options[:except]
|
233
|
-
include Creation
|
234
|
-
include Destruction
|
235
|
-
include Update
|
236
|
-
end
|
237
|
-
|
238
|
-
if options[:except].is_a? Array
|
239
|
-
options[:only] = all_options - options[:except]
|
240
|
-
end
|
241
|
-
|
242
|
-
if options[:only].is_a? Array
|
243
|
-
options[:only].each do |opt|
|
244
|
-
if opt.eql?(:create)
|
245
|
-
include Creation
|
246
|
-
elsif opt.eql?(:destroy)
|
247
|
-
include Destruction
|
248
|
-
elsif opt.eql?(:update)
|
249
|
-
include Update
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
if options[:owner]
|
255
|
-
self.activity_owner_global = options[:owner]
|
256
|
-
end
|
257
|
-
if options[:recipient]
|
258
|
-
self.activity_recipient_global = options[:recipient]
|
259
|
-
end
|
260
|
-
if options[:params]
|
261
|
-
self.activity_params_global = options[:params]
|
262
|
-
end
|
263
|
-
if options.has_key?(:on) and options[:on].is_a? Hash
|
264
|
-
self.activity_hooks = options[:on].delete_if {|_, v| !v.is_a? Proc}.symbolize_keys if RUBY_VERSION == "1.8.7"
|
265
|
-
self.activity_hooks = options[:on].select {|_, v| v.is_a? Proc}.symbolize_keys if RUBY_VERSION != "1.8.7"
|
266
|
-
end
|
267
|
-
has_many :activities, :class_name => "PublicActivity::Activity", :as => :trackable
|
268
|
-
|
269
|
-
nil
|
270
|
-
end
|
271
|
-
|
272
|
-
# Extracts a hook from the _:on_ option provided in
|
273
|
-
# {Tracked::ClassMethods#tracked}. Returns nil when no hook exists for
|
274
|
-
# given action
|
275
|
-
# {Tracked#get_hook}
|
276
|
-
#
|
277
|
-
# @see Tracked#get_hook
|
278
|
-
# @param key [String, Symbol] action to retrieve a hook for
|
279
|
-
# @return [Proc, nil] callable hook or nil
|
280
|
-
# @since 0.4.0
|
281
|
-
# @api private
|
282
|
-
def get_hook(key)
|
283
|
-
key = key.to_sym
|
284
|
-
if self.activity_hooks.has_key?(key) and self.activity_hooks[key].is_a? Proc
|
285
|
-
self.activity_hooks[key]
|
286
|
-
else
|
287
|
-
nil
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
# Switches public_activity off for this class
|
292
|
-
def public_activity_off
|
293
|
-
self.public_activity_enabled_for_model = false
|
294
|
-
end
|
295
|
-
|
296
|
-
# Switches public_activity on for this class
|
297
|
-
def public_activity_on
|
298
|
-
self.public_activity_enabled_for_model = true
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
# Returns true if PublicActivity is enabled
|
303
|
-
# globally and for this class.
|
304
|
-
# @return [Boolean]
|
305
|
-
# @api private
|
306
|
-
# @since 0.5.0
|
307
|
-
def public_activity_enabled?
|
308
|
-
PublicActivity.enabled? && self.class.public_activity_enabled_for_model
|
309
|
-
end
|
310
|
-
|
311
|
-
# Shortcut for {Tracked::ClassMethods#get_hook}
|
312
|
-
# @param (see Tracked::ClassMethods#get_hook)
|
313
|
-
# @return (see Tracked::ClassMethods#get_hook)
|
314
|
-
# @since (see Tracked::ClassMethods#get_hook)
|
315
|
-
# @api (see Tracked::ClassMethods#get_hook)
|
316
|
-
def get_hook(key)
|
317
|
-
self.class.get_hook(key)
|
318
|
-
end
|
319
|
-
|
320
|
-
# Calls hook safely.
|
321
|
-
# If a hook for given action exists, calls it with model (self) and
|
322
|
-
# controller (if available, see {StoreController})
|
323
|
-
# @param key (see #get_hook)
|
324
|
-
# @return [Boolean] if hook exists, it's decision, if there's no hook, true
|
325
|
-
# @since 0.4.0
|
326
|
-
# @api private
|
327
|
-
def call_hook_safe(key)
|
328
|
-
hook = self.get_hook(key)
|
329
|
-
if hook
|
330
|
-
# provides hook with model and controller
|
331
|
-
hook.call(self, PublicActivity.get_controller)
|
332
|
-
else
|
333
|
-
true
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|