public_activity 0.5.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/Gemfile
CHANGED
@@ -1,5 +1,22 @@
|
|
1
|
+
ENV['PA_ORM'] ||= 'active_record'
|
2
|
+
|
1
3
|
source :rubygems
|
2
4
|
|
3
|
-
gemspec
|
4
5
|
|
5
|
-
|
6
|
+
case ENV['PA_ORM']
|
7
|
+
when 'active_record'
|
8
|
+
gem 'activerecord', '~> 3.0'
|
9
|
+
when 'mongoid'
|
10
|
+
gem 'mongoid', '~> 3.0'
|
11
|
+
end
|
12
|
+
|
13
|
+
group :development, :test do
|
14
|
+
gem 'sqlite3', '~> 1.3.7' if ENV['PA_ORM'] == 'active_record'
|
15
|
+
gem 'mocha', '~> 0.13.0', require: false
|
16
|
+
gem 'simplecov', '~> 0.7.0'
|
17
|
+
gem 'minitest', '>= 4.3.0'
|
18
|
+
gem 'redcarpet'
|
19
|
+
gem 'yard', '~> 0.8'
|
20
|
+
end
|
21
|
+
|
22
|
+
gemspec
|
data/README.md
CHANGED
@@ -1,7 +1,24 @@
|
|
1
|
-
# PublicActivity [![Build Status](https://secure.travis-ci.org/pokonski/public_activity.png)](http://travis-ci.org/pokonski/public_activity)
|
2
|
-
|
3
|
-
|
4
|
-
Simply put: it records what has been changed or
|
1
|
+
# PublicActivity [![Build Status](https://secure.travis-ci.org/pokonski/public_activity.png)](http://travis-ci.org/pokonski/public_activity) [![Dependency Status](https://gemnasium.com/pokonski/public_activity.png)](https://gemnasium.com/pokonski/public_activity)
|
2
|
+
|
3
|
+
_public_activity_ provides smooth activity tracking for your **ActiveRecord** and **Mongoid 3** models in Rails 3.
|
4
|
+
Simply put: it records what has been changed or created and gives you the ability to present those
|
5
|
+
recorded activities to users - in a similar way to how GitHub does it.
|
6
|
+
|
7
|
+
## Table of contents
|
8
|
+
|
9
|
+
1. [Example](#example)
|
10
|
+
* [Demo](#online-demo)
|
11
|
+
3. **[Upgrading](#upgrading)**
|
12
|
+
4. [Setup](#setup)
|
13
|
+
1. [Gem installation](#gem-installation)
|
14
|
+
2. [Database setup](#database-setup)
|
15
|
+
3. [Model configuration](#model-configuration)
|
16
|
+
4. [Custom activities](#custom-activities)
|
17
|
+
5. [Displaying activities](#displaying-activities)
|
18
|
+
1. [Activity views](#activity-views)
|
19
|
+
2. [i18n](#i18n)
|
20
|
+
5. [Documentation](#documentation)
|
21
|
+
6. **[Help](#help)**
|
5
22
|
|
6
23
|
## Example
|
7
24
|
|
@@ -14,11 +31,12 @@ Here is a simple example showing what this gem is about:
|
|
14
31
|
You can see an actual application using this gem here: http://public-activity-example.herokuapp.com/feed
|
15
32
|
|
16
33
|
The source code of the demo is hosted here: https://github.com/pokonski/activity_blog
|
17
|
-
|
34
|
+
|
35
|
+
## Upgrading from pre-0.4.0
|
18
36
|
|
19
37
|
If you are using versions earlier than 0.4.0 please click [here](#upgrading) or scroll to the "Upgrading" section at the bottom of this README.
|
20
38
|
|
21
|
-
##
|
39
|
+
## Setup
|
22
40
|
|
23
41
|
### Gem installation
|
24
42
|
|
@@ -34,7 +52,17 @@ gem 'public_activity'
|
|
34
52
|
|
35
53
|
### Database setup
|
36
54
|
|
37
|
-
|
55
|
+
By default _public_activity_ uses Active Record. If you want to use Mongoid as your backend, create
|
56
|
+
an initializer file in your Rails application with this code inside:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# config/initializers/public_activity.rb
|
60
|
+
PublicActivity::Config.set do
|
61
|
+
orm :mongoid
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
**(ActiveRecord only)** Create migration for activities and migrate the database (in your Rails project):
|
38
66
|
|
39
67
|
rails g public_activity:migration
|
40
68
|
rake db:migrate
|
@@ -43,6 +71,8 @@ Create migration for activities and migrate the database (in your Rails project)
|
|
43
71
|
|
44
72
|
Include `PublicActivity::Model` and add `tracked` to the model you want to keep track of:
|
45
73
|
|
74
|
+
For _ActiveRecord:_
|
75
|
+
|
46
76
|
```ruby
|
47
77
|
class Article < ActiveRecord::Base
|
48
78
|
include PublicActivity::Model
|
@@ -50,11 +80,36 @@ class Article < ActiveRecord::Base
|
|
50
80
|
end
|
51
81
|
```
|
52
82
|
|
53
|
-
|
83
|
+
For _Mongoid:_
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class Article
|
87
|
+
include Mongoid::Document
|
88
|
+
include PublicActivity::Model
|
89
|
+
tracked
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
And now, by default create/update/destroy activities are recorded in activities table.
|
94
|
+
This is all you need to start recording activities for basic CRUD actions.
|
95
|
+
|
96
|
+
_Optional_: If you don't need `#tracked` but still want the comfort of `#create_activity`,
|
97
|
+
you can include only the lightweight `Common` module instead of `Model`.
|
98
|
+
|
99
|
+
#### Custom activities
|
100
|
+
|
101
|
+
You can trigger custom activities by setting all your required parameters and triggering `create_activity`
|
102
|
+
on the tracked model, like this:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
@article.create_activity key: 'article.commented_on', owner: current_user
|
106
|
+
```
|
107
|
+
|
108
|
+
See this entry http://rubydoc.info/gems/public_activity/PublicActivity/Common:create_activity for more details.
|
54
109
|
|
55
110
|
### Displaying activities
|
56
111
|
|
57
|
-
To display them you simply query the `PublicActivity::Activity`
|
112
|
+
To display them you simply query the `PublicActivity::Activity` model:
|
58
113
|
|
59
114
|
```ruby
|
60
115
|
# notifications_controller.rb
|
@@ -66,25 +121,31 @@ end
|
|
66
121
|
And in your views:
|
67
122
|
|
68
123
|
```erb
|
69
|
-
<%
|
124
|
+
<% @activities.each do |activity| %>
|
70
125
|
<%= render_activity(activity) %>
|
71
126
|
<% end %>
|
72
127
|
```
|
73
128
|
|
74
129
|
*Note*: `render_activity` is a helper for use in view templates. `render_activity(activity)` can be written as `activity.render(self)` and it will have the same meaning.
|
75
130
|
|
76
|
-
|
77
|
-
|
131
|
+
#### Layouts
|
132
|
+
|
133
|
+
You can also pass options to both `activity#render` and `#render_activity` methods, which are passed deeper
|
134
|
+
to the internally used `render_partial` method.
|
135
|
+
A useful example would be to render activities wrapped in layout, which shares common elements of an activity,
|
136
|
+
like a timestamp, owner's avatar etc:
|
78
137
|
|
79
138
|
```erb
|
80
|
-
<%
|
139
|
+
<% @activities.each do |activity| %>
|
81
140
|
<%= render_activity(activity, :layout => :activity) %>
|
82
141
|
<% end %>
|
83
142
|
```
|
84
143
|
|
85
|
-
The activity will be wrapped with the `app/views/layouts/
|
144
|
+
The activity will be wrapped with the `app/views/layouts/_activity.erb` layout, in the above example.
|
145
|
+
|
146
|
+
**Important**: please note that layouts for activities are also partials. Hence the `_` prefix.
|
86
147
|
|
87
|
-
|
148
|
+
#### Activity views
|
88
149
|
|
89
150
|
Since version `0.4.0` you can use views to render activities. `public_activity` looks for views in `app/views/public_activity`, and this is now the *default* behaviour.
|
90
151
|
|
@@ -94,7 +155,7 @@ For example, if you have an activity with `:key` set to `"activity.user.changed_
|
|
94
155
|
|
95
156
|
If a view file does not exist, then p_a falls back to the old behaviour and tries to translate the activity `:key` using `I18n#translate` method (see the section below).
|
96
157
|
|
97
|
-
|
158
|
+
#### i18n
|
98
159
|
|
99
160
|
Translations are used by the `#text` method, to which you can pass additional options in form of a hash. `#render` method uses translations when view templates have not been provided.
|
100
161
|
|
@@ -112,10 +173,14 @@ This structure is valid for activities with keys `"activity.article.create"` or
|
|
112
173
|
|
113
174
|
## Upgrading
|
114
175
|
|
176
|
+
**Read this if you have been using versions older than 0.4.**
|
177
|
+
|
115
178
|
There are a couple of major differences between 0.3 and 0.4 version. To upgrade, follow these steps:
|
116
179
|
|
117
180
|
1. Add `include PublicActivity::Model` above `tracked` method call in your tracked models, like this:
|
118
181
|
|
182
|
+
_ActiveRecord:_
|
183
|
+
|
119
184
|
```ruby
|
120
185
|
class Article < ActiveRecord::Base
|
121
186
|
include PublicActivity::Model
|
@@ -123,12 +188,22 @@ There are a couple of major differences between 0.3 and 0.4 version. To upgrade,
|
|
123
188
|
end
|
124
189
|
```
|
125
190
|
|
126
|
-
|
191
|
+
_Mongoid:_
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
class Article
|
195
|
+
include Mongoid::Document
|
196
|
+
include PublicActivity::Model
|
197
|
+
tracked
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
2. _public_activity_'s config YAML file is no longer used (by default in `config/pba.yml`). Move your YAML contents to your `config/locales/*.yml` files.
|
127
202
|
|
128
203
|
<br/>**IMPORTANT**: Locales are no longer rendered with ERB, this has been removed in favor of real view partials like in actual Rails apps.
|
129
204
|
Read [Activity views](#activity-views) section above to learn how to use those templates.<br/>
|
130
205
|
|
131
|
-
3. Generate and run migration which adds new column to `activities` table:
|
206
|
+
3. **(ActiveRecord only)** Generate and run migration which adds new column to `activities` table:
|
132
207
|
|
133
208
|
```bash
|
134
209
|
rails g public_activity:migration_upgrade
|
@@ -143,6 +218,15 @@ For more customization go [here](http://rubydoc.info/gems/public_activity/index)
|
|
143
218
|
|
144
219
|
* [[How to] Set the Activity's owner to current_user by default](https://github.com/pokonski/public_activity/wiki/%5BHow-to%5D-Set-the-Activity's-owner-to-current_user-by-default)
|
145
220
|
* [[How to] Disable tracking for a class or globally](https://github.com/pokonski/public_activity/wiki/%5BHow-to%5D-Disable-tracking-for-a-class-or-globally)
|
221
|
+
* [[How to] Create custom activities](https://github.com/pokonski/public_activity/wiki/%5BHow-to%5D-Create-custom-activities)
|
222
|
+
|
223
|
+
## Help
|
224
|
+
|
225
|
+
If you need help with using public_activity please visit our discussion group and ask a question there:
|
226
|
+
|
227
|
+
https://groups.google.com/forum/?fromgroups#!forum/public-activity
|
228
|
+
|
229
|
+
Please do not ask general questions in the Github Issues.
|
146
230
|
|
147
231
|
## License
|
148
|
-
Copyright (c) 2012 Piotrek Okoński, released under the MIT license
|
232
|
+
Copyright (c) 2012 Piotrek Okoński, released under the MIT license
|
data/UPGRADING
CHANGED
@@ -1,17 +1,8 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
|
4
|
-
|
5
|
-
The 0.5 version of public_activity will be the
|
6
|
-
last to support versions older than 1.9.2. Please
|
7
|
-
update your Ruby if you wish to continue using
|
8
|
-
this gem.
|
9
|
-
|
10
|
-
--------------------------------------------------
|
11
|
-
| NOTE FOR UPGRADING FROM PRE-0.4.0 VERSION |
|
12
|
-
--------------------------------------------------
|
1
|
+
##################################################
|
2
|
+
# NOTE FOR UPGRADING FROM PRE-0.4.0 VERSION #
|
3
|
+
##################################################
|
13
4
|
|
14
5
|
public_activity 0.4.0 brings major changes compared to 0.3.X versions,
|
15
|
-
please read https://github.com/pokonski/public_activity#upgrading
|
6
|
+
please read https://github.com/pokonski/public_activity#upgrading
|
16
7
|
to learn about all the changes you need to apply to properly
|
17
8
|
upgrade your applications.
|
@@ -11,6 +11,10 @@ class CreateActivities < ActiveRecord::Migration
|
|
11
11
|
|
12
12
|
t.timestamps
|
13
13
|
end
|
14
|
+
|
15
|
+
add_index :activities, [:trackable_id, :trackable_type]
|
16
|
+
add_index :activities, [:owner_id, :owner_type]
|
17
|
+
add_index :activities, [:recipient_id, :recipient_type]
|
14
18
|
end
|
15
19
|
# Drop table
|
16
20
|
def self.down
|
data/lib/public_activity.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'action_view'
|
3
|
-
require 'active_record'
|
4
|
-
|
5
3
|
# +public_activity+ keeps track of changes made to models
|
6
4
|
# and allows you to display them to the users.
|
7
5
|
#
|
@@ -10,15 +8,15 @@ require 'active_record'
|
|
10
8
|
module PublicActivity
|
11
9
|
extend ActiveSupport::Concern
|
12
10
|
extend ActiveSupport::Autoload
|
13
|
-
autoload :Activist
|
14
|
-
autoload :Activity
|
15
11
|
autoload :Config
|
16
|
-
autoload :Tracked
|
17
|
-
autoload :
|
18
|
-
autoload :
|
19
|
-
autoload :
|
12
|
+
autoload :Tracked, 'public_activity/roles/tracked.rb'
|
13
|
+
autoload :Deactivatable,'public_activity/roles/deactivatable.rb'
|
14
|
+
autoload :Creation, 'public_activity/actions/creation.rb'
|
15
|
+
autoload :Update, 'public_activity/actions/update.rb'
|
16
|
+
autoload :Destruction, 'public_activity/actions/destruction.rb'
|
20
17
|
autoload :VERSION
|
21
18
|
autoload :Common
|
19
|
+
autoload :Renderable
|
22
20
|
|
23
21
|
# Switches PublicActivity on or off.
|
24
22
|
# @param value [Boolean]
|
@@ -45,11 +43,17 @@ module PublicActivity
|
|
45
43
|
module Model
|
46
44
|
extend ActiveSupport::Concern
|
47
45
|
included do
|
46
|
+
include Common
|
47
|
+
include Deactivatable
|
48
48
|
include Tracked
|
49
|
-
include Activist
|
49
|
+
include Activist # optional associations by recipient|owner
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
|
55
|
-
|
54
|
+
# Force Active Record ORM to load
|
55
|
+
# makes initializer optional for default config
|
56
|
+
PublicActivity.config if defined? ActiveRecord
|
57
|
+
|
58
|
+
require 'public_activity/utility/store_controller'
|
59
|
+
require 'public_activity/utility/view_helpers'
|
File without changes
|
File without changes
|
File without changes
|
@@ -20,6 +20,174 @@ module PublicActivity
|
|
20
20
|
# Common methods shared across the gem.
|
21
21
|
module Common
|
22
22
|
extend ActiveSupport::Concern
|
23
|
+
|
24
|
+
included do
|
25
|
+
::PublicActivity.config # it's the first module to be loaded into models
|
26
|
+
# we need to have pieces provided by orm loaded
|
27
|
+
include Trackable
|
28
|
+
class_attribute :activity_owner_global, :activity_recipient_global,
|
29
|
+
:activity_params_global, :activity_hooks, :activity_custom_fields_global
|
30
|
+
set_public_activity_class_defaults
|
31
|
+
end
|
32
|
+
|
33
|
+
# @!group Global options
|
34
|
+
|
35
|
+
# @!attribute activity_owner_global
|
36
|
+
# Global version of activity owner
|
37
|
+
# @see #activity_owner
|
38
|
+
# @return [Model]
|
39
|
+
|
40
|
+
# @!attribute activity_recipient_global
|
41
|
+
# Global version of activity recipient
|
42
|
+
# @see #activity_recipient
|
43
|
+
# @return [Model]
|
44
|
+
|
45
|
+
# @!attribute activity_params_global
|
46
|
+
# Global version of activity parameters
|
47
|
+
# @see #activity_params
|
48
|
+
# @return [Hash<Symbol, Object>]
|
49
|
+
|
50
|
+
# @!attribute activity_hooks
|
51
|
+
# @return [Hash<Symbol, Proc>]
|
52
|
+
# Hooks/functions that will be used to decide *if* the activity should get
|
53
|
+
# created.
|
54
|
+
#
|
55
|
+
# The supported keys are:
|
56
|
+
# * :create
|
57
|
+
# * :update
|
58
|
+
# * :destroy
|
59
|
+
|
60
|
+
# @!endgroup
|
61
|
+
|
62
|
+
# @!group Instance options
|
63
|
+
|
64
|
+
# Set or get parameters that will be passed to {Activity} when saving
|
65
|
+
#
|
66
|
+
# == Usage:
|
67
|
+
#
|
68
|
+
# @article.activity_params = {:article_title => @article.title}
|
69
|
+
# @article.save
|
70
|
+
#
|
71
|
+
# This way you can pass strings that should remain constant, even when model attributes
|
72
|
+
# change after creating this {Activity}.
|
73
|
+
# @return [Hash<Symbol, Object>]
|
74
|
+
attr_accessor :activity_params
|
75
|
+
@activity_params = {}
|
76
|
+
# Set or get owner object responsible for the {Activity}.
|
77
|
+
#
|
78
|
+
# == Usage:
|
79
|
+
#
|
80
|
+
# # where current_user is an object of logged in user
|
81
|
+
# @article.activity_owner = current_user
|
82
|
+
# # OR: take @article.author association
|
83
|
+
# @article.activity_owner = :author
|
84
|
+
# # OR: provide a Proc with custom code
|
85
|
+
# @article.activity_owner = proc {|controller, model| model.author }
|
86
|
+
# @article.save
|
87
|
+
# @article.activities.last.owner #=> Returns owner object
|
88
|
+
# @return [Model] Polymorphic model
|
89
|
+
# @see #activity_owner_global
|
90
|
+
attr_accessor :activity_owner
|
91
|
+
@activity_owner = nil
|
92
|
+
|
93
|
+
# Set or get recipient for activity.
|
94
|
+
#
|
95
|
+
# Association is polymorphic, thus allowing assignment of
|
96
|
+
# all types of models. This can be used for example in the case of sending
|
97
|
+
# private notifications for only a single user.
|
98
|
+
# @return (see #activity_owner)
|
99
|
+
attr_accessor :activity_recipient
|
100
|
+
@activity_recipient = nil
|
101
|
+
# Set or get custom i18n key passed to {Activity}, later used in {Activity#text}
|
102
|
+
#
|
103
|
+
# == Usage:
|
104
|
+
#
|
105
|
+
# @article = Article.new
|
106
|
+
# @article.activity_key = "my.custom.article.key"
|
107
|
+
# @article.save
|
108
|
+
# @article.activities.last.key #=> "my.custom.article.key"
|
109
|
+
#
|
110
|
+
# @return [String]
|
111
|
+
attr_accessor :activity_key
|
112
|
+
@activity_key = nil
|
113
|
+
|
114
|
+
# Set or get custom fields for later processing
|
115
|
+
#
|
116
|
+
# @return [Hash]
|
117
|
+
attr_accessor :activity_custom_fields
|
118
|
+
@activity_custom_fields = {}
|
119
|
+
|
120
|
+
# @!visibility private
|
121
|
+
@@activity_hooks = {}
|
122
|
+
|
123
|
+
# @!endgroup
|
124
|
+
module ClassMethods
|
125
|
+
#
|
126
|
+
# @since 1.0.0
|
127
|
+
# @api private
|
128
|
+
def set_public_activity_class_defaults
|
129
|
+
self.activity_owner_global = nil
|
130
|
+
self.activity_recipient_global = nil
|
131
|
+
self.activity_params_global = {}
|
132
|
+
self.activity_hooks = {}
|
133
|
+
self.activity_custom_fields_global = {}
|
134
|
+
end
|
135
|
+
|
136
|
+
# Extracts a hook from the _:on_ option provided in
|
137
|
+
# {Tracked::ClassMethods#tracked}. Returns nil when no hook exists for
|
138
|
+
# given action
|
139
|
+
# {Tracked#get_hook}
|
140
|
+
#
|
141
|
+
# @see Tracked#get_hook
|
142
|
+
# @param key [String, Symbol] action to retrieve a hook for
|
143
|
+
# @return [Proc, nil] callable hook or nil
|
144
|
+
# @since 0.4.0
|
145
|
+
# @api private
|
146
|
+
def get_hook(key)
|
147
|
+
key = key.to_sym
|
148
|
+
if self.activity_hooks.has_key?(key) and self.activity_hooks[key].is_a? Proc
|
149
|
+
self.activity_hooks[key]
|
150
|
+
else
|
151
|
+
nil
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
#
|
156
|
+
# Returns true if PublicActivity is enabled
|
157
|
+
# globally and for this class.
|
158
|
+
# @return [Boolean]
|
159
|
+
# @api private
|
160
|
+
# @since 0.5.0
|
161
|
+
def public_activity_enabled?
|
162
|
+
PublicActivity.enabled?
|
163
|
+
end
|
164
|
+
#
|
165
|
+
# Shortcut for {Tracked::ClassMethods#get_hook}
|
166
|
+
# @param (see Tracked::ClassMethods#get_hook)
|
167
|
+
# @return (see Tracked::ClassMethods#get_hook)
|
168
|
+
# @since (see Tracked::ClassMethods#get_hook)
|
169
|
+
# @api (see Tracked::ClassMethods#get_hook)
|
170
|
+
def get_hook(key)
|
171
|
+
self.class.get_hook(key)
|
172
|
+
end
|
173
|
+
|
174
|
+
# Calls hook safely.
|
175
|
+
# If a hook for given action exists, calls it with model (self) and
|
176
|
+
# controller (if available, see {StoreController})
|
177
|
+
# @param key (see #get_hook)
|
178
|
+
# @return [Boolean] if hook exists, it's decision, if there's no hook, true
|
179
|
+
# @since 0.4.0
|
180
|
+
# @api private
|
181
|
+
def call_hook_safe(key)
|
182
|
+
hook = self.get_hook(key)
|
183
|
+
if hook
|
184
|
+
# provides hook with model and controller
|
185
|
+
hook.call(self, PublicActivity.get_controller)
|
186
|
+
else
|
187
|
+
true
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
23
191
|
# Directly creates activity record in the database, based on supplied options.
|
24
192
|
#
|
25
193
|
# It's meant for creating custom activities while *preserving* *all*
|
@@ -82,14 +250,11 @@ module PublicActivity
|
|
82
250
|
options = prepare_settings(*args)
|
83
251
|
|
84
252
|
if call_hook_safe(options[:key].split('.').last)
|
85
|
-
self.activities.create(
|
86
|
-
:key => options[:key],
|
87
|
-
:owner => options[:owner],
|
88
|
-
:recipient => options[:recipient],
|
89
|
-
:parameters => options[:params]
|
90
|
-
)
|
91
253
|
reset_activity_instance_options
|
254
|
+
PublicActivity::Adapter.create_activity(self, options)
|
92
255
|
end
|
256
|
+
|
257
|
+
return nil
|
93
258
|
end
|
94
259
|
|
95
260
|
# Prepares settings used during creation of Activity record.
|
@@ -105,39 +270,50 @@ module PublicActivity
|
|
105
270
|
# @see #create_activity
|
106
271
|
def prepare_settings(*args)
|
107
272
|
# key
|
108
|
-
|
273
|
+
all_options = args.extract_options!
|
274
|
+
options = {
|
275
|
+
key: all_options.delete(:key),
|
276
|
+
owner: all_options.delete(:key),
|
277
|
+
action: all_options.delete(:action),
|
278
|
+
recipient: all_options.delete(:recipient),
|
279
|
+
parameters: all_options.delete(:parameters) || all_options.delete(:params)
|
280
|
+
}
|
109
281
|
action = (args.first || options[:action]).try(:to_s)
|
110
282
|
|
111
|
-
key = extract_key(action, options)
|
283
|
+
options[:key] = extract_key(action, options)
|
112
284
|
|
113
|
-
raise NoKeyProvided, "No key provided for #{self.class.name}" unless key
|
285
|
+
raise NoKeyProvided, "No key provided for #{self.class.name}" unless options[:key]
|
286
|
+
|
287
|
+
options.delete(:action)
|
114
288
|
|
115
289
|
# user responsible for the activity
|
116
|
-
owner = PublicActivity.resolve_value(self,
|
290
|
+
options[:owner] = PublicActivity.resolve_value(self,
|
117
291
|
options[:owner] ||
|
118
292
|
self.activity_owner ||
|
119
293
|
self.class.activity_owner_global
|
120
294
|
)
|
121
295
|
|
122
296
|
# recipient of the activity
|
123
|
-
recipient = PublicActivity.resolve_value(self,
|
297
|
+
options[:recipient] = PublicActivity.resolve_value(self,
|
124
298
|
options[:recipient] ||
|
125
299
|
self.activity_recipient ||
|
126
300
|
self.class.activity_recipient_global
|
127
301
|
)
|
128
302
|
|
129
303
|
#customizable parameters
|
130
|
-
params = options[:params] || {}
|
304
|
+
params = options[:params] || options[:parameters] || {}
|
131
305
|
params.merge!(self.class.activity_params_global)
|
132
306
|
params.merge!(self.activity_params) if self.activity_params
|
133
307
|
params.each { |k, v| params[k] = PublicActivity.resolve_value(self, v) }
|
308
|
+
options[:parameters] = params
|
309
|
+
options.delete(:params)
|
134
310
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
311
|
+
customs = self.class.activity_custom_fields_global
|
312
|
+
customs.merge!(self.activity_custom_fields) if self.activity_custom_fields
|
313
|
+
customs.merge!(all_options)
|
314
|
+
customs.each do |k, v|
|
315
|
+
customs[k] = PublicActivity.resolve_value(self, v)
|
316
|
+
end.merge options
|
141
317
|
end
|
142
318
|
|
143
319
|
# Helper method to serialize class name into relevant key
|
@@ -159,6 +335,7 @@ module PublicActivity
|
|
159
335
|
@activity_key = nil
|
160
336
|
@activity_owner = nil
|
161
337
|
@activity_recipient = nil
|
338
|
+
@activity_custom_fields = {}
|
162
339
|
end
|
163
340
|
end
|
164
341
|
end
|