public_activity 1.6.4 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -28
- data/Rakefile +4 -5
- data/lib/generators/public_activity/migration/migration_generator.rb +1 -1
- data/lib/generators/public_activity/migration/templates/migration.rb +10 -10
- data/lib/generators/public_activity/migration_upgrade/migration_upgrade_generator.rb +2 -1
- data/lib/generators/public_activity/migration_upgrade/templates/upgrade.rb +2 -2
- data/lib/public_activity/orm/active_record/activity.rb +14 -12
- data/lib/public_activity/renderable.rb +3 -2
- data/lib/public_activity/version.rb +1 -1
- data/test/migrations/001_create_activities.rb +1 -1
- data/test/migrations/002_create_articles.rb +1 -4
- data/test/migrations/003_create_users.rb +1 -3
- data/test/migrations/004_add_nonstandard_to_activities.rb +1 -3
- data/test/test_activist.rb +37 -36
- data/test/test_activity.rb +37 -38
- data/test/test_common.rb +58 -49
- data/test/test_controller_integration.rb +17 -11
- data/test/test_generators.rb +5 -17
- data/test/test_helper.rb +18 -24
- data/test/test_testing.rb +11 -10
- data/test/test_tracking.rb +169 -147
- data/test/test_view_helpers.rb +5 -5
- metadata +49 -40
- data/lib/generators/public_activity/activity/activity_generator.rb +0 -19
- data/lib/generators/public_activity/activity/templates/activity.rb +0 -5
- data/test/migrations_base.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f12beefe37e1f8d3c8fbaada9d0318de0838dd8514c1b32c93d47b97d7af6d57
|
4
|
+
data.tar.gz: bf5e00de61c14049488a4fe549a9bc198367eca931e9d73c0d47dd0ba9c173f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a95d53aac261777b5b52a1a35add35acc0d93d742d4cbb3d2f4deea08ec552ade88b61cbd4447146172953b4ecda464b3ea2539ecb82d58d26c7756cdc5c52b7
|
7
|
+
data.tar.gz: 7dc0ddb8a3622c52b856f1b5a1f45124d300892559800cd62b06115bc14854d0ff84bf33b5a88a50f9f6ae7c060f298f171cb7d082f9bae3aa9696dfdcb36d5a
|
data/README.md
CHANGED
@@ -1,37 +1,35 @@
|
|
1
1
|
# PublicActivity [![Build Status](https://secure.travis-ci.org/chaps-io/public_activity.svg)](http://travis-ci.org/chaps-io/public_activity) [![Code Climate](https://codeclimate.com/github/chaps-io/public_activity.svg)](https://codeclimate.com/github/chaps-io/public_activity) [![Gem Version](https://badge.fury.io/rb/public_activity.svg)](http://badge.fury.io/rb/public_activity)
|
2
2
|
|
3
3
|
`public_activity` provides easy activity tracking for your **ActiveRecord**, **Mongoid 3** and **MongoMapper** models
|
4
|
-
in Rails
|
5
|
-
recorded activities to users -
|
4
|
+
in Rails 5.0+. Simply put: it records what has been changed or created and gives you the ability to present those
|
5
|
+
recorded activities to users - similarly to how GitHub does it.
|
6
6
|
|
7
|
+
## Rails 7
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
## Rails 5
|
14
|
-
|
15
|
-
**As of 1.6.0 version, public_activity also supports Rails up to 5.2.**
|
9
|
+
**As of version 2.0.0, public_activity also supports Rails up to 7.0.**
|
16
10
|
|
17
11
|
## Table of contents
|
18
12
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
13
|
+
- [Rails 7](#rails-7)
|
14
|
+
- [Table of contents](#table-of-contents)
|
15
|
+
- [Example](#example)
|
16
|
+
- [Online demo](#online-demo)
|
17
|
+
- [Screencast](#screencast)
|
18
|
+
- [Setup](#setup)
|
19
|
+
- [Gem installation](#gem-installation)
|
20
|
+
- [Database setup](#database-setup)
|
21
|
+
- [Model configuration](#model-configuration)
|
22
|
+
- [Custom activities](#custom-activities)
|
23
|
+
- [Displaying activities](#displaying-activities)
|
24
|
+
- [Layouts](#layouts)
|
25
|
+
- [Locals](#locals)
|
26
|
+
- [Activity views](#activity-views)
|
27
|
+
- [I18n](#I18n)
|
28
|
+
- [Testing](#testing)
|
29
|
+
- [Documentation](#documentation)
|
30
|
+
- [Common examples](#common-examples)
|
31
|
+
- [Help](#help)
|
32
|
+
- [License](#license)
|
35
33
|
|
36
34
|
## Example
|
37
35
|
|
@@ -197,7 +195,7 @@ For example, if you have an activity with `:key` set to `"activity.user.changed_
|
|
197
195
|
|
198
196
|
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).
|
199
197
|
|
200
|
-
####
|
198
|
+
#### I18n
|
201
199
|
|
202
200
|
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`.
|
203
201
|
|
@@ -224,7 +222,7 @@ require 'public_activity/testing'
|
|
224
222
|
PublicActivity.enabled = false
|
225
223
|
```
|
226
224
|
|
227
|
-
In your specs you can then blockwise decide
|
225
|
+
In your specs you can then blockwise decide whether to turn `public_activity` on
|
228
226
|
or off.
|
229
227
|
|
230
228
|
```ruby
|
data/Rakefile
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'bundler/gem_tasks'
|
4
4
|
require 'rake'
|
5
5
|
require 'yard'
|
6
6
|
require 'yard/rake/yardoc_task'
|
7
7
|
require 'rake/testtask'
|
8
8
|
|
9
|
-
task :
|
9
|
+
task default: :test
|
10
10
|
|
11
11
|
desc 'Generate documentation for the public_activity plugin.'
|
12
12
|
YARD::Rake::YardocTask.new do |doc|
|
@@ -14,7 +14,6 @@ YARD::Rake::YardocTask.new do |doc|
|
|
14
14
|
end
|
15
15
|
|
16
16
|
Rake::TestTask.new do |t|
|
17
|
-
|
18
|
-
|
17
|
+
t.libs << 'test'
|
18
|
+
t.test_files = FileList['test/test*.rb']
|
19
19
|
end
|
20
|
-
|
@@ -9,7 +9,7 @@ module PublicActivity
|
|
9
9
|
class MigrationGenerator < ActiveRecord::Generators::Base
|
10
10
|
extend Base
|
11
11
|
|
12
|
-
argument :name, :
|
12
|
+
argument :name, type: :string, default: 'create_activities'
|
13
13
|
# Create migration in project's folder
|
14
14
|
def generate_files
|
15
15
|
migration_template 'migration.rb', "db/migrate/#{name}.rb"
|
@@ -1,23 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# Migration responsible for creating a table with activities
|
4
|
-
class CreateActivities <
|
5
|
-
# Create table
|
4
|
+
class CreateActivities < ActiveRecord::Migration[5.0]
|
6
5
|
def self.up
|
7
6
|
create_table :activities do |t|
|
8
|
-
t.belongs_to :trackable, :
|
9
|
-
t.belongs_to :owner, :
|
10
|
-
t.string
|
11
|
-
t.text
|
12
|
-
t.belongs_to :recipient, :
|
7
|
+
t.belongs_to :trackable, polymorphic: true
|
8
|
+
t.belongs_to :owner, polymorphic: true
|
9
|
+
t.string :key
|
10
|
+
t.text :parameters
|
11
|
+
t.belongs_to :recipient, polymorphic: true
|
13
12
|
|
14
13
|
t.timestamps
|
15
14
|
end
|
16
15
|
|
17
|
-
add_index :activities, [
|
18
|
-
add_index :activities, [
|
19
|
-
add_index :activities, [
|
16
|
+
add_index :activities, %i[trackable_id trackable_type]
|
17
|
+
add_index :activities, %i[owner_id owner_type]
|
18
|
+
add_index :activities, %i[recipient_id recipient_type]
|
20
19
|
end
|
20
|
+
|
21
21
|
# Drop table
|
22
22
|
def self.down
|
23
23
|
drop_table :activities
|
@@ -9,7 +9,8 @@ module PublicActivity
|
|
9
9
|
class MigrationUpgradeGenerator < ActiveRecord::Generators::Base
|
10
10
|
extend Base
|
11
11
|
|
12
|
-
argument :name, :
|
12
|
+
argument :name, type: :string, default: 'upgrade_activities'
|
13
|
+
|
13
14
|
# Create migration in project's folder
|
14
15
|
def generate_files
|
15
16
|
migration_template 'upgrade.rb', "db/migrate/#{name}.rb"
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# Migration responsible for creating a table with activities
|
4
|
-
class UpgradeActivities < ActiveRecord::Migration
|
4
|
+
class UpgradeActivities < ActiveRecord::Migration[5.0]
|
5
5
|
# Create table
|
6
6
|
def self.change
|
7
7
|
change_table :activities do |t|
|
8
|
-
t.belongs_to :recipient, :
|
8
|
+
t.belongs_to :recipient, polymorphic: true
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -25,20 +25,22 @@ module PublicActivity
|
|
25
25
|
self.abstract_class = true
|
26
26
|
|
27
27
|
# Define polymorphic association to the parent
|
28
|
-
belongs_to :trackable, :
|
28
|
+
belongs_to :trackable, polymorphic: true
|
29
29
|
|
30
30
|
case ::ActiveRecord::VERSION::MAJOR
|
31
|
-
when
|
32
|
-
|
33
|
-
belongs_to :owner, :polymorphic => true
|
34
|
-
# Define ownership to a resource targeted by this activity
|
35
|
-
belongs_to :recipient, :polymorphic => true
|
36
|
-
when 5..6
|
37
|
-
with_options(:required => false) do
|
31
|
+
when 5
|
32
|
+
with_options(required: false) do
|
38
33
|
# Define ownership to a resource responsible for this activity
|
39
|
-
belongs_to :owner, :
|
34
|
+
belongs_to :owner, polymorphic: true
|
40
35
|
# Define ownership to a resource targeted by this activity
|
41
|
-
belongs_to :recipient, :
|
36
|
+
belongs_to :recipient, polymorphic: true
|
37
|
+
end
|
38
|
+
when 6..7
|
39
|
+
with_options(optional: true) do
|
40
|
+
# Define ownership to a resource responsible for this activity
|
41
|
+
belongs_to :owner, polymorphic: true
|
42
|
+
# Define ownership to a resource targeted by this activity
|
43
|
+
belongs_to :recipient, polymorphic: true
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
@@ -49,9 +51,9 @@ module PublicActivity
|
|
49
51
|
else
|
50
52
|
warn("[WARN] table #{name} doesn't exist. Skipping PublicActivity::Activity#parameters's serialization")
|
51
53
|
end
|
52
|
-
rescue ::ActiveRecord::NoDatabaseError
|
54
|
+
rescue ::ActiveRecord::NoDatabaseError
|
53
55
|
warn("[WARN] database doesn't exist. Skipping PublicActivity::Activity#parameters's serialization")
|
54
|
-
rescue ::PG::ConnectionBad
|
56
|
+
rescue ::PG::ConnectionBad
|
55
57
|
warn("[WARN] couldn't connect to database. Skipping PublicActivity::Activity#parameters's serialization")
|
56
58
|
rescue Mysql2::Error::ConnectionError
|
57
59
|
warn("[WARN] couldn't connect to database. Skipping PublicActivity::Activity#parameters's serialization")
|
@@ -12,7 +12,8 @@ module PublicActivity
|
|
12
12
|
k.unshift('activity') if k.first != 'activity'
|
13
13
|
k = k.join('.')
|
14
14
|
|
15
|
-
|
15
|
+
translate_params = parameters.merge(params) || {}
|
16
|
+
I18n.t(k, **translate_params)
|
16
17
|
end
|
17
18
|
|
18
19
|
# Renders activity from views.
|
@@ -142,7 +143,7 @@ module PublicActivity
|
|
142
143
|
def prepare_layout(root, layout)
|
143
144
|
if layout
|
144
145
|
path = layout.to_s
|
145
|
-
unless path.
|
146
|
+
unless path.start_with?(root) || path.start_with?("/")
|
146
147
|
return File.join(root, path)
|
147
148
|
end
|
148
149
|
end
|
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
../../lib/generators/public_activity/migration/templates/migration.rb
|
@@ -1,10 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
class CreateArticles < MigrationsBase
|
3
|
+
class CreateArticles < ActiveRecord::Migration[5.0]
|
6
4
|
def self.up
|
7
|
-
puts "creating"
|
8
5
|
create_table :articles do |t|
|
9
6
|
t.string :name
|
10
7
|
t.boolean :published
|
data/test/test_activist.rb
CHANGED
@@ -5,54 +5,55 @@ require 'test_helper'
|
|
5
5
|
describe PublicActivity::Activist do
|
6
6
|
it 'adds owner association' do
|
7
7
|
klass = article
|
8
|
-
klass
|
8
|
+
assert_respond_to klass, :activist
|
9
9
|
klass.activist
|
10
|
-
klass.new
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
assert_respond_to klass.new, :activities
|
11
|
+
|
12
|
+
case ENV['PA_ORM']
|
13
|
+
when 'active_record'
|
14
|
+
assert_equal klass.reflect_on_association(:activities_as_owner).options[:as], :owner
|
15
|
+
when 'mongoid'
|
16
|
+
assert_equal klass.reflect_on_association(:activities_as_owner).options[:inverse_of], :owner
|
17
|
+
when 'mongo_mapper'
|
18
|
+
assert_equal klass.associations[:activities_as_owner].options[:as], :owner
|
18
19
|
end
|
19
20
|
|
20
|
-
if ENV[
|
21
|
-
klass.associations[:activities_as_owner].options[:class_name]
|
21
|
+
if ENV['PA_ORM'] == 'mongo_mapper'
|
22
|
+
assert_equal klass.associations[:activities_as_owner].options[:class_name], '::PublicActivity::Activity'
|
22
23
|
else
|
23
|
-
klass.reflect_on_association(:activities_as_owner).options[:class_name]
|
24
|
+
assert_equal klass.reflect_on_association(:activities_as_owner).options[:class_name], '::PublicActivity::Activity'
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
27
28
|
it 'returns activities from association' do
|
28
29
|
case PublicActivity::Config.orm
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
30
|
+
when :active_record
|
31
|
+
class ActivistUser < ActiveRecord::Base
|
32
|
+
include PublicActivity::Model
|
33
|
+
self.table_name = 'users'
|
34
|
+
activist
|
35
|
+
end
|
36
|
+
when :mongoid
|
37
|
+
class ActivistUser
|
38
|
+
include Mongoid::Document
|
39
|
+
include PublicActivity::Model
|
40
|
+
activist
|
41
|
+
|
42
|
+
field :name, type: String
|
43
|
+
end
|
44
|
+
when :mongo_mapper
|
45
|
+
class ActivistUser
|
46
|
+
include MongoMapper::Document
|
47
|
+
include PublicActivity::Model
|
48
|
+
activist
|
49
|
+
|
50
|
+
key :name, String
|
51
|
+
end
|
51
52
|
end
|
52
|
-
owner = ActivistUser.create(:
|
53
|
+
owner = ActivistUser.create(name: 'Peter Pan')
|
53
54
|
a = article(owner: owner).new
|
54
55
|
a.save
|
55
56
|
|
56
|
-
owner.activities_as_owner.length
|
57
|
+
assert_equal owner.activities_as_owner.length, 1
|
57
58
|
end
|
58
59
|
end
|
data/test/test_activity.rb
CHANGED
@@ -4,86 +4,85 @@ require 'test_helper'
|
|
4
4
|
|
5
5
|
describe 'PublicActivity::Activity Rendering' do
|
6
6
|
describe '#text' do
|
7
|
-
subject { PublicActivity::Activity.new(:
|
7
|
+
subject { PublicActivity::Activity.new(key: 'activity.test', parameters: { one: 1 }) }
|
8
8
|
|
9
9
|
specify '#text uses translations' do
|
10
10
|
subject.save
|
11
|
-
I18n.config.backend.store_translations(:en,
|
12
|
-
|
13
|
-
|
14
|
-
subject.text(:two => 2).must_equal('1 2')
|
15
|
-
subject.parameters.must_equal({:one => 1})
|
11
|
+
I18n.config.backend.store_translations(:en, activity: { test: '%{one} %{two}' })
|
12
|
+
assert_equal subject.text(two: 2), '1 2'
|
13
|
+
assert_equal subject.parameters, one: 1
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
17
|
describe '#render' do
|
20
18
|
subject do
|
21
|
-
s = PublicActivity::Activity.new(:
|
22
|
-
s.save
|
19
|
+
s = PublicActivity::Activity.new(key: 'activity.test', parameters: { one: 1 })
|
20
|
+
s.save
|
21
|
+
s
|
23
22
|
end
|
24
23
|
|
25
24
|
let(:template_output) { "<strong>1, 2</strong>\n<em>activity.test, #{subject.id}</em>\n" }
|
26
|
-
before { @controller.view_paths << File.expand_path('
|
25
|
+
before { @controller.view_paths << File.expand_path('views', __dir__) }
|
27
26
|
|
28
27
|
it 'uses view partials when available' do
|
29
28
|
PublicActivity.set_controller(Struct.new(:current_user).new('fake'))
|
30
|
-
subject.render(self, :
|
31
|
-
rendered
|
29
|
+
subject.render(self, two: 2)
|
30
|
+
assert_equal rendered, "#{template_output}fake\n"
|
32
31
|
end
|
33
32
|
|
34
33
|
it 'uses requested partial'
|
35
34
|
|
36
35
|
it 'uses view partials without controller' do
|
37
36
|
PublicActivity.set_controller(nil)
|
38
|
-
subject.render(self, :
|
39
|
-
rendered
|
37
|
+
subject.render(self, two: 2)
|
38
|
+
assert_equal rendered, "#{template_output}\n"
|
40
39
|
end
|
41
40
|
|
42
41
|
it 'provides local variables' do
|
43
42
|
PublicActivity.set_controller(nil)
|
44
|
-
subject.render(self, locals: {two: 2})
|
45
|
-
rendered.chomp
|
43
|
+
subject.render(self, locals: { two: 2 })
|
44
|
+
assert_equal rendered.chomp, '2'
|
46
45
|
end
|
47
46
|
|
48
47
|
it 'uses translations only when requested' do
|
49
|
-
I18n.config.backend.store_translations(:en,
|
50
|
-
{:activity => {:test => '%{one} %{two}'}}
|
51
|
-
)
|
48
|
+
I18n.config.backend.store_translations(:en, activity: { test: '%{one} %{two}' })
|
52
49
|
@controller.view_paths.paths.clear
|
53
50
|
subject.render(self, two: 2, display: :i18n)
|
54
|
-
rendered
|
51
|
+
assert_equal rendered, '1 2'
|
55
52
|
end
|
56
53
|
|
57
|
-
it
|
54
|
+
it 'pass all params to view context' do
|
58
55
|
view_context = mock('ViewContext')
|
59
56
|
PublicActivity.set_controller(nil)
|
60
|
-
view_context.expects(:render).with
|
61
|
-
subject.render(view_context, :
|
57
|
+
view_context.expects(:render).with { |params| params[:formats] == ['json'] }
|
58
|
+
subject.render(view_context, formats: ['json'])
|
62
59
|
end
|
63
60
|
|
64
|
-
it
|
61
|
+
it 'uses specified layout' do
|
65
62
|
PublicActivity.set_controller(nil)
|
66
|
-
subject.render(self, :
|
67
|
-
rendered
|
63
|
+
subject.render(self, layout: 'activity')
|
64
|
+
assert_includes rendered, 'Here be the layouts'
|
68
65
|
|
69
|
-
subject.render(self, :
|
70
|
-
rendered
|
66
|
+
subject.render(self, layout: 'layouts/activity')
|
67
|
+
assert_includes rendered, 'Here be the layouts'
|
71
68
|
|
72
|
-
subject.render(self, :
|
73
|
-
rendered
|
69
|
+
subject.render(self, layout: :activity)
|
70
|
+
assert_includes rendered, 'Here be the layouts'
|
74
71
|
end
|
75
72
|
|
76
|
-
it
|
77
|
-
subject.render(self, :
|
78
|
-
rendered
|
73
|
+
it 'accepts a custom layout root' do
|
74
|
+
subject.render(self, layout: :layout, layout_root: 'custom')
|
75
|
+
assert_includes rendered, 'Here be the custom layouts'
|
79
76
|
end
|
80
|
-
|
81
|
-
|
82
|
-
|
77
|
+
|
78
|
+
it 'accepts an absolute layout path' do
|
79
|
+
subject.render(self, layout: '/custom/layout')
|
80
|
+
assert_includes rendered, 'Here be the custom layouts'
|
83
81
|
end
|
84
|
-
|
85
|
-
|
86
|
-
|
82
|
+
|
83
|
+
it 'accepts a template root' do
|
84
|
+
subject.render(self, root: 'custom')
|
85
|
+
assert_includes rendered, 'Custom Template Root'
|
87
86
|
end
|
88
87
|
end
|
89
88
|
end
|