public_activity 1.1.0 → 1.4.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/Gemfile +0 -20
- data/README.md +50 -62
- data/lib/public_activity/activity.rb +2 -0
- data/lib/public_activity/common.rb +9 -7
- data/lib/public_activity/config.rb +15 -0
- data/lib/public_activity/models/activist.rb +2 -0
- data/lib/public_activity/models/adapter.rb +1 -0
- data/lib/public_activity/models/trackable.rb +2 -0
- data/lib/public_activity/orm/active_record/activist.rb +9 -6
- data/lib/public_activity/orm/active_record/activity.rb +4 -3
- data/lib/public_activity/orm/active_record/adapter.rb +4 -0
- data/lib/public_activity/orm/active_record/trackable.rb +4 -0
- data/lib/public_activity/orm/mongo_mapper/activist.rb +46 -0
- data/lib/public_activity/orm/mongo_mapper/activity.rb +33 -0
- data/lib/public_activity/orm/mongo_mapper/adapter.rb +12 -0
- data/lib/public_activity/orm/mongo_mapper/trackable.rb +11 -0
- data/lib/public_activity/orm/mongo_mapper.rb +4 -0
- data/lib/public_activity/orm/mongoid/activity.rb +1 -0
- data/lib/public_activity/renderable.rb +12 -8
- data/lib/public_activity/roles/deactivatable.rb +3 -0
- data/lib/public_activity/roles/tracked.rb +6 -6
- data/lib/public_activity/utility/view_helpers.rb +11 -3
- data/lib/public_activity/version.rb +1 -1
- data/test/migrations/001_create_activities.rb +23 -0
- data/test/migrations/002_create_articles.rb +11 -0
- data/test/migrations/003_create_users.rb +8 -0
- data/test/migrations/004_add_nonstandard_to_activities.rb +7 -0
- data/test/mongo_mapper.yml +4 -0
- data/test/mongoid.yml +6 -0
- data/test/test_activist.rb +56 -0
- data/test/test_activity.rb +67 -0
- data/test/test_common.rb +168 -0
- data/test/test_controller_integration.rb +41 -0
- data/test/test_generators.rb +41 -0
- data/test/test_helper.rb +124 -0
- data/test/test_tracking.rb +378 -0
- data/test/test_view_helpers.rb +36 -0
- data/test/views/layouts/_activity.erb +1 -0
- data/test/views/public_activity/_test.erb +8 -0
- metadata +139 -22
- data/UPGRADING +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86fca5f56a3715cdc7aa50a5429478d3da652e2a
|
4
|
+
data.tar.gz: 6873ddaac083d14044c4e9eb7f4137b257d7bfec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b432bcada38def3820e0a72b49d233e14bd1c87ceb6fdca7263e5b442a1c4d888cf88cc106a3df66febf6b5a6785b1a92a15b148c5410a72b8608596732a540
|
7
|
+
data.tar.gz: 8ace1656f39368a932ff0e5883d710a59a546ce269f25608bfaa9e08e6fde99881681e8eade10461f0d8142e3a72751545016a0a9eda3aae5f3c319332cb731c
|
data/Gemfile
CHANGED
@@ -1,22 +1,2 @@
|
|
1
|
-
ENV['PA_ORM'] ||= 'active_record'
|
2
|
-
|
3
1
|
source "https://rubygems.org"
|
4
|
-
|
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', '< 5.0.0'
|
18
|
-
gem 'redcarpet'
|
19
|
-
gem 'yard', '~> 0.8'
|
20
|
-
end
|
21
|
-
|
22
2
|
gemspec
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# PublicActivity [](http://travis-ci.org/pokonski/public_activity) [](https://gemnasium.com/pokonski/public_activity)
|
2
2
|
|
3
|
-
_public_activity_ provides
|
4
|
-
Simply put: it records what has been changed or created and gives you the ability to present those
|
3
|
+
_public_activity_ provides easy activity tracking for your **ActiveRecord**, **Mongoid 3** and **MongoMapper** models
|
4
|
+
in Rails 3. Simply put: it records what has been changed or created and gives you the ability to present those
|
5
5
|
recorded activities to users - in a similar way to how GitHub does it.
|
6
6
|
|
7
7
|
## Table of contents
|
@@ -9,8 +9,8 @@ recorded activities to users - in a similar way to how GitHub does it.
|
|
9
9
|
1. [Example](#example)
|
10
10
|
* [Demo](#online-demo)
|
11
11
|
* [Screencast](#screencast)
|
12
|
-
|
13
|
-
|
12
|
+
* **[Common examples](#common-examples)**
|
13
|
+
2. [Setup](#setup)
|
14
14
|
1. [Gem installation](#gem-installation)
|
15
15
|
2. [Database setup](#database-setup)
|
16
16
|
3. [Model configuration](#model-configuration)
|
@@ -18,8 +18,9 @@ recorded activities to users - in a similar way to how GitHub does it.
|
|
18
18
|
5. [Displaying activities](#displaying-activities)
|
19
19
|
1. [Activity views](#activity-views)
|
20
20
|
2. [i18n](#i18n)
|
21
|
-
|
22
|
-
|
21
|
+
3. [Documentation](#documentation)
|
22
|
+
4. **[Help](#help)**
|
23
|
+
5. [Upgrading](https://github.com/pokonski/public_activity/wiki/Upgrading-from-pre-0.4.0-versions)
|
23
24
|
|
24
25
|
## Example
|
25
26
|
|
@@ -37,10 +38,6 @@ The source code of the demo is hosted here: https://github.com/pokonski/activity
|
|
37
38
|
|
38
39
|
Ryan Bates made a [great screencast](http://railscasts.com/episodes/406-public-activity) describing how to integrate Public Activity in your Rails Application.
|
39
40
|
|
40
|
-
## Upgrading from pre-0.4.0
|
41
|
-
|
42
|
-
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.
|
43
|
-
|
44
41
|
## Setup
|
45
42
|
|
46
43
|
### Gem installation
|
@@ -57,8 +54,10 @@ gem 'public_activity'
|
|
57
54
|
|
58
55
|
### Database setup
|
59
56
|
|
60
|
-
By default _public_activity_ uses Active Record. If you want to use Mongoid as your backend, create
|
61
|
-
an initializer file in your Rails application with
|
57
|
+
By default _public_activity_ uses Active Record. If you want to use Mongoid or MongoMapper as your backend, create
|
58
|
+
an initializer file in your Rails application with the corresponding code inside:
|
59
|
+
|
60
|
+
For _Mongoid:_
|
62
61
|
|
63
62
|
```ruby
|
64
63
|
# config/initializers/public_activity.rb
|
@@ -67,6 +66,15 @@ PublicActivity::Config.set do
|
|
67
66
|
end
|
68
67
|
```
|
69
68
|
|
69
|
+
For _MongoMapper:_
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
# config/initializers/public_activity.rb
|
73
|
+
PublicActivity::Config.set do
|
74
|
+
orm :mongo_mapper
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
70
78
|
**(ActiveRecord only)** Create migration for activities and migrate the database (in your Rails project):
|
71
79
|
|
72
80
|
rails g public_activity:migration
|
@@ -95,6 +103,16 @@ class Article
|
|
95
103
|
end
|
96
104
|
```
|
97
105
|
|
106
|
+
For _MongoMapper:_
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
class Article
|
110
|
+
include MongoMapper::Document
|
111
|
+
include PublicActivity::Model
|
112
|
+
tracked
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
98
116
|
And now, by default create/update/destroy activities are recorded in activities table.
|
99
117
|
This is all you need to start recording activities for basic CRUD actions.
|
100
118
|
|
@@ -126,13 +144,13 @@ end
|
|
126
144
|
And in your views:
|
127
145
|
|
128
146
|
```erb
|
129
|
-
|
130
|
-
<%= render_activity(activity) %>
|
131
|
-
<% end %>
|
147
|
+
<%= render_activities(@activities) %>
|
132
148
|
```
|
133
149
|
|
134
150
|
*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.
|
135
151
|
|
152
|
+
*Note*: `render_activities` is an alias for `render_activity` and does the same.
|
153
|
+
|
136
154
|
#### Layouts
|
137
155
|
|
138
156
|
You can also pass options to both `activity#render` and `#render_activity` methods, which are passed deeper
|
@@ -141,18 +159,26 @@ A useful example would be to render activities wrapped in layout, which shares c
|
|
141
159
|
like a timestamp, owner's avatar etc:
|
142
160
|
|
143
161
|
```erb
|
144
|
-
|
145
|
-
<%= render_activity(activity, :layout => :activity) %>
|
146
|
-
<% end %>
|
162
|
+
<%= render_activities(@activities, layout: :activity) %>
|
147
163
|
```
|
148
164
|
|
149
165
|
The activity will be wrapped with the `app/views/layouts/_activity.erb` layout, in the above example.
|
150
166
|
|
151
167
|
**Important**: please note that layouts for activities are also partials. Hence the `_` prefix.
|
152
168
|
|
169
|
+
#### Locals
|
170
|
+
|
171
|
+
Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:
|
172
|
+
|
173
|
+
```erb
|
174
|
+
<%= render_activity(@activity, locals: {friends: current_user.friends} %>
|
175
|
+
```
|
176
|
+
|
177
|
+
*Note*: Before 1.4.0, one could pass variables directly to the options hash for `#render_activity` and access it from activity parameters. This functionality is retained in 1.4.0 and later, but the `:locals` method is **preferred**, since it prevents bugs from shadowing variables from activity parameters in the database.
|
178
|
+
|
153
179
|
#### Activity views
|
154
180
|
|
155
|
-
|
181
|
+
`public_activity` looks for views in `app/views/public_activity`.
|
156
182
|
|
157
183
|
For example, if you have an activity with `:key` set to `"activity.user.changed_avatar"`, the gem will look for a partial in `app/views/public_activity/user/_changed_avatar.(erb|haml|slim|something_else)`.
|
158
184
|
|
@@ -162,7 +188,7 @@ If a view file does not exist, then p_a falls back to the old behaviour and trie
|
|
162
188
|
|
163
189
|
#### i18n
|
164
190
|
|
165
|
-
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.
|
191
|
+
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. You can render pure i18n strings by passing `{display: :i18n}` to `#render_activity` or `#render`.
|
166
192
|
|
167
193
|
Translations should be put in your locale `.yml` files. To render pure strings from I18n Example structure:
|
168
194
|
|
@@ -176,54 +202,16 @@ activity:
|
|
176
202
|
|
177
203
|
This structure is valid for activities with keys `"activity.article.create"` or `"article.create"`. As mentioned before, `"activity."` part of the key is optional.
|
178
204
|
|
179
|
-
## Upgrading
|
180
|
-
|
181
|
-
**Read this if you have been using versions older than 0.4.**
|
182
|
-
|
183
|
-
There are a couple of major differences between 0.3 and 0.4 version. To upgrade, follow these steps:
|
184
|
-
|
185
|
-
1. Add `include PublicActivity::Model` above `tracked` method call in your tracked models, like this:
|
186
|
-
|
187
|
-
_ActiveRecord:_
|
188
|
-
|
189
|
-
```ruby
|
190
|
-
class Article < ActiveRecord::Base
|
191
|
-
include PublicActivity::Model
|
192
|
-
tracked
|
193
|
-
end
|
194
|
-
```
|
195
|
-
|
196
|
-
_Mongoid:_
|
197
|
-
|
198
|
-
```ruby
|
199
|
-
class Article
|
200
|
-
include Mongoid::Document
|
201
|
-
include PublicActivity::Model
|
202
|
-
tracked
|
203
|
-
end
|
204
|
-
```
|
205
|
-
|
206
|
-
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.
|
207
|
-
|
208
|
-
<br/>**IMPORTANT**: Locales are no longer rendered with ERB, this has been removed in favor of real view partials like in actual Rails apps.
|
209
|
-
Read [Activity views](#activity-views) section above to learn how to use those templates.<br/>
|
210
|
-
|
211
|
-
3. **(ActiveRecord only)** Generate and run migration which adds new column to `activities` table:
|
212
|
-
|
213
|
-
```bash
|
214
|
-
rails g public_activity:migration_upgrade
|
215
|
-
rake db:migrate
|
216
|
-
```
|
217
|
-
|
218
205
|
## Documentation
|
219
206
|
|
220
|
-
For more
|
207
|
+
For more documentation go [here](http://rubydoc.info/gems/public_activity/index)
|
221
208
|
|
222
|
-
##
|
209
|
+
## Common examples
|
223
210
|
|
224
211
|
* [[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)
|
225
212
|
* [[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)
|
226
213
|
* [[How to] Create custom activities](https://github.com/pokonski/public_activity/wiki/%5BHow-to%5D-Create-custom-activities)
|
214
|
+
* [[How to] Use custom fields on Activity](https://github.com/pokonski/public_activity/wiki/%5BHow-to%5D-Use-custom-fields-on-Activity)
|
227
215
|
|
228
216
|
## Help
|
229
217
|
|
@@ -234,4 +222,4 @@ https://groups.google.com/forum/?fromgroups#!forum/public-activity
|
|
234
222
|
Please do not ask general questions in the Github Issues.
|
235
223
|
|
236
224
|
## License
|
237
|
-
Copyright (c)
|
225
|
+
Copyright (c) 2011-2013 Piotrek Okoński, released under the MIT license
|
@@ -96,7 +96,7 @@ module PublicActivity
|
|
96
96
|
# @return (see #activity_owner)
|
97
97
|
attr_accessor :activity_recipient
|
98
98
|
@activity_recipient = nil
|
99
|
-
# Set or get custom i18n key passed to {Activity}, later used in {
|
99
|
+
# Set or get custom i18n key passed to {Activity}, later used in {Renderable#text}
|
100
100
|
#
|
101
101
|
# == Usage:
|
102
102
|
#
|
@@ -119,6 +119,8 @@ module PublicActivity
|
|
119
119
|
@@activity_hooks = {}
|
120
120
|
|
121
121
|
# @!endgroup
|
122
|
+
|
123
|
+
# Provides some global methods for every model class.
|
122
124
|
module ClassMethods
|
123
125
|
#
|
124
126
|
# @since 1.0.0
|
@@ -134,7 +136,7 @@ module PublicActivity
|
|
134
136
|
# Extracts a hook from the _:on_ option provided in
|
135
137
|
# {Tracked::ClassMethods#tracked}. Returns nil when no hook exists for
|
136
138
|
# given action
|
137
|
-
# {
|
139
|
+
# {Common#get_hook}
|
138
140
|
#
|
139
141
|
# @see Tracked#get_hook
|
140
142
|
# @param key [String, Symbol] action to retrieve a hook for
|
@@ -160,11 +162,11 @@ module PublicActivity
|
|
160
162
|
PublicActivity.enabled?
|
161
163
|
end
|
162
164
|
#
|
163
|
-
# Shortcut for {
|
164
|
-
# @param (see
|
165
|
-
# @return (see
|
166
|
-
# @since (see
|
167
|
-
# @api (see
|
165
|
+
# Shortcut for {ClassMethods#get_hook}
|
166
|
+
# @param (see ClassMethods#get_hook)
|
167
|
+
# @return (see ClassMethods#get_hook)
|
168
|
+
# @since (see ClassMethods#get_hook)
|
169
|
+
# @api (see ClassMethods#get_hook)
|
168
170
|
def get_hook(key)
|
169
171
|
self.class.get_hook(key)
|
170
172
|
end
|
@@ -13,6 +13,12 @@ module PublicActivity
|
|
13
13
|
@enabled = true
|
14
14
|
end
|
15
15
|
|
16
|
+
# Evaluates given block to provide DSL configuration.
|
17
|
+
# @example Initializer for Rails
|
18
|
+
# PublicActivity::Config.set do
|
19
|
+
# orm :mongo_mapper
|
20
|
+
# enabled false
|
21
|
+
# end
|
16
22
|
def self.set &block
|
17
23
|
b = Block.new
|
18
24
|
b.instance_eval &block
|
@@ -23,23 +29,32 @@ module PublicActivity
|
|
23
29
|
instance.instance_variable_set(:@enabled, enabled) unless enabled.nil?
|
24
30
|
end
|
25
31
|
|
32
|
+
# Set the ORM for use by PublicActivity.
|
26
33
|
def self.orm(orm = nil)
|
27
34
|
@@orm = (orm ? orm.to_sym : false) || @@orm
|
28
35
|
end
|
29
36
|
|
37
|
+
# alias for {#orm}
|
38
|
+
# @see #orm
|
30
39
|
def self.orm=(orm = nil)
|
31
40
|
orm(orm)
|
32
41
|
end
|
33
42
|
|
43
|
+
# instance version of {Config#orm}
|
44
|
+
# @see Config#orm
|
34
45
|
def orm(orm=nil)
|
35
46
|
self.class.orm(orm)
|
36
47
|
end
|
37
48
|
|
49
|
+
# Provides simple DSL for the config block.
|
38
50
|
class Block
|
51
|
+
# @see Config#orm
|
39
52
|
def orm(orm = nil)
|
40
53
|
@orm = (orm ? orm.to_sym : false) || @orm
|
41
54
|
end
|
42
55
|
|
56
|
+
# Decides whether to enable PublicActivity.
|
57
|
+
# @param en [Boolean] Enabled?
|
43
58
|
def enabled(en = nil)
|
44
59
|
@en = (en.nil? ? @en : en)
|
45
60
|
end
|
@@ -5,16 +5,12 @@ module PublicActivity
|
|
5
5
|
module Activist
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
+
# Loads the {ClassMethods#activist} method for declaring the class
|
9
|
+
# as an activist.
|
8
10
|
def self.extended(base)
|
9
11
|
base.extend(ClassMethods)
|
10
12
|
end
|
11
|
-
# Association of activities as their owner.
|
12
|
-
# @!method activities
|
13
|
-
# @return [Array<Activity>] Activities which self is the owner of.
|
14
13
|
|
15
|
-
# Association of activities as their recipient.
|
16
|
-
# @!method private_activities
|
17
|
-
# @return [Array<Activity>] Activities which self is the recipient of.
|
18
14
|
|
19
15
|
# Module extending classes that serve as owners
|
20
16
|
module ClassMethods
|
@@ -35,7 +31,14 @@ module PublicActivity
|
|
35
31
|
# User.first.activities
|
36
32
|
#
|
37
33
|
def activist
|
34
|
+
# Association of activities as their owner.
|
35
|
+
# @!method activities_as_owner
|
36
|
+
# @return [Array<Activity>] Activities which self is the owner of.
|
38
37
|
has_many :activities_as_owner, :class_name => "::PublicActivity::Activity", :as => :owner
|
38
|
+
|
39
|
+
# Association of activities as their recipient.
|
40
|
+
# @!method activities_as_recipient
|
41
|
+
# @return [Array<Activity>] Activities which self is the recipient of.
|
39
42
|
has_many :activities_as_recipient, :class_name => "::PublicActivity::Activity", :as => :recipient
|
40
43
|
end
|
41
44
|
end
|
@@ -15,9 +15,10 @@ module PublicActivity
|
|
15
15
|
# Serialize parameters Hash
|
16
16
|
serialize :parameters, Hash
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
if ::ActiveRecord::VERSION::MAJOR < 4
|
19
|
+
attr_accessible :key, :owner, :parameters, :recipient, :trackable
|
20
|
+
end
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|
23
|
-
end
|
24
|
+
end
|
@@ -1,6 +1,10 @@
|
|
1
1
|
module PublicActivity
|
2
2
|
module ORM
|
3
|
+
# Support for ActiveRecord for PublicActivity. Used by default and supported
|
4
|
+
# officialy.
|
3
5
|
module ActiveRecord
|
6
|
+
# Provides ActiveRecord specific, database-related routines for use by
|
7
|
+
# PublicActivity.
|
4
8
|
class Adapter
|
5
9
|
# Creates the activity on `trackable` with `options`
|
6
10
|
def self.create_activity(trackable, options)
|
@@ -1,7 +1,11 @@
|
|
1
1
|
module PublicActivity
|
2
2
|
module ORM
|
3
3
|
module ActiveRecord
|
4
|
+
# Implements {PublicActivity::Trackable} for ActiveRecord
|
5
|
+
# @see PublicActivity::Trackable
|
4
6
|
module Trackable
|
7
|
+
# Creates an association for activities where self is the *trackable*
|
8
|
+
# object.
|
5
9
|
def self.extended(base)
|
6
10
|
base.has_many :activities, :class_name => "::PublicActivity::Activity", :as => :trackable
|
7
11
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module PublicActivity
|
2
|
+
module ORM
|
3
|
+
module MongoMapper
|
4
|
+
# Module extending classes that serve as owners
|
5
|
+
module Activist
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def self.extended(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
# Association of activities as their owner.
|
12
|
+
# @!method activities
|
13
|
+
# @return [Array<Activity>] Activities which self is the owner of.
|
14
|
+
|
15
|
+
# Association of activities as their recipient.
|
16
|
+
# @!method private_activities
|
17
|
+
# @return [Array<Activity>] Activities which self is the recipient of.
|
18
|
+
|
19
|
+
# Module extending classes that serve as owners
|
20
|
+
module ClassMethods
|
21
|
+
# Adds MongoMapper associations to model to simplify fetching
|
22
|
+
# so you can list activities performed by the owner.
|
23
|
+
# It is completely optional. Any model can be an owner to an activity
|
24
|
+
# even without being an explicit activist.
|
25
|
+
#
|
26
|
+
# == Usage:
|
27
|
+
# In model:
|
28
|
+
#
|
29
|
+
# class User
|
30
|
+
# include MongoMapper::Document
|
31
|
+
# include PublicActivity::Model
|
32
|
+
# activist
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# In controller:
|
36
|
+
# User.first.activities
|
37
|
+
#
|
38
|
+
def activist
|
39
|
+
many :activities_as_owner, :class_name => "::PublicActivity::Activity", :as => :owner
|
40
|
+
many :activities_as_recipient, :class_name => "::PublicActivity::Activity", :as => :recipient
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
module PublicActivity
|
5
|
+
module ORM
|
6
|
+
module MongoMapper
|
7
|
+
# The MongoMapper document containing
|
8
|
+
# details about recorded activity.
|
9
|
+
class Activity
|
10
|
+
include ::MongoMapper::Document
|
11
|
+
include Renderable
|
12
|
+
|
13
|
+
class SymbolHash < Hash
|
14
|
+
def self.from_mongo(value)
|
15
|
+
value.symbolize_keys unless value.nil?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Define polymorphic association to the parent
|
20
|
+
belongs_to :trackable, polymorphic: true
|
21
|
+
# Define ownership to a resource responsible for this activity
|
22
|
+
belongs_to :owner, polymorphic: true
|
23
|
+
# Define ownership to a resource targeted by this activity
|
24
|
+
belongs_to :recipient, polymorphic: true
|
25
|
+
|
26
|
+
key :key, String
|
27
|
+
key :parameters, SymbolHash
|
28
|
+
|
29
|
+
timestamps!
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
module PublicActivity
|
2
|
+
# Provides logic for rendering activities. Handles both i18n strings
|
3
|
+
# support and smart partials rendering (different templates per activity key).
|
2
4
|
module Renderable
|
3
5
|
# Virtual attribute returning text description of the activity
|
4
6
|
# using the activity's key to translate using i18n.
|
@@ -80,23 +82,25 @@ module PublicActivity
|
|
80
82
|
return context.render :text => self.text(params) if params[:display].to_sym == :"i18n"
|
81
83
|
partial_path = 'public_activity/'+params[:display].to_s
|
82
84
|
end
|
83
|
-
|
84
|
-
params_indifferent = self.parameters.with_indifferent_access
|
85
|
-
params_indifferent.merge!(params)
|
85
|
+
|
86
86
|
controller = PublicActivity.get_controller
|
87
|
-
layout =
|
88
|
-
if layout
|
87
|
+
if layout = params.delete(:layout)
|
89
88
|
layout = layout.to_s
|
90
89
|
layout = layout[0,8] == "layouts/" ? layout : "layouts/#{layout}"
|
91
90
|
end
|
91
|
+
|
92
|
+
locals = params.delete(:locals) || Hash.new
|
93
|
+
|
94
|
+
params_indifferent = self.parameters.with_indifferent_access
|
95
|
+
params_indifferent.merge!(params)
|
96
|
+
|
92
97
|
context.render :partial => (partial_path || self.template_path(self.key)),
|
93
98
|
:layout => layout,
|
94
|
-
:locals =>
|
95
|
-
{:a => self, :activity => self,
|
99
|
+
:locals => locals.merge(:a => self, :activity => self,
|
96
100
|
:controller => controller,
|
97
101
|
:current_user => controller.respond_to?(:current_user) ?
|
98
102
|
controller.current_user : nil ,
|
99
|
-
:p => params_indifferent, :params => params_indifferent
|
103
|
+
:p => params_indifferent, :params => params_indifferent)
|
100
104
|
end
|
101
105
|
|
102
106
|
protected
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module PublicActivity
|
2
|
+
# Enables per-class disabling of PublicActivity functionality.
|
2
3
|
module Deactivatable
|
3
4
|
extend ActiveSupport::Concern
|
4
5
|
|
@@ -17,6 +18,8 @@ module PublicActivity
|
|
17
18
|
PublicActivity.enabled? && self.class.public_activity_enabled_for_model
|
18
19
|
end
|
19
20
|
|
21
|
+
# Provides global methods to disable or enable PublicActivity on a per-class
|
22
|
+
# basis.
|
20
23
|
module ClassMethods
|
21
24
|
# Switches public_activity off for this class
|
22
25
|
def public_activity_off
|
@@ -9,13 +9,13 @@ module PublicActivity
|
|
9
9
|
# == Options
|
10
10
|
#
|
11
11
|
# [:key]
|
12
|
-
# See {#activity_key}
|
12
|
+
# See {Common#activity_key}
|
13
13
|
# [:owner]
|
14
|
-
# See {#activity_owner}
|
14
|
+
# See {Common#activity_owner}
|
15
15
|
# [:params]
|
16
|
-
# See {#activity_params}
|
16
|
+
# See {Common#activity_params}
|
17
17
|
# [:recipient]
|
18
|
-
# Set the recipient for this activity. Useful for private notifications, which should only be visible to a certain user. See {#activity_recipient}.
|
18
|
+
# Set the recipient for this activity. Useful for private notifications, which should only be visible to a certain user. See {Common#activity_recipient}.
|
19
19
|
# @example
|
20
20
|
#
|
21
21
|
# @article = Article.new
|
@@ -66,7 +66,7 @@ module PublicActivity
|
|
66
66
|
# provide id number of the owner object.
|
67
67
|
# [:params]
|
68
68
|
# Accepts a Hash with custom parameters you want to pass to i18n.translate
|
69
|
-
# method. It is later used in {
|
69
|
+
# method. It is later used in {Renderable#text} method.
|
70
70
|
# == Example:
|
71
71
|
# class Article < ActiveRecord::Base
|
72
72
|
# include PublicActivity::Model
|
@@ -128,7 +128,7 @@ module PublicActivity
|
|
128
128
|
# In the example above, given a model Article with boolean column _published_.
|
129
129
|
# The activities with key _article.update_ will only be created
|
130
130
|
# if the published status is set to true on that article.
|
131
|
-
# @param
|
131
|
+
# @param opts [Hash] options
|
132
132
|
# @return [nil] options
|
133
133
|
def tracked(opts = {})
|
134
134
|
options = opts.clone
|