public_activity 1.6.4 → 2.0.2

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/test/test_common.rb CHANGED
@@ -4,55 +4,60 @@ require 'test_helper'
4
4
 
5
5
  describe PublicActivity::Common do
6
6
  before do
7
- @owner = User.create(:name => "Peter Pan")
8
- @recipient = User.create(:name => "Bruce Wayne")
9
- @options = {:params => {:author_name => "Peter",
10
- :summary => "Default summary goes here..."},
11
- :owner => @owner, :recipient => @recipient}
7
+ @owner = User.create(name: 'Peter Pan')
8
+ @recipient = User.create(name: 'Bruce Wayne')
9
+ @options = {
10
+ params: {
11
+ author_name: 'Peter',
12
+ summary: 'Default summary goes here...'
13
+ },
14
+ owner: @owner,
15
+ recipient: @recipient
16
+ }
12
17
  end
13
18
  subject { article(@options).new }
14
19
 
15
20
  it 'prioritizes parameters passed to #create_activity' do
16
21
  subject.save
17
- subject.create_activity(:test, params: {author_name: 'Pan'}).parameters[:author_name].must_equal 'Pan'
18
- subject.create_activity(:test, parameters: {author_name: 'Pan'}).parameters[:author_name].must_equal 'Pan'
19
- subject.create_activity(:test, params: {author_name: nil}).parameters[:author_name].must_be_nil
20
- subject.create_activity(:test, parameters: {author_name: nil}).parameters[:author_name].must_be_nil
22
+ assert_equal subject.create_activity(:test, params: { author_name: 'Pan' }).parameters[:author_name], 'Pan'
23
+ assert_equal subject.create_activity(:test, parameters: { author_name: 'Pan' }).parameters[:author_name], 'Pan'
24
+ assert_nil subject.create_activity(:test, params: { author_name: nil }).parameters[:author_name]
25
+ assert_nil subject.create_activity(:test, parameters: { author_name: nil }).parameters[:author_name]
21
26
  end
22
27
 
23
28
  it 'prioritizes owner passed to #create_activity' do
24
29
  subject.save
25
- subject.create_activity(:test, owner: @recipient).owner.must_equal @recipient
26
- subject.create_activity(:test, owner: nil).owner.must_be_nil
30
+ assert_equal subject.create_activity(:test, owner: @recipient).owner, @recipient
31
+ assert_nil subject.create_activity(:test, owner: nil).owner
27
32
  end
28
33
 
29
34
  it 'prioritizes recipient passed to #create_activity' do
30
35
  subject.save
31
- subject.create_activity(:test, recipient: @owner).recipient.must_equal @owner
32
- subject.create_activity(:test, recipient: nil).recipient.must_be_nil
36
+ assert_equal subject.create_activity(:test, recipient: @owner).recipient, @owner
37
+ assert_nil subject.create_activity(:test, recipient: nil).recipient
33
38
  end
34
39
 
35
40
  it 'uses global fields' do
36
41
  subject.save
37
42
  activity = subject.activities.last
38
- activity.parameters.must_equal @options[:params]
39
- activity.owner.must_equal @owner
43
+ assert_equal activity.parameters, @options[:params]
44
+ assert_equal activity.owner, @owner
40
45
  end
41
46
 
42
47
  it 'allows custom fields' do
43
48
  subject.save
44
- subject.create_activity :with_custom_fields, nonstandard: "Custom allowed"
45
- subject.activities.last.nonstandard.must_equal "Custom allowed"
49
+ subject.create_activity :with_custom_fields, nonstandard: 'Custom allowed'
50
+ assert_equal subject.activities.last.nonstandard, 'Custom allowed'
46
51
  end
47
52
 
48
53
  it '#create_activity returns a new activity object' do
49
54
  subject.save
50
- subject.create_activity("some.key").wont_be_nil
55
+ assert subject.create_activity('some.key')
51
56
  end
52
57
 
53
58
  it '#create_activity! returns a new activity object' do
54
59
  subject.save
55
- activity = subject.create_activity!("some.key")
60
+ activity = subject.create_activity!('some.key')
56
61
  assert activity.persisted?
