core_merchant 0.6.2 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fceb2084b15e74e2aebe360df8cade628ffd930afd2ef689a6779c3f59f5f70a
4
- data.tar.gz: 96368a736ce980a109bfd9d863b56cbec0c814111edd4839874738d13cc6c9e3
3
+ metadata.gz: 154e94061339edca03a2e9c88630c4e49ce37272252e5d6436810579ea2bd529
4
+ data.tar.gz: 5fe4eb2a4c24b26945a505f35d9a7694de69e7630c03776ac78b7dcd6671616b
5
5
  SHA512:
6
- metadata.gz: 643e829909744d948af7a5270ebdd0f3be2dfa7f3d540da9f0fd728fc9d052731719d6ec6f93cfc73d7357f25c3bbd50a8fd32046b0c2a354058fc611d34e8ac
7
- data.tar.gz: a2ecdd77b9628e2dee8b67b422f0d4fa2f1a62214c28bc9bb1ae72338986272b03bfce1a193969a3fbbc6e440e45ac041c8dd2c637de1fa0d7f796244f6b6daf
6
+ metadata.gz: 9debecdb722370c173fc97648d5f38e130e95475edb673d5f420b7074db10dfad908221cb2b2a1dec966be064fa07040a4ad287ab10380741cd9d9df27f8d618
7
+ data.tar.gz: 6d5675f20dfa56f708026a28f64cb48dcc44792f6e2663a13e1c466867defb2120f5947f84db8314d983246ef511bac2ce434aa3956fc621bdaf9cdd12b4a04c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- core_merchant (0.6.0)
4
+ core_merchant (0.6.2)
5
5
  activesupport (~> 7.0)
6
6
  rails (~> 7.0)
7
7
 
@@ -99,6 +99,8 @@ GEM
99
99
  diff-lcs (1.5.1)
100
100
  drb (2.2.1)
101
101
  erubi (1.13.0)
102
+ factory_bot (6.4.6)
103
+ activesupport (>= 5.0.0)
102
104
  generator_spec (0.9.5)
103
105
  activesupport (>= 3.0.0)
104
106
  railties (>= 3.0.0)
@@ -251,6 +253,7 @@ PLATFORMS
251
253
  DEPENDENCIES
252
254
  core_merchant!
253
255
  database_cleaner
256
+ factory_bot
254
257
  generator_spec (~> 0.9.4)
255
258
  railties (~> 7.0)
256
259
  rake (~> 13.0)
data/README.md CHANGED
@@ -9,8 +9,8 @@ CoreMerchant is a library for customer, product, and subscription management in
9
9
  - [X] Add customer behavior
10
10
  - [X] Add initializer generator
11
11
  - [X] Add SubscriptionPlan model
12
- - [ ] Add Subscription model
13
- - [ ] Add subscription management service
12
+ - [X] Add Subscription model
13
+ - [ ] Implement subscription manager and callbacks
14
14
  - [ ] Add sidekiq jobs for subscription management
15
15
  - [ ] Add Invoice model
16
16
  - [ ] Add billing and invoicing service
@@ -37,6 +37,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
37
37
  spec.add_dependency "rails", "~> 7.0"
38
38
 
39
39
  spec.add_development_dependency "database_cleaner"
40
+ spec.add_development_dependency "factory_bot"
40
41
  spec.add_development_dependency "generator_spec", "~> 0.9.4"
41
42
  spec.add_development_dependency "railties", "~> 7.0"
42
43
  spec.add_development_dependency "rspec-rails", "~> 5.0"
@@ -6,18 +6,21 @@ module CoreMerchant
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- end
9
+ def core_merchant_customer_id
10
+ id
11
+ end
10
12
 
11
- def core_merchant_customer_id
12
- id
13
- end
13
+ def core_merchant_customer_email
14
+ email
15
+ end
14
16
 
15
- def core_merchant_customer_email
16
- email
17
- end
17
+ def core_merchant_customer_name
18
+ name if respond_to?(:name)
19
+ end
18
20
 
19
- def core_merchant_customer_name
20
- name if respond_to?(:name)
21
+ def subscriptions
22
+ CoreMerchant::Subscription.where(customer_id: core_merchant_customer_id)
23
+ end
21
24
  end
22
25
  end
23
26
  end
@@ -99,12 +99,6 @@ module CoreMerchant
99
99
 
100
100
  private
101
101
 
102
- def set_period_dates
103
- self.start_date ||= Time.current
104
- self.current_period_start ||= start_date
105
- self.current_period_end ||= start_date + subscription_plan.duration_in_date
106
- end
107
-
108
102
  def end_date_after_start_date
109
103
  errors.add(:end_date, "must be after the `start date") if end_date <= start_date
110
104
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CoreMerchant
4
+ # Include this module in your application to listen for subscription events.
5
+ module SubscriptionListener
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ def on_test_event_received
10
+ puts "Test event received by CoreMerchant::SubscriptionListener. Override this method in your application."
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CoreMerchant
4
+ # Manages subscriptions in CoreMerchant.
5
+ class SubscriptionManager
6
+ attr_reader :listeners
7
+
8
+ def initialize
9
+ @listeners = []
10
+ end
11
+
12
+ def add_listener(listener)
13
+ @listeners << listener
14
+ end
15
+
16
+ def notify_test_event
17
+ @listeners.each do |listener|
18
+ listener.on_test_event_received if listener.respond_to?(:on_test_event_received)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CoreMerchant
4
- VERSION = "0.6.2"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/core_merchant.rb CHANGED
@@ -5,6 +5,8 @@ require "active_support/concern"
5
5
  require_relative "core_merchant/version"
6
6
  require_relative "core_merchant/customer_behavior"
