activity_notification 0.0.8 → 0.0.9
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.lock +11 -1
- data/README.md +63 -28
- data/activity_notification.gemspec +4 -2
- data/app/controllers/activity_notification/notifications_controller.rb +1 -1
- data/app/controllers/activity_notification/notifications_with_devise_controller.rb +10 -10
- data/app/views/activity_notification/notifications/default/_index.html.erb +9 -4
- data/lib/activity_notification.rb +7 -6
- data/lib/activity_notification/apis/notification_api.rb +14 -15
- data/lib/activity_notification/common.rb +15 -7
- data/lib/activity_notification/config.rb +2 -0
- data/lib/activity_notification/helpers/view_helpers.rb +5 -4
- data/lib/activity_notification/mailers/helpers.rb +9 -9
- data/lib/activity_notification/models.rb +16 -0
- data/lib/activity_notification/models/{notifiable.rb → concerns/notifiable.rb} +15 -10
- data/lib/activity_notification/models/{notifier.rb → concerns/notifier.rb} +6 -0
- data/lib/activity_notification/models/{target.rb → concerns/target.rb} +34 -17
- data/lib/activity_notification/renderable.rb +2 -1
- data/lib/activity_notification/roles/acts_as_notifiable.rb +38 -23
- data/lib/activity_notification/roles/acts_as_notifier.rb +11 -0
- data/lib/activity_notification/roles/acts_as_target.rb +9 -18
- data/lib/activity_notification/version.rb +1 -1
- data/lib/generators/activity_notification/{migration → active_record}/migration_generator.rb +5 -4
- data/lib/generators/activity_notification/controllers_generator.rb +1 -1
- data/lib/generators/activity_notification/install_generator.rb +3 -6
- data/lib/generators/activity_notification/{notification → models}/notification_generator.rb +5 -4
- data/lib/generators/activity_notification/views_generator.rb +20 -22
- data/lib/generators/templates/active_record/migration.rb +1 -1
- data/lib/generators/templates/activity_notification.rb +13 -3
- data/{config → lib/generators/templates}/locales/en.yml +0 -0
- data/lib/generators/templates/notification/notification.rb +4 -1
- data/spec/concerns/{notification_api_spec.rb → apis/notification_api_spec.rb} +169 -45
- data/spec/concerns/common_spec.rb +150 -0
- data/spec/concerns/models/notifiable_spec.rb +435 -0
- data/spec/concerns/models/notifier_spec.rb +23 -0
- data/spec/concerns/models/target_spec.rb +579 -0
- data/spec/concerns/renderable_spec.rb +110 -0
- data/spec/controllers/notifications_controller_shared_examples.rb +457 -0
- data/spec/controllers/notifications_controller_spec.rb +12 -0
- data/spec/controllers/notifications_with_devise_controller_spec.rb +81 -0
- data/spec/factories/admins.rb +5 -0
- data/spec/factories/articles.rb +1 -1
- data/spec/factories/comments.rb +1 -1
- data/spec/factories/dummy/dummy_notifiable.rb +4 -0
- data/spec/factories/dummy/dummy_notifier.rb +4 -0
- data/spec/factories/dummy/dummy_target.rb +4 -0
- data/spec/factories/notifications.rb +1 -1
- data/spec/factories/users.rb +7 -1
- data/spec/generators/active_record/migration_generator_spec.rb +41 -0
- data/spec/generators/controllers_generator_spec.rb +62 -0
- data/spec/generators/install_generator_spec.rb +43 -0
- data/spec/generators/models/notification_generator_spec.rb +41 -0
- data/spec/generators/views_generator_spec.rb +111 -0
- data/spec/helpers/polymorphic_helpers_spec.rb +89 -0
- data/spec/helpers/view_helpers_spec.rb +258 -0
- data/spec/mailers/mailer_spec.rb +98 -0
- data/spec/models/dummy/dummy_notifiable_spec.rb +6 -0
- data/spec/models/dummy/dummy_notifier_spec.rb +6 -0
- data/spec/models/dummy/dummy_target_spec.rb +6 -0
- data/spec/models/notification_spec.rb +5 -4
- data/spec/rails_app/app/assets/javascripts/application.js +2 -0
- data/spec/rails_app/app/controllers/articles_controller.rb +62 -0
- data/spec/rails_app/app/controllers/comments_controller.rb +34 -0
- data/spec/rails_app/app/models/admin.rb +8 -0
- data/spec/rails_app/app/models/article.rb +6 -6
- data/spec/rails_app/app/models/comment.rb +2 -2
- data/spec/rails_app/app/models/dummy/dummy_base.rb +2 -0
- data/spec/rails_app/app/models/dummy/dummy_notifiable.rb +4 -0
- data/spec/rails_app/app/models/dummy/dummy_notifier.rb +4 -0
- data/spec/rails_app/app/models/dummy/dummy_target.rb +4 -0
- data/spec/rails_app/app/models/user.rb +5 -5
- data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_path_test.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/default/custom/_test.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/users/_custom_index.html.erb +1 -0
- data/spec/rails_app/app/views/activity_notification/notifications/users/custom/_test.html.erb +1 -0
- data/spec/rails_app/app/views/articles/_form.html.erb +20 -0
- data/spec/rails_app/app/views/articles/edit.html.erb +6 -0
- data/spec/rails_app/app/views/articles/index.html.erb +67 -0
- data/spec/rails_app/app/views/articles/new.html.erb +5 -0
- data/spec/rails_app/app/views/articles/show.html.erb +38 -0
- data/spec/rails_app/app/views/layouts/_header.html.erb +8 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +3 -4
- data/spec/rails_app/config/initializers/activity_notification.rb +13 -3
- data/spec/rails_app/config/initializers/devise.rb +274 -274
- data/spec/rails_app/config/locales/activity_notification.en.yml +20 -0
- data/spec/rails_app/config/locales/devise.en.yml +62 -0
- data/spec/rails_app/config/routes.rb +6 -2
- data/spec/rails_app/db/migrate/20160715050433_create_test_tables.rb +6 -2
- data/spec/rails_app/db/schema.rb +8 -0
- data/spec/rails_app/db/seeds.rb +43 -0
- data/spec/roles/acts_as_notifiable_spec.rb +32 -0
- data/spec/roles/acts_as_notifier_spec.rb +17 -0
- data/spec/roles/acts_as_target_spec.rb +40 -0
- data/spec/spec_helper.rb +18 -14
- metadata +136 -12
@@ -0,0 +1,150 @@
|
|
1
|
+
shared_examples_for :common do
|
2
|
+
let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym }
|
3
|
+
let(:test_instance) { create(test_class_name) }
|
4
|
+
|
5
|
+
describe "as public ActivityNotification methods with described class" do
|
6
|
+
describe "#resolve_value" do
|
7
|
+
before do
|
8
|
+
allow(ActivityNotification).to receive(:get_controller).and_return('StubController')
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with value" do
|
12
|
+
it "returns specified value" do
|
13
|
+
expect(ActivityNotification.resolve_value(test_instance, 1)).to eq(1)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with Symbol" do
|
18
|
+
it "returns specified symbol without arguments" do
|
19
|
+
module AdditionalMethods
|
20
|
+
def custom_method
|
21
|
+
1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
test_instance.extend(AdditionalMethods)
|
25
|
+
expect(ActivityNotification.resolve_value(test_instance, :custom_method)).to eq(1)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns specified symbol with controller arguments" do
|
29
|
+
module AdditionalMethods
|
30
|
+
def custom_method(controller)
|
31
|
+
controller == 'StubController' ? 1 : 0
|
32
|
+
end
|
33
|
+
end
|
34
|
+
test_instance.extend(AdditionalMethods)
|
35
|
+
expect(ActivityNotification.resolve_value(test_instance, :custom_method)).to eq(1)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns specified symbol with controller and additional arguments" do
|
39
|
+
module AdditionalMethods
|
40
|
+
def custom_method(controller, key)
|
41
|
+
controller == 'StubController' and key == 'test1.key' ? 1 : 0
|
42
|
+
end
|
43
|
+
end
|
44
|
+
test_instance.extend(AdditionalMethods)
|
45
|
+
expect(ActivityNotification.resolve_value(test_instance, :custom_method, 'test1.key')).to eq(1)
|
46
|
+
expect(ActivityNotification.resolve_value(test_instance, :custom_method, 'test2.key')).to eq(0)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "with Proc" do
|
51
|
+
it "returns specified lambda with context(model) arguments" do
|
52
|
+
test_proc = ->(model){ model == test_instance ? 1 : 0 }
|
53
|
+
expect(ActivityNotification.resolve_value(test_instance, test_proc)).to eq(1)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns specified lambda with controller and context(model) arguments" do
|
57
|
+
test_proc = ->(controller, model){ controller == 'StubController' and model == test_instance ? 1 : 0 }
|
58
|
+
expect(ActivityNotification.resolve_value(test_instance, test_proc)).to eq(1)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns specified lambda with controller, context(model) and additional arguments" do
|
62
|
+
test_proc = ->(controller, model, key){ controller == 'StubController' and model == test_instance and key == 'test1.key' ? 1 : 0 }
|
63
|
+
expect(ActivityNotification.resolve_value(test_instance, test_proc, 'test1.key')).to eq(1)
|
64
|
+
expect(ActivityNotification.resolve_value(test_instance, test_proc, 'test2.key')).to eq(0)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "with Hash" do
|
69
|
+
it "returns resolve_value for each entry of hash" do
|
70
|
+
module AdditionalMethods
|
71
|
+
def custom_method(controller)
|
72
|
+
controller == 'StubController' ? 2 : 0
|
73
|
+
end
|
74
|
+
end
|
75
|
+
test_instance.extend(AdditionalMethods)
|
76
|
+
test_hash = {
|
77
|
+
key1: 1,
|
78
|
+
key2: :custom_method,
|
79
|
+
key3: ->(controller, model){ 3 }
|
80
|
+
}
|
81
|
+
expect(ActivityNotification.resolve_value(test_instance, test_hash)).to eq({ key1: 1, key2: 2, key3: 3 })
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "as public instance methods" do
|
88
|
+
describe "#resolve_value" do
|
89
|
+
context "with value" do
|
90
|
+
it "returns specified value" do
|
91
|
+
expect(test_instance.resolve_value(1)).to eq(1)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "with Symbol" do
|
96
|
+
it "returns specified symbol without arguments" do
|
97
|
+
module AdditionalMethods
|
98
|
+
def custom_method
|
99
|
+
1
|
100
|
+
end
|
101
|
+
end
|
102
|
+
test_instance.extend(AdditionalMethods)
|
103
|
+
expect(test_instance.resolve_value(:custom_method)).to eq(1)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "returns specified symbol with additional arguments" do
|
107
|
+
module AdditionalMethods
|
108
|
+
def custom_method(key)
|
109
|
+
key == 'test1.key' ? 1 : 0
|
110
|
+
end
|
111
|
+
end
|
112
|
+
test_instance.extend(AdditionalMethods)
|
113
|
+
expect(test_instance.resolve_value(:custom_method, 'test1.key')).to eq(1)
|
114
|
+
expect(test_instance.resolve_value(:custom_method, 'test2.key')).to eq(0)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context "with Proc" do
|
119
|
+
it "returns specified lambda with context(model) argument" do
|
120
|
+
test_proc = ->(model){ model == test_instance ? 1 : 0 }
|
121
|
+
expect(test_instance.resolve_value(test_proc)).to eq(1)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "returns specified lambda with context(model) and additional arguments" do
|
125
|
+
test_proc = ->(model, key){ model == test_instance and key == 'test1.key' ? 1 : 0 }
|
126
|
+
expect(test_instance.resolve_value(test_proc, 'test1.key')).to eq(1)
|
127
|
+
expect(test_instance.resolve_value(test_proc, 'test2.key')).to eq(0)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "with Hash" do
|
132
|
+
it "returns resolve_value for each entry of hash" do
|
133
|
+
module AdditionalMethods
|
134
|
+
def custom_method
|
135
|
+
2
|
136
|
+
end
|
137
|
+
end
|
138
|
+
test_instance.extend(AdditionalMethods)
|
139
|
+
test_hash = {
|
140
|
+
key1: 1,
|
141
|
+
key2: :custom_method,
|
142
|
+
key3: ->(model){ model == test_instance ? 3 : 0 }
|
143
|
+
}
|
144
|
+
expect(test_instance.resolve_value(test_hash)).to eq({ key1: 1, key2: 2, key3: 3 })
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
@@ -0,0 +1,435 @@
|
|
1
|
+
shared_examples_for :notifiable do
|
2
|
+
let(:test_class_name) { described_class.to_s.underscore.split('/').last.to_sym }
|
3
|
+
let(:test_instance) { create(test_class_name) }
|
4
|
+
let(:test_target) { create(:user) }
|
5
|
+
|
6
|
+
include Rails.application.routes.url_helpers
|
7
|
+
|
8
|
+
describe "as public class methods" do
|
9
|
+
describe "#available_as_notifiable?" do
|
10
|
+
it "returns true" do
|
11
|
+
expect(described_class.available_as_notifiable?).to be_truthy
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#set_notifiable_class_defaults" do
|
16
|
+
it "set parameter fields as default" do
|
17
|
+
described_class.set_notifiable_class_defaults
|
18
|
+
expect(described_class._notification_targets).to eq({})
|
19
|
+
expect(described_class._notification_group).to eq({})
|
20
|
+
expect(described_class._notifier).to eq({})
|
21
|
+
expect(described_class._notification_parameters).to eq({})
|
22
|
+
expect(described_class._notification_email_allowed).to eq({})
|
23
|
+
expect(described_class._notifiable_path).to eq({})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "as public instance methods" do
|
29
|
+
before do
|
30
|
+
User.delete_all
|
31
|
+
described_class.set_notifiable_class_defaults
|
32
|
+
create(:user)
|
33
|
+
create(:user)
|
34
|
+
expect(User.all.count).to eq(2)
|
35
|
+
expect(User.all.first).to be_an_instance_of(User)
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#notification_targets" do
|
39
|
+
context "without any configuration" do
|
40
|
+
it "raises NotImplementedError" do
|
41
|
+
expect { test_instance.notification_targets(User, 'dummy_key') }
|
42
|
+
.to raise_error(NotImplementedError, /You have to implement .+ or set :targets in acts_as_notifiable/)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "configured with overriden method" do
|
47
|
+
it "returns specified value" do
|
48
|
+
module AdditionalMethods
|
49
|
+
def notification_users(key)
|
50
|
+
User.all
|
51
|
+
end
|
52
|
+
end
|
53
|
+
test_instance.extend(AdditionalMethods)
|
54
|
+
expect(test_instance.notification_targets(User, 'dummy_key')).to eq(User.all)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "configured with a field" do
|
59
|
+
it "returns specified value" do
|
60
|
+
described_class._notification_targets[:users] = User.all
|
61
|
+
expect(test_instance.notification_targets(User, 'dummy_key')).to eq(User.all)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "returns specified symbol without argumentss" do
|
65
|
+
module AdditionalMethods
|
66
|
+
def custom_notification_users
|
67
|
+
User.all
|
68
|
+
end
|
69
|
+
end
|
70
|
+
test_instance.extend(AdditionalMethods)
|
71
|
+
described_class._notification_targets[:users] = :custom_notification_users
|
72
|
+
expect(test_instance.notification_targets(User, 'dummy_key')).to eq(User.all)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "returns specified symbol with key argument" do
|
76
|
+
module AdditionalMethods
|
77
|
+
def custom_notification_users(key)
|
78
|
+
User.all
|
79
|
+
end
|
80
|
+
end
|
81
|
+
test_instance.extend(AdditionalMethods)
|
82
|
+
described_class._notification_targets[:users] = :custom_notification_users
|
83
|
+
expect(test_instance.notification_targets(User, 'dummy_key')).to eq(User.all)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "returns specified lambda with single notifiable argument" do
|
87
|
+
described_class._notification_targets[:users] = ->(notifiable){ User.all }
|
88
|
+
expect(test_instance.notification_targets(User, 'dummy_key')).to eq(User.all)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "returns specified lambda with notifiable and key arguments" do
|
92
|
+
described_class._notification_targets[:users] = ->(notifiable, key){ User.all }
|
93
|
+
expect(test_instance.notification_targets(User, 'dummy_key')).to eq(User.all)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "#notification_group" do
|
99
|
+
context "without any configuration" do
|
100
|
+
it "returns nil" do
|
101
|
+
expect(test_instance.notification_group(User, 'dummy_key')).to be_nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "configured with overriden method" do
|
106
|
+
it "returns specified value" do
|
107
|
+
module AdditionalMethods
|
108
|
+
def notification_group_for_users(key)
|
109
|
+
User.all.first
|
110
|
+
end
|
111
|
+
end
|
112
|
+
test_instance.extend(AdditionalMethods)
|
113
|
+
expect(test_instance.notification_group(User, 'dummy_key')).to eq(User.all.first)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context "configured with a field" do
|
118
|
+
it "returns specified value" do
|
119
|
+
described_class._notification_group[:users] = User.all.first
|
120
|
+
expect(test_instance.notification_group(User, 'dummy_key')).to eq(User.all.first)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "returns specified symbol without argumentss" do
|
124
|
+
module AdditionalMethods
|
125
|
+
def custom_notification_group
|
126
|
+
User.all.first
|
127
|
+
end
|
128
|
+
end
|
129
|
+
test_instance.extend(AdditionalMethods)
|
130
|
+
described_class._notification_group[:users] = :custom_notification_group
|
131
|
+
expect(test_instance.notification_group(User, 'dummy_key')).to eq(User.all.first)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "returns specified symbol with key argument" do
|
135
|
+
module AdditionalMethods
|
136
|
+
def custom_notification_group(key)
|
137
|
+
User.all.first
|
138
|
+
end
|
139
|
+
end
|
140
|
+
test_instance.extend(AdditionalMethods)
|
141
|
+
described_class._notification_group[:users] = :custom_notification_group
|
142
|
+
expect(test_instance.notification_group(User, 'dummy_key')).to eq(User.all.first)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "returns specified lambda with single notifiable argument" do
|
146
|
+
described_class._notification_group[:users] = ->(notifiable){ User.all.first }
|
147
|
+
expect(test_instance.notification_group(User, 'dummy_key')).to eq(User.all.first)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "returns specified lambda with notifiable and key arguments" do
|
151
|
+
described_class._notification_group[:users] = ->(notifiable, key){ User.all.first }
|
152
|
+
expect(test_instance.notification_group(User, 'dummy_key')).to eq(User.all.first)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "#notification_parameters" do
|
158
|
+
context "without any configuration" do
|
159
|
+
it "returns blank hash" do
|
160
|
+
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({})
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context "configured with overriden method" do
|
165
|
+
it "returns specified value" do
|
166
|
+
module AdditionalMethods
|
167
|
+
def notification_parameters_for_users(key)
|
168
|
+
{ hoge: 'fuga', foo: 'bar' }
|
169
|
+
end
|
170
|
+
end
|
171
|
+
test_instance.extend(AdditionalMethods)
|
172
|
+
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({ hoge: 'fuga', foo: 'bar' })
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "configured with a field" do
|
177
|
+
it "returns specified value" do
|
178
|
+
described_class._notification_parameters[:users] = { hoge: 'fuga', foo: 'bar' }
|
179
|
+
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({ hoge: 'fuga', foo: 'bar' })
|
180
|
+
end
|
181
|
+
|
182
|
+
it "returns specified symbol without arguments" do
|
183
|
+
module AdditionalMethods
|
184
|
+
def custom_notification_parameters
|
185
|
+
{ hoge: 'fuga', foo: 'bar' }
|
186
|
+
end
|
187
|
+
end
|
188
|
+
test_instance.extend(AdditionalMethods)
|
189
|
+
described_class._notification_parameters[:users] = :custom_notification_parameters
|
190
|
+
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({ hoge: 'fuga', foo: 'bar' })
|
191
|
+
end
|
192
|
+
|
193
|
+
it "returns specified symbol with key argument" do
|
194
|
+
module AdditionalMethods
|
195
|
+
def custom_notification_parameters(key)
|
196
|
+
{ hoge: 'fuga', foo: 'bar' }
|
197
|
+
end
|
198
|
+
end
|
199
|
+
test_instance.extend(AdditionalMethods)
|
200
|
+
described_class._notification_parameters[:users] = :custom_notification_parameters
|
201
|
+
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({ hoge: 'fuga', foo: 'bar' })
|
202
|
+
end
|
203
|
+
|
204
|
+
it "returns specified lambda with single notifiable argument" do
|
205
|
+
described_class._notification_parameters[:users] = ->(notifiable){ { hoge: 'fuga', foo: 'bar' } }
|
206
|
+
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({ hoge: 'fuga', foo: 'bar' })
|
207
|
+
end
|
208
|
+
|
209
|
+
it "returns specified lambda with notifiable and key arguments" do
|
210
|
+
described_class._notification_parameters[:users] = ->(notifiable, key){ { hoge: 'fuga', foo: 'bar' } }
|
211
|
+
expect(test_instance.notification_parameters(User, 'dummy_key')).to eq({ hoge: 'fuga', foo: 'bar' })
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe "#notifier" do
|
217
|
+
context "without any configuration" do
|
218
|
+
it "returns nil" do
|
219
|
+
expect(test_instance.notifier(User, 'dummy_key')).to be_nil
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
context "configured with overriden method" do
|
224
|
+
it "returns specified value" do
|
225
|
+
module AdditionalMethods
|
226
|
+
def notifier_for_users(key)
|
227
|
+
User.all.first
|
228
|
+
end
|
229
|
+
end
|
230
|
+
test_instance.extend(AdditionalMethods)
|
231
|
+
expect(test_instance.notifier(User, 'dummy_key')).to eq(User.all.first)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
context "configured with a field" do
|
236
|
+
it "returns specified value" do
|
237
|
+
described_class._notifier[:users] = User.all.first
|
238
|
+
expect(test_instance.notifier(User, 'dummy_key')).to eq(User.all.first)
|
239
|
+
end
|
240
|
+
|
241
|
+
it "returns specified symbol without arguments" do
|
242
|
+
module AdditionalMethods
|
243
|
+
def custom_notifier
|
244
|
+
User.all.first
|
245
|
+
end
|
246
|
+
end
|
247
|
+
test_instance.extend(AdditionalMethods)
|
248
|
+
described_class._notifier[:users] = :custom_notifier
|
249
|
+
expect(test_instance.notifier(User, 'dummy_key')).to eq(User.all.first)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "returns specified symbol with key argument" do
|
253
|
+
module AdditionalMethods
|
254
|
+
def custom_notifier(key)
|
255
|
+
User.all.first
|
256
|
+
end
|
257
|
+
end
|
258
|
+
test_instance.extend(AdditionalMethods)
|
259
|
+
described_class._notifier[:users] = :custom_notifier
|
260
|
+
expect(test_instance.notifier(User, 'dummy_key')).to eq(User.all.first)
|
261
|
+
end
|
262
|
+
|
263
|
+
it "returns specified lambda with single notifiable argument" do
|
264
|
+
described_class._notifier[:users] = ->(notifiable){ User.all.first }
|
265
|
+
expect(test_instance.notifier(User, 'dummy_key')).to eq(User.all.first)
|
266
|
+
end
|
267
|
+
|
268
|
+
it "returns specified lambda with notifiable and key arguments" do
|
269
|
+
described_class._notifier[:users] = ->(notifiable, key){ User.all.first }
|
270
|
+
expect(test_instance.notifier(User, 'dummy_key')).to eq(User.all.first)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
describe "#notification_email_allowed?" do
|
276
|
+
context "without any configuration" do
|
277
|
+
it "returns ActivityNotification.config.email_enabled" do
|
278
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key'))
|
279
|
+
.to eq(ActivityNotification.config.email_enabled)
|
280
|
+
end
|
281
|
+
|
282
|
+
it "returns false as default" do
|
283
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to be_falsey
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
context "configured with overriden method" do
|
288
|
+
it "returns specified value" do
|
289
|
+
module AdditionalMethods
|
290
|
+
def notification_email_allowed_for_users?(target, key)
|
291
|
+
true
|
292
|
+
end
|
293
|
+
end
|
294
|
+
test_instance.extend(AdditionalMethods)
|
295
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(true)
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
context "configured with a field" do
|
300
|
+
it "returns specified value" do
|
301
|
+
described_class._notification_email_allowed[:users] = true
|
302
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(true)
|
303
|
+
end
|
304
|
+
|
305
|
+
it "returns specified symbol without arguments" do
|
306
|
+
module AdditionalMethods
|
307
|
+
def custom_notification_email_allowed?
|
308
|
+
true
|
309
|
+
end
|
310
|
+
end
|
311
|
+
test_instance.extend(AdditionalMethods)
|
312
|
+
described_class._notification_email_allowed[:users] = :custom_notification_email_allowed?
|
313
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(true)
|
314
|
+
end
|
315
|
+
|
316
|
+
it "returns specified symbol with target and key arguments" do
|
317
|
+
module AdditionalMethods
|
318
|
+
def custom_notification_email_allowed?(target, key)
|
319
|
+
true
|
320
|
+
end
|
321
|
+
end
|
322
|
+
test_instance.extend(AdditionalMethods)
|
323
|
+
described_class._notification_email_allowed[:users] = :custom_notification_email_allowed?
|
324
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(true)
|
325
|
+
end
|
326
|
+
|
327
|
+
it "returns specified lambda with single notifiable argument" do
|
328
|
+
described_class._notification_email_allowed[:users] = ->(notifiable){ true }
|
329
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(true)
|
330
|
+
end
|
331
|
+
|
332
|
+
it "returns specified lambda with notifiable, target and key arguments" do
|
333
|
+
described_class._notification_email_allowed[:users] = ->(notifiable, target, key){ true }
|
334
|
+
expect(test_instance.notification_email_allowed?(test_target, 'dummy_key')).to eq(true)
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
describe "#notifiable_path" do
|
340
|
+
context "without any configuration" do
|
341
|
+
it "raises NotImplementedError" do
|
342
|
+
expect { test_instance.notifiable_path(User, 'dummy_key') }
|
343
|
+
.to raise_error(NotImplementedError, /You have to implement .+, set :notifiable_path in acts_as_notifiable or set polymorphic_path routing for/)
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
context "configured with polymorphic_path" do
|
348
|
+
it "returns polymorphic_path" do
|
349
|
+
article = create(:article)
|
350
|
+
expect(article.notifiable_path(User, 'dummy_key')).to eq(article_path(article))
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
context "configured with overriden method" do
|
355
|
+
it "returns specified value" do
|
356
|
+
module AdditionalMethods
|
357
|
+
def notifiable_path_for_users(key)
|
358
|
+
article_path(1)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
test_instance.extend(AdditionalMethods)
|
362
|
+
expect(test_instance.notifiable_path(User, 'dummy_key')).to eq(article_path(1))
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
context "configured with a field" do
|
367
|
+
it "returns specified value" do
|
368
|
+
described_class._notifiable_path[:users] = article_path(1)
|
369
|
+
expect(test_instance.notifiable_path(User, 'dummy_key')).to eq(article_path(1))
|
370
|
+
end
|
371
|
+
|
372
|
+
it "returns specified symbol without arguments" do
|
373
|
+
module AdditionalMethods
|
374
|
+
def custom_notifiable_path
|
375
|
+
article_path(1)
|
376
|
+
end
|
377
|
+
end
|
378
|
+
test_instance.extend(AdditionalMethods)
|
379
|
+
described_class._notifiable_path[:users] = :custom_notifiable_path
|
380
|
+
expect(test_instance.notifiable_path(User, 'dummy_key')).to eq(article_path(1))
|
381
|
+
end
|
382
|
+
|
383
|
+
it "returns specified symbol with key argument" do
|
384
|
+
module AdditionalMethods
|
385
|
+
def custom_notifiable_path(key)
|
386
|
+
article_path(1)
|
387
|
+
end
|
388
|
+
end
|
389
|
+
test_instance.extend(AdditionalMethods)
|
390
|
+
described_class._notifiable_path[:users] = :custom_notifiable_path
|
391
|
+
expect(test_instance.notifiable_path(User, 'dummy_key')).to eq(article_path(1))
|
392
|
+
end
|
393
|
+
|
394
|
+
it "returns specified lambda with single notifiable argument" do
|
395
|
+
described_class._notifiable_path[:users] = ->(notifiable){ article_path(1) }
|
396
|
+
expect(test_instance.notifiable_path(User, 'dummy_key')).to eq(article_path(1))
|
397
|
+
end
|
398
|
+
|
399
|
+
it "returns specified lambda with notifiable and key arguments" do
|
400
|
+
described_class._notifiable_path[:users] = ->(notifiable, key){ article_path(1) }
|
401
|
+
expect(test_instance.notifiable_path(User, 'dummy_key')).to eq(article_path(1))
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
|
406
|
+
describe "#notify" do
|
407
|
+
it "is an alias of ActivityNotification::Notification.notify" do
|
408
|
+
expect(ActivityNotification::Notification).to receive(:notify)
|
409
|
+
test_instance.notify :users
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
describe "#notify_to" do
|
414
|
+
it "is an alias of ActivityNotification::Notification.notify_to" do
|
415
|
+
expect(ActivityNotification::Notification).to receive(:notify_to)
|
416
|
+
test_instance.notify_to create(:user)
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
describe "#notify_all" do
|
421
|
+
it "is an alias of ActivityNotification::Notification.notify_all" do
|
422
|
+
expect(ActivityNotification::Notification).to receive(:notify_all)
|
423
|
+
test_instance.notify_all [create(:user)]
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
describe "#default_notification_key" do
|
428
|
+
it "returns '#to_resource_name.default'" do
|
429
|
+
expect(test_instance.default_notification_key).to eq("#{test_instance.to_resource_name}.default")
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
end
|
434
|
+
|
435
|
+
end
|