57
62
  assert_equal 'article.some.key', activity.key
58
63
  end
@@ -63,103 +68,102 @@ describe PublicActivity::Common do
63
68
  subject.save
64
69
  subject.save
65
70
  after_count = subject.activities.count
66
- before_count.must_equal after_count
71
+ assert_equal before_count, after_count
67
72
  end
68
73
 
69
-
70
74
  it 'allows passing owner through #create_activity' do
71
75
  article = article().new
72
76
  article.save
73
- activity = article.create_activity("some.key", :owner => @owner)
74
- activity.owner.must_equal @owner
77
+ activity = article.create_activity('some.key', owner: @owner)
78
+ assert_equal activity.owner, @owner
75
79
  end
76
80
 
77
81
  it 'allows resolving custom fields' do
78
- subject.name = "Resolving is great"
82
+ subject.name = 'Resolving is great'
79
83
  subject.published = true
80
84
  subject.save
81
85
  subject.create_activity :with_custom_fields, nonstandard: :name
82
- subject.activities.last.nonstandard.must_equal "Resolving is great"
83
- subject.create_activity :with_custom_fields_2, nonstandard: proc {|_, model| model.published.to_s}
84
- subject.activities.last.nonstandard.must_equal "true"
86
+ assert_equal subject.activities.last.nonstandard, 'Resolving is great'
87
+ subject.create_activity :with_custom_fields_2, nonstandard: proc { |_, model| model.published.to_s }
88
+ assert_equal subject.activities.last.nonstandard, 'true'
85
89
  end
86
90
 
87
91
  it 'inherits instance parameters' do
88
- subject.activity :params => {:author_name => "Michael"}
92
+ subject.activity params: { author_name: 'Michael' }
89
93
  subject.save
90
94
  activity = subject.activities.last
91
95
 
92
- activity.parameters[:author_name].must_equal "Michael"
96
+ assert_equal activity.parameters[:author_name], 'Michael'
93
97
  end
94
98
 
95
99
  it 'accepts instance recipient' do
96
- subject.activity :recipient => @recipient
100
+ subject.activity recipient: @recipient
97
101
  subject.save
98
- subject.activities.last.recipient.must_equal @recipient
102
+ assert_equal subject.activities.last.recipient, @recipient
99
103
  end
100
104
 
101
105
  it 'accepts instance owner' do
102
- subject.activity :owner => @owner
106
+ subject.activity owner: @owner
103
107
  subject.save
104
- subject.activities.last.owner.must_equal @owner
108
+ assert_equal subject.activities.last.owner, @owner
105
109
  end
106
110
 
107
111
  it 'accepts owner as a symbol' do
108
- klass = article(:owner => :user)
109
- @article = klass.new(:user => @owner)
112
+ klass = article(owner: :user)
113
+ @article = klass.new(user: @owner)
110
114
  @article.save
111
115
  activity = @article.activities.last
112
116
 
113
- activity.owner.must_equal @owner
117
+ assert_equal activity.owner, @owner
114
118
  end
115
119
 
116
120
  it 'reports PublicActivity::Activity as the base class' do
117
- if ENV["PA_ORM"] == "active_record" # Only relevant for ActiveRecord
121
+ if ENV['PA_ORM'] == 'active_record' # Only relevant for ActiveRecord
118
122
  subject.save
119
- subject.activities.last.class.base_class.must_equal PublicActivity::Activity
123
+ assert_equal subject.activities.last.class.base_class, PublicActivity::Activity
120
124
  end
121
125
  end
122
126
 
123
127
  describe '#prepare_key' do
124
128
  describe 'for class#activity_key method' do
125
129
  before do
126
- @article = article(:owner => :user).new(:user => @owner)
130
+ @article = article(owner: :user).new(user: @owner)
127
131
  end
128
132
 
129
133
  it 'assigns key to value of activity_key if set' do
130
- def @article.activity_key; "my_custom_key" end
134
+ def @article.activity_key; 'my_custom_key' end
131
135
 
132
- @article.prepare_key(:create, {}).must_equal "my_custom_key"
136
+ assert_equal @article.prepare_key(:create, {}), 'my_custom_key'
133
137
  end