7
7
  require_relative "core_merchant/subscription_plan"
8
+ require_relative "core_merchant/subscription_manager"
9
+ require_relative "core_merchant/subscription_listener"
8
10
 
9
11
  # CoreMerchant module
10
12
  module CoreMerchant
@@ -19,16 +21,25 @@ module CoreMerchant
19
21
 
20
22
  def configure
21
23
  yield(configuration)
24
+
25
+ return unless configuration.subscription_listener_class
26
+
27
+ listener = configuration.subscription_listener_class.constantize.new
28
+ subscription_manager.add_listener(listener)
22
29
  end
23
30
 
24
31
  def customer_class
25
32
  configuration.customer_class.constantize
26
33
  end
34
+
35
+ def subscription_manager
36
+ @subscription_manager ||= CoreMerchant::SubscriptionManager.new
37
+ end
27
38
  end
28
39
 
29
40
  # Used to configure CoreMerchant.
30
41
  class Configuration
31
- attr_accessor :customer_class
42
+ attr_accessor :customer_class, :subscription_listener_class
32
43
 
33
44
  def initialize
34
45
  @customer_class = "CoreMerchant::Customer"
@@ -10,12 +10,8 @@ module CoreMerchant
10
10
 
11
11
  source_root File.expand_path("templates", __dir__)
12
12
 
13
- class_option :customer_class, type: :string, required: true,
14
- desc: "Name of your existing customer class, e.g. User"
15
-
16
13
  def copy_initializer
17
- @customer_class = options[:customer_class].classify
18
- template "core_merchant.erb", "config/initializers/core_merchant.rb"
14
+ template "core_merchant.rb", "config/initializers/core_merchant.rb"
19
15
  end
20
16
 
21
17
  def copy_locales
@@ -36,20 +32,22 @@ module CoreMerchant
36
32
 
37
33
  def show_post_install
38
34
  say "CoreMerchant has been successfully installed.", :green
39
- say <<~MESSAGE
40
- Customer class: #{@customer_class}. Please update this model to include the CoreMerchant::CustomerBehavior module.
35
+ next_steps = <<~MESSAGE
36
+ Next steps:
37
+ 1. Set the customer class in the initializer file (config/initializers/core_merchant.rb) to the class you want to use for customers.
38
+ 2. Create a subscription listener class (should include CoreMerchant::SubscriptionListener) in your app and set this class in the initializer file (config/initializers/core_merchant.rb) to the class you want to use for subscription listeners.
39
+ 3. Run `rails db:migrate` to create the subscription and subscription plan tables.
41
40
  MESSAGE
42
- say "Please run `rails db:migrate` to create the subscription and subscription plan tables.", :yellow
41
+ say next_steps, :yellow
43
42
  end
44
43
 
45
44
  def self.banner
46
- "rails generate core_merchant:install --customer_class=User"
45
+ "rails generate core_merchant:install"
47
46
  end
48
47
 
49
48
  def self.description
50
49
  <<~DESC
51
- Installs CoreMerchant into your application with the specified customer class.
52
- This could be User, Customer, or any other existing model in your application that represents a customer."
50
+ Installs CoreMerchant into your application. This generator will create an initializer file, migration files for the subscription and subscription plan tables, and a locale file."
53
51
  DESC
54
52
  end
55
53
  end
@@ -0,0 +1,26 @@
1
+ # Created by: rails generate core_merchant:install
2
+
3
+ CoreMerchant.configure do |config|
4
+ # Set the class that represents a customer in your application.
5
+ # This class must include the CoreMerchant::CustomerBehavior module as such:
6
+ # class User < ApplicationRecord
7
+ # include CoreMerchant::CustomerBehavior
8
+ # ...
9
+ # end
10
+ config.customer_class = "User"
11
+
12
+ # Set the class that will receive subscription notifications/
13
+ # This class must include the CoreMerchant::SubscriptionListener module.
14
+ # class SubscriptionListener
15
+ # include CoreMerchant::SubscriptionListener
16
+ # def on_subscription_created(subscription)
17
+ # ...
18
+ # end
19
+ # ...
20
+ # end
21
+ # This class will receive notifications when a subscription is created, updated, canceled etc.
22
+ #
23
+ # To test notifications to this class, you can override `on_test_event_received` method
24
+ # and call `CoreMerchant.subscription_manager.notify_test_event`.
25
+ config.subscription_listener_class = "SubscriptionListener"
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: core_merchant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seyithan Teymur
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_bot
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: generator_spec
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -133,11 +147,13 @@ files:
133
147
  - lib/core_merchant/concerns/subscription_state_machine.rb
134
148
  - lib/core_merchant/customer_behavior.rb
135
149
  - lib/core_merchant/subscription.rb
150
+ - lib/core_merchant/subscription_listener.rb
151
+ - lib/core_merchant/subscription_manager.rb
136
152
  - lib/core_merchant/subscription_plan.rb
137
153
  - lib/core_merchant/version.rb
138
154
  - lib/generators/core_merchant/install_generator.rb
139
155
  - lib/generators/core_merchant/templates/core_merchant.en.yml
140
- - lib/generators/core_merchant/templates/core_merchant.erb
156
+ - lib/generators/core_merchant/templates/core_merchant.rb
141
157
  - lib/generators/core_merchant/templates/migrate/create_core_merchant_subscription_plans.erb
142
158
  - lib/generators/core_merchant/templates/migrate/create_core_merchant_subscriptions.erb
143
159
  - sig/core_merchant.rbs
@@ -1,5 +0,0 @@
1
- # Created by: rails generate core_merchant:install --customer_class=<%= @customer_class %>
2
-
3
- CoreMerchant.configure do |config|
4
- config.customer_class = "<%= @customer_class %>"
5
- end