proclaimer 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ module Proclaimer
2
+ class Engine < Rails::Engine
3
+ require "spree/core"
4
+ isolate_namespace Spree
5
+ engine_name "proclaimer"
6
+
7
+ # use rspec for tests
8
+ config.generators do |g|
9
+ g.test_framework :rspec
10
+ end
11
+
12
+ def self.activate
13
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
14
+ Rails.configuration.cache_classes ? require(c) : load(c)
15
+ end
16
+ end
17
+
18
+ config.to_prepare &method(:activate).to_proc
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Proclaimer
2
+ VERSION = "0.2.0".freeze
3
+ end
data/lib/proclaimer.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "spree_core"
2
+ require "proclaimer/engine"
3
+ require "proclaimer/configuration"
4
+
5
+ module Proclaimer
6
+ def self.configure(&block)
7
+ Configuration.new.instance_eval(&block)
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ require "./lib/proclaimer/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.platform = Gem::Platform::RUBY
6
+ s.name = "proclaimer"
7
+ s.version = Proclaimer::VERSION
8
+ s.summary = "An easy, and extensible transactional event notification " +
9
+ "add on for Spree."
10
+ s.description = s.summary
11
+ s.required_ruby_version = ">= 2.2.0"
12
+
13
+ s.authors = ["Prem Sichanugrist", "Vincent Franco", "David Freerksen"]
14
+ s.homepage = "https://github.com/groundctrl/proclaimer"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.require_path = "lib"
19
+ s.requirements << "none"
20
+
21
+ s.add_dependency "spree_backend", "~> 3.0.0"
22
+
23
+ s.add_development_dependency "capybara", "~> 2.4"
24
+ s.add_development_dependency "coffee-rails"
25
+ s.add_development_dependency "database_cleaner"
26
+ s.add_development_dependency "factory_girl", "~> 4.5"
27
+ s.add_development_dependency "rspec-rails", "~> 3.1"
28
+ s.add_development_dependency "sass-rails", "~> 4.0.2"
29
+ s.add_development_dependency "simplecov"
30
+ s.add_development_dependency "sqlite3"
31
+ s.add_development_dependency "pry-byebug"
32
+ end
@@ -0,0 +1,102 @@
1
+ require "spec_helper"
2
+ require "proclaimer/configuration"
3
+
4
+ RSpec.describe Proclaimer::Configuration do
5
+ before do
6
+ stub_const "PAYLOAD", Object.new
7
+ end
8
+
9
+ after do
10
+ ActiveSupport::Notifications.unsubscribe(/^spree\./)
11
+ end
12
+
13
+ describe "#subscribe" do
14
+ context "subscription" do
15
+ it "supports subscribing to an event with given event name" do
16
+ event_name = nil
17
+ payload = nil
18
+
19
+ configuration.subscribe("order.complete") do |*args|
20
+ event_name = args.first
21
+ payload = args.second
22
+ end
23
+
24
+ trigger_event "spree.order.complete", PAYLOAD
25
+
26
+ expect(event_name).to eq "spree.order.complete"
27
+ expect(payload).to eq PAYLOAD
28
+ end
29
+
30
+ it "supports subscribing to any event starting with given string" do
31
+ event_name = nil
32
+ payload = nil
33
+
34
+ configuration.subscribe("order") do |*args|
35
+ event_name = args.first
36
+ payload = args.second
37
+ end
38
+
39
+ trigger_event "spree.order.complete", PAYLOAD
40
+
41
+ expect(event_name).to eq "spree.order.complete"
42
+ expect(payload).to eq PAYLOAD
43
+ end
44
+ end
45
+
46
+ context "callable" do
47
+ it "raises error if the given object does not respond to call" do
48
+ expect { configuration.subscribe("event", :nope) }.
49
+ to raise_error(ArgumentError, /call/)
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#subscribe_all" do
55
+ context "subscription" do
56
+ it "subscribe to all spree events" do
57
+ event_name = nil
58
+ payload = nil
59
+
60
+ configuration.subscribe_all do |*args|
61
+ event_name = args.first
62
+ payload = args.second
63
+ end
64
+
65
+ trigger_event "spree.order.complete", PAYLOAD
66
+
67
+ expect(event_name).to eq "spree.order.complete"
68
+ expect(payload).to eq PAYLOAD
69
+ end
70
+
71
+ it "does not subscribe to non-spree event" do
72
+ event_name = nil
73
+ payload = nil
74
+
75
+ configuration.subscribe_all do |*args|
76
+ event_name = args.first
77
+ payload = args.second
78
+ end
79
+
80
+ trigger_event "some.other.event", {}
81
+
82
+ expect(event_name).to be_nil
83
+ expect(payload).to be_nil
84
+ end
85
+ end
86
+
87
+ context "callable" do
88
+ it "raises error if the given object does not respond to call" do
89
+ expect { configuration.subscribe_all(:nope) }.
90
+ to raise_error(ArgumentError, /call/)
91
+ end
92
+ end
93
+ end
94
+
95
+ def configuration
96
+ Proclaimer::Configuration.new
97
+ end
98
+
99
+ def trigger_event(event_name, payload)
100
+ ActiveSupport::Notifications.instrument(event_name, payload)
101
+ end
102
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Spree::Order do
4
+ before { stub_const("EMPTY_PAYLOAD", Object.new) }
5
+
6
+ context "when order is first created" do
7
+ it "does not instrument any order event" do
8
+ payload = EMPTY_PAYLOAD
9
+
10
+ ActiveSupport::Notifications.subscribed(
11
+ -> (*args) { payload = args.last },
12
+ /^spree\.order/
13
+ ) do
14
+ create(:order)
15
+
16
+ expect(payload).to eq EMPTY_PAYLOAD
17
+ end
18
+ end
19
+ end
20
+
21
+ context "when order is complete" do
22
+ it "instruments order.complete event" do
23
+ payload = EMPTY_PAYLOAD
24
+
25
+ ActiveSupport::Notifications.subscribed(
26
+ -> (*args) { payload = args.last },
27
+ "spree.order.complete"
28
+ ) do
29
+ order = create(:order_with_line_items, state: "confirm")
30
+ create(:payment, amount: order.total, order: order)
31
+ order.next!
32
+
33
+ expect(payload[:order]).to eq order
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ module Spree
4
+ RSpec.describe OrderUpdater, type: :model do
5
+
6
+ describe "#update_shipments_with_status_tracking" do
7
+ it "calls shipment#broadcast on shipments that have changed state" do
8
+ fake_shipments = [
9
+ fake_shipment_with_state('pending', 'ready'),
10
+ fake_shipment_with_state('pending')
11
+ ]
12
+ fake_order = double(Order, shipments: fake_shipments)
13
+ updater = described_class.new(fake_order)
14
+
15
+ updater.update_shipments
16
+
17
+ expect(fake_shipments.first).to have_received(:broadcast_state)
18
+ expect(fake_shipments.last).not_to have_received(:broadcast_state)
19
+ end
20
+
21
+ def fake_shipment_with_state(*states)
22
+ spy(Shipment).tap do |shipment|
23
+ allow(shipment).to receive(:state).and_return(*states)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Spree::Payment do
4
+ context "when payment is complete" do
5
+ it "instruments payment.complete event" do
6
+ payload = nil
7
+
8
+ ActiveSupport::Notifications.subscribed(
9
+ -> (*args) { payload = args.last },
10
+ "spree.payment.complete"
11
+ ) do
12
+ payment = create(:payment)
13
+ payment.complete
14
+
15
+ expect(payload[:payment]).to eq payment
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Spree::Refund do
4
+ before { stub_const("EMPTY_PAYLOAD", Object.new) }
5
+
6
+ context "when refund have not been processed" do
7
+ it "does not instrument any refund event" do
8
+ payload = EMPTY_PAYLOAD
9
+
10
+ ActiveSupport::Notifications.subscribed(
11
+ -> (*args) { payload = args.last },
12
+ /^spree\.refund/
13
+ ) do
14
+ build(:refund)
15
+
16
+ expect(payload).to eq EMPTY_PAYLOAD
17
+ end
18
+ end
19
+ end
20
+
21
+ context "when the refund has been processed" do
22
+ it "instruments refund.complete event" do
23
+ payload = EMPTY_PAYLOAD
24
+
25
+ ActiveSupport::Notifications.subscribed(
26
+ -> (*args) { payload = args.last },
27
+ "spree.refund.complete"
28
+ ) do
29
+ refund = create(:refund, payment: create(:payment, amount: 100))
30
+
31
+ expect(payload[:refund]).to eq refund
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Spree::Shipment do
4
+ context "when shipment is pending" do
5
+ it "instruments shipment.pending event" do
6
+ payload = nil
7
+
8
+ ActiveSupport::Notifications.subscribed(
9
+ -> (*args) { payload = args.last },
10
+ "spree.shipment.pending"
11
+ ) do
12
+ shipment = create(:shipment)
13
+
14
+ expect(payload[:shipment]).to eq shipment
15
+ end
16
+ end
17
+ end
18
+
19
+ context "when shipment is ready" do
20
+ it "instruments shipment.ready event" do
21
+ shipment = create(:shipment)
22
+ allow(shipment).to receive(:determine_state) { 'ready' }
23
+ allow(ActiveSupport::Notifications).to receive(:instrument)
24
+
25
+ shipment.ready
26
+
27
+ expect(ActiveSupport::Notifications).
28
+ to have_received(:instrument).
29
+ with("spree.shipment.ready", shipment: shipment)
30
+ end
31
+ end
32
+
33
+ context "when shipment is canceled" do
34
+ it "instruments shipment.canceled event" do
35
+ shipment = create(:shipment)
36
+ allow(ActiveSupport::Notifications).to receive(:instrument)
37
+
38
+ shipment.cancel
39
+
40
+ expect(ActiveSupport::Notifications).
41
+ to have_received(:instrument).
42
+ with("spree.shipment.canceled", shipment: shipment)
43
+ end
44
+ end
45
+
46
+ describe "#broadcast_state" do
47
+ it "instruments shipment.STATE event" do
48
+ shipment = build(:shipment, state: 'pending')
49
+ allow(ActiveSupport::Notifications).to receive(:instrument)
50
+
51
+ shipment.broadcast_state
52
+
53
+ expect(ActiveSupport::Notifications).
54
+ to have_received(:instrument).
55
+ with("spree.shipment.pending", shipment: shipment)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+ require "proclaimer"
3
+
4
+ RSpec.describe Proclaimer do
5
+ describe "configure" do
6
+ it "initialize a configuration object and passing in the block" do
7
+ configuration = double(:configuration)
8
+ given_block = -> {}
9
+ received_block = nil
10
+ allow(Proclaimer::Configuration).
11
+ to receive(:new).and_return(configuration)
12
+ allow(configuration).to receive(:instance_eval) do |&block|
13
+ received_block = block
14
+ end
15
+
16
+ Proclaimer.configure(&given_block)
17
+
18
+ expect(configuration).to have_received(:instance_eval)
19
+ expect(received_block).to eq given_block
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,88 @@
1
+ # Run Coverage report
2
+ require "simplecov"
3
+ SimpleCov.start do
4
+ add_filter "spec/dummy"
5
+ add_group "Controllers", "app/controllers"
6
+ add_group "Helpers", "app/helpers"
7
+ add_group "Mailers", "app/mailers"
8
+ add_group "Models", "app/models"
9
+ add_group "Views", "app/views"
10
+ add_group "Libraries", "lib"
11
+ end
12
+
13
+ # Configure Rails Environment
14
+ ENV["RAILS_ENV"] = "test"
15
+
16
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
17
+
18
+ require "rspec/rails"
19
+ require "database_cleaner"
20
+ require "ffaker"
21
+
22
+ # Requires supporting ruby files with custom matchers and macros, etc,
23
+ # in spec/support/ and its subdirectories.
24
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }
25
+
26
+ # Requires factories and other useful helpers defined in spree_core.
27
+ require "spree/testing_support/authorization_helpers"
28
+ require "spree/testing_support/capybara_ext"
29
+ require "spree/testing_support/controller_requests"
30
+ require "spree/testing_support/factories"
31
+ require "spree/testing_support/url_helpers"
32
+
33
+ RSpec.configure do |config|
34
+ config.include FactoryGirl::Syntax::Methods
35
+
36
+ # Infer an example group"s spec type from the file location.
37
+ config.infer_spec_type_from_file_location!
38
+
39
+ # == URL Helpers
40
+ #
41
+ # Allows access to Spree"s routes in specs:
42
+ #
43
+ # visit spree.admin_path
44
+ # current_path.should eql(spree.products_path)
45
+ config.include Spree::TestingSupport::UrlHelpers
46
+
47
+ # == Mock Framework
48
+ #
49
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
50
+ #
51
+ # config.mock_with :mocha
52
+ # config.mock_with :flexmock
53
+ # config.mock_with :rr
54
+ config.mock_with :rspec
55
+ config.color = true
56
+
57
+ # Remove this line if you"re not using ActiveRecord or ActiveRecord fixtures
58
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
59
+
60
+ # Capybara javascript drivers require transactional fixtures set to false, and we use DatabaseCleaner
61
+ # to cleanup after each test instead. Without transactional fixtures set to false the records created
62
+ # to setup a test will be unavailable to the browser, which runs under a separate server instance.
63
+ config.use_transactional_fixtures = false
64
+
65
+ # Ensure Suite is set to use transactions for speed.
66
+ config.before :suite do
67
+ DatabaseCleaner.strategy = :transaction
68
+ DatabaseCleaner.clean_with :truncation
69
+ end
70
+
71
+ # Before each spec check if it is a Javascript test and switch between using database transactions or not where necessary.
72
+ config.before :each do
73
+ DatabaseCleaner.strategy = RSpec.current_example.metadata[:js] ? :truncation : :transaction
74
+ DatabaseCleaner.start
75
+ end
76
+
77
+ # After each spec clean the database.
78
+ config.after :each do
79
+ DatabaseCleaner.clean
80
+ end
81
+
82
+ config.fail_fast = ENV["FAIL_FAST"] || false
83
+ config.order = "random"
84
+
85
+ # Include helpers for controller test
86
+ config.include Spree::TestingSupport::ControllerRequests, type: :controller
87
+ config.include Devise::TestHelpers, type: :controller
88
+ end
metadata ADDED
@@ -0,0 +1,220 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proclaimer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Prem Sichanugrist
8
+ - Vincent Franco
9
+ - David Freerksen
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-10-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: spree_backend
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: 3.0.0
29
+ - !ruby/object:Gem::Dependency
30
+ name: capybara
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '2.4'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.4'
43
+ - !ruby/object:Gem::Dependency
44
+ name: coffee-rails
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: database_cleaner
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: factory_girl
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '4.5'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '4.5'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rspec-rails
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '3.1'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '3.1'
99
+ - !ruby/object:Gem::Dependency
100
+ name: sass-rails
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: 4.0.2
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: 4.0.2
113
+ - !ruby/object:Gem::Dependency
114
+ name: simplecov
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: sqlite3
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: pry-byebug
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ type: :development
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ description: An easy, and extensible transactional event notification add on for Spree.
156
+ email:
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - ".travis.yml"
163
+ - Gemfile
164
+ - Gemfile.lock
165
+ - LICENSE
166
+ - README.md
167
+ - Rakefile
168
+ - app/models/spree/order_decorator.rb
169
+ - app/models/spree/order_updater_decorator.rb
170
+ - app/models/spree/payment_decorator.rb
171
+ - app/models/spree/refund_decorator.rb
172
+ - app/models/spree/shipment_decorator.rb
173
+ - bin/rails
174
+ - bin/rake
175
+ - lib/proclaimer.rb
176
+ - lib/proclaimer/configuration.rb
177
+ - lib/proclaimer/engine.rb
178
+ - lib/proclaimer/version.rb
179
+ - proclaimer.gemspec
180
+ - spec/configuration_spec.rb
181
+ - spec/models/spree/order_spec.rb
182
+ - spec/models/spree/order_updater_spec.rb
183
+ - spec/models/spree/payment_spec.rb
184
+ - spec/models/spree/refund_spec.rb
185
+ - spec/models/spree/shipment_spec.rb
186
+ - spec/proclaimer_spec.rb
187
+ - spec/spec_helper.rb
188
+ homepage: https://github.com/groundctrl/proclaimer
189
+ licenses: []
190
+ metadata: {}
191
+ post_install_message:
192
+ rdoc_options: []
193
+ require_paths:
194
+ - lib
195
+ required_ruby_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: 2.2.0
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ requirements:
206
+ - none
207
+ rubyforge_project:
208
+ rubygems_version: 2.4.5
209
+ signing_key:
210
+ specification_version: 4
211
+ summary: An easy, and extensible transactional event notification add on for Spree.
212
+ test_files:
213
+ - spec/configuration_spec.rb
214
+ - spec/models/spree/order_spec.rb
215
+ - spec/models/spree/order_updater_spec.rb
216
+ - spec/models/spree/payment_spec.rb
217
+ - spec/models/spree/refund_spec.rb
218
+ - spec/models/spree/shipment_spec.rb
219
+ - spec/proclaimer_spec.rb
220
+ - spec/spec_helper.rb