134
138
 
135
139
  it 'assigns key based on class name as fallback' do
136
140
  def @article.activity_key; nil end
137
141
 
138
- @article.prepare_key(:create).must_equal "article.create"
142
+ assert_equal @article.prepare_key(:create), 'article.create'
139
143
  end
140
144
 
141
145
  it 'assigns key value from options hash' do
142
- @article.prepare_key(:create, :key => :my_custom_key).must_equal "my_custom_key"
146
+ assert_equal @article.prepare_key(:create, key: :my_custom_key), 'my_custom_key'
143
147
  end
144
148
  end
145
149
 
146
150
  describe 'for camel cased classes' do
147
151
  before do
148
- class CamelCase < article(:owner => :user)
152
+ class CamelCase < article(owner: :user)
149
153
  def self.name; 'CamelCase' end
150
154
  end
151
155
  @camel_case = CamelCase.new
152
156
  end
153
157
 
154
158
  it 'assigns generates key from class name' do
155
- @camel_case.prepare_key(:create, {}).must_equal "camel_case.create"
159
+ assert_equal @camel_case.prepare_key(:create, {}), 'camel_case.create'
156
160
  end
157
161
  end
158
162
 
159
163
  describe 'for namespaced classes' do
160
164
  before do
161
165
  module ::MyNamespace;
162
- class CamelCase < article(:owner => :user)
166
+ class CamelCase < article(owner: :user)
163
167
  def self.name; 'MyNamespace::CamelCase' end
164
168
  end
165
169
  end
@@ -167,13 +171,15 @@ describe PublicActivity::Common do
167
171
  end
168
172
 
169
173
  it 'assigns key value from options hash' do
170
- @namespaced_camel_case.prepare_key(:create, {}).must_equal "my_namespace_camel_case.create"
174
+ assert_equal @namespaced_camel_case.prepare_key(:create, {}), 'my_namespace_camel_case.create'
171
175
  end
172
176
  end
173
177
  end
174
178
 
175
179
  # no key implicated or given
176
- specify { ->{subject.prepare_settings}.must_raise PublicActivity::NoKeyProvided }
180
+ specify do
181
+ assert_raises(PublicActivity::NoKeyProvided) { subject.prepare_settings }
182
+ end
177
183
 
178
184
  describe 'resolving values' do
179
185
  it 'allows procs with models and controllers' do
@@ -182,7 +188,7 @@ describe PublicActivity::Common do
182
188
  controller = mock('controller')
183
189
  controller.expects(:current_user).returns(:cu)
184
190
  PublicActivity.set_controller(controller)
185
- p = proc {|c, m|
191
+ p = proc { |c, m|
186
192
  assert_equal :cu, c.current_user
187
193
  assert_equal 5, m.accessor
188
194
  }
@@ -191,4 +197,7 @@ describe PublicActivity::Common do
191
197
  end
192
198
  end
193
199
 
200
+ def teardown
201
+ PublicActivity.set_controller(nil)
202
+ end
194
203
  end
@@ -11,31 +11,37 @@ describe PublicActivity::StoreController do
11
11
  it 'stores controller' do
12
12
  controller = StoringController.new
13
13
  PublicActivity.set_controller(controller)
14
- controller.must_be_same_as Thread.current[:public_activity_controller]
15
- controller.must_be_same_as PublicActivity.get_controller
14
+ assert_same controller, Thread.current[:public_activity_controller]
15
+ assert_same controller, PublicActivity.get_controller
16
16
  end
17
17
 
18
18
  it 'stores controller with a filter in controller' do
19
19
  controller = StoringController.new
20
- controller._process_action_callbacks.select {|c| c.kind == :around}.map(&:filter).must_include :store_controller_for_public_activity
21
- controller.instance_eval do
22
- store_controller_for_public_activity do
23
- controller.must_be_same_as PublicActivity.get_controller
20
+
21
+ callbacks = controller._process_action_callbacks.select { |c| c.kind == :around }.map(&:filter)
22
+ assert_includes(callbacks, :store_controller_for_public_activity)
23
+
24
+ public_activity_controller =
25
+ controller.instance_eval do
26
+ store_controller_for_public_activity do
27
+ PublicActivity.get_controller
28
+ end
24
29
  end
25
- end
30
+
31
+ assert_equal controller, public_activity_controller
26
32
  end
27
33
 
28
34
  it 'stores controller in a threadsafe way' do
29
35
  PublicActivity.set_controller(1)
30
- PublicActivity.get_controller.must_equal 1
36
+ assert_equal PublicActivity.get_controller, 1
31
37
 
32
- Thread.new {
38
+ Thread.new do
33
39
  PublicActivity.set_controller(2)
34
40
  assert_equal 2, PublicActivity.get_controller
35
41
  PublicActivity.set_controller(nil)
36
- }
42
+ end
37
43
 
38
- PublicActivity.get_controller.must_equal 1
44
+ assert_equal PublicActivity.get_controller, 1
39
45
 
40
46
  PublicActivity.set_controller(nil)
41
47
  end
@@ -1,43 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if ENV["PA_ORM"] == "active_record"
3
+ if ENV['PA_ORM'] == 'active_record'
4
4
 
5
5
  require 'test_helper'
6
6
  require 'rails/generators/test_case'
7
- require 'generators/public_activity/activity/activity_generator'
8
7
  require 'generators/public_activity/migration/migration_generator'
9
8
  require 'generators/public_activity/migration_upgrade/migration_upgrade_generator'
10
9
 
11
- class TestActivityGenerator < Rails::Generators::TestCase
12
- tests PublicActivity::Generators::ActivityGenerator
13
- destination File.expand_path("../tmp", File.dirname(__FILE__))
14
- setup :prepare_destination
15
-
16
- def test_generating_activity_model
17
- run_generator
18
- assert_file "app/models/activity.rb"
19
- end
20
- end
21
-
22
10
  class TestMigrationGenerator < Rails::Generators::TestCase
23
11
  tests PublicActivity::Generators::MigrationGenerator
24
- destination File.expand_path("../tmp", File.dirname(__FILE__))
12
+ destination File.expand_path('../tmp', File.dirname(__FILE__))
25
13
  setup :prepare_destination
26
14
 
27
15
  def test_generating_activity_model
28
16
  run_generator
29
- assert_migration "db/migrate/create_activities.rb"
17
+ assert_migration 'db/migrate/create_activities.rb'
30
18
  end
31
19
  end
32
20
 
33
21
  class TestMigrationUpgradeGenerator < Rails::Generators::TestCase
34
22
  tests PublicActivity::Generators::MigrationUpgradeGenerator
35
- destination File.expand_path("../tmp", File.dirname(__FILE__))
23
+ destination File.expand_path('../tmp', File.dirname(__FILE__))
36
24
  setup :prepare_destination
37
25
 
38
26
  def test_generating_activity_model
39
27
  run_generator
40
- assert_migration "db/migrate/upgrade_activities.rb"
28
+ assert_migration 'db/migrate/upgrade_activities.rb'
41
29
  end
42
30
  end
43
31
  end
data/test/test_helper.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "rubygems"
4
- require "bundler"
3
+ require 'rubygems'
4
+ require 'bundler'
5
5
  Bundler.setup(:default, :test)
6
6
 
7
7
  if ENV['COV']
8
8
  require 'simplecov'
9
9
  SimpleCov.start do
10
- add_filter "/test/"
10
+ add_filter '/test/'
11
11
  end
12
12
  end
13
- $:.unshift File.expand_path('../../lib/', __FILE__)
13
+ $:.unshift File.expand_path('../lib', __dir__)
14
14
  require 'active_support/testing/setup_and_teardown'
15
15
  require 'public_activity'
16
16
  require 'public_activity/testing'
@@ -26,46 +26,40 @@ when :active_record
26
26
  require 'active_record/connection_adapters/sqlite3_adapter'
27
27
  require 'stringio' # silence the output
28
28
  $stdout = StringIO.new # from migrator
29
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
29
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
30
30
 
31
- migrations_path = File.expand_path('../migrations', __FILE__)
31
+ migrations_path = File.expand_path('migrations', __dir__)
32
+ active_record_version = ActiveRecord.version.release
32
33
 
33
- if ActiveRecord.version.release() < Gem::Version.new('5.2.0')
34
- ActiveRecord::Migrator.migrate(migrations_path)
35
- else
34
+ if active_record_version >= Gem::Version.new('6.0.0')
35
+ schema_path = File.expand_path('../tmp/schema.rb', File.dirname(__FILE__))
36
+ ActiveRecord::MigrationContext.new(migrations_path, ActiveRecord::SchemaMigration).migrate
37
+ elsif active_record_version >= Gem::Version.new('5.2.0')
36
38
  ActiveRecord::MigrationContext.new(migrations_path).migrate
39
+ else # active_record_version < Gem::Version.new('5.2.0')
40
+ ActiveRecord::Migrator.migrate(migrations_path)
37
41
  end
38
42
 
39
43
  $stdout = STDOUT
40
44
 
41
45
  def article(options = {})
42
- klass = Class.new(ActiveRecord::Base) do
46
+ Class.new(ActiveRecord::Base) do
43
47
  self.table_name = 'articles'
44
48
  include PublicActivity::Model
45
49
  tracked options
46
50
  belongs_to :user
47
51
 
48
52
  def self.name
49
- "Article"
50
- end
51
-
52
- if ::ActiveRecord::VERSION::MAJOR < 4
53
- attr_accessible :name, :published, :user
53
+ 'Article'
54
54
  end
55
55
  end
56
- klass
57
56
  end
58
- class User < ActiveRecord::Base; end
59
57
 
60
- if ::ActiveRecord::VERSION::MAJOR < 4
61
- PublicActivity::Activity.class_eval do
62
- attr_accessible :nonstandard
63
- end
64
- end
58
+ class User < ActiveRecord::Base; end
65
59
  when :mongoid
66
60
  require 'mongoid'
67
61
 
68
- Mongoid.load!(File.expand_path("test/mongoid.yml"), :test)
62
+ Mongoid.load!(File.expand_path('test/mongoid.yml'), :test)
69
63
 
70
64
  class User
71
65
  include Mongoid::Document
@@ -102,7 +96,7 @@ when :mongoid
102
96
  when :mongo_mapper
103
97
  require 'mongo_mapper'
104
98
 
105
- config = YAML.load(File.read("test/mongo_mapper.yml"))
99
+ config = YAML.load(File.read('test/mongo_mapper.yml'))
106
100
  MongoMapper.setup(config, :test)
107
101
 
108
102
  class User
data/test/test_testing.rb CHANGED
@@ -1,36 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'test_helper'
4
- describe PublicActivity do
5
4
 
6
- describe "self.with_tracking" do
5
+ describe PublicActivity do
6
+ describe 'self.with_tracking' do
7
7
  after do
8
8
  PublicActivity.enabled = true
9
9
  end
10
-
11
- it "enables tracking inside the block" do
10
+
11
+ it 'enables tracking inside the block' do
12
12
  PublicActivity.enabled = false
13
13
 
14
14
  PublicActivity.with_tracking do
15
- PublicActivity.enabled?.must_equal true
15
+ assert PublicActivity.enabled?
16
16
  end
17
17
  end
18
18
 
19
- it "restores previous `enabled` state" do
19
+ it 'restores previous `enabled` state' do
20
20
  PublicActivity.enabled = false
21
21
  PublicActivity.with_tracking do
22
22
  # something
23
23
  end
24
- PublicActivity.enabled?.must_equal false
24
+
25
+ assert_equal PublicActivity.enabled?, false
25
26
  end
26
27
  end
27
28
 
28
- describe "self.without_tracking" do
29
- it "disables tracking inside the block" do
29
+ describe 'self.without_tracking' do
30
+ it 'disables tracking inside the block' do
30
31
  PublicActivity.enabled = true
31
32
 
32
33
  PublicActivity.without_tracking do
33
- PublicActivity.enabled?.must_equal false
34
+ assert_equal PublicActivity.enabled?, false
34
35
  end
35
36
  end
36
37
  end