refer 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 736ad28aa8233ae7dd3de16fe4f735931fa626b24f2875d2f60ac0d2791e1c05
4
- data.tar.gz: c114325825aebd3e62fc5f2a0f29cf8ec7b562b646b2597c326a6631efd5920e
3
+ metadata.gz: d3e23e2268de377fc2bf73d90fc5ede044f1a31cd37f748e925ca68f11fcb059
4
+ data.tar.gz: 27ae4e3b9d7cca116132bcc3fb5310c6a7b82352d2330f70b52b5004c76838cf
5
5
  SHA512:
6
- metadata.gz: 2929027d9b24206e3ed98c6e6b8858bc5efa9e0df3c094edd272dd80c318f764fe83761a6540d045faf6f9628adf537e94109e4fc88b9108ffb2b71d91fa3969
7
- data.tar.gz: ab6a96074eba0db0bd0319c4233aae91418a506182dc93ccf6937debd1a8f909a20485fbd238defa6d3ebf261d866554783360d1c16a634c1b25e3330cfce670
6
+ metadata.gz: ee9e1dae466166dd4299e39ede4e06ea492c21c03f21ea0f4f13fc8343a1eca039c8e6d2914fbf35e8ea4354b8dda9a04057699361db91ad45e56d896e98ac6a
7
+ data.tar.gz: 0e5b8663f94465cc9ce236310f1b728f011d442f8076f6184cd7a0bb5a2480bbd81205c0ecc5a5bc81dbe611e30c672a6f7015bb0153233c3c70c98def454238
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Refer
2
2
 
3
- Referral codes for Ruby on Rails applications.
3
+ Referral codes and affiliate links for Ruby on Rails applications.
4
4
 
5
5
  ## 📦 Installation
6
6
  Add this line to your application's Gemfile:
@@ -14,9 +14,15 @@ And then execute:
14
14
  $ bundle
15
15
  ```
16
16
 
17
+ Add Refer to your controllers to store referral cookies:
18
+ ```bash
19
+ bin/rails generate refer:install
20
+ ```
21
+
17
22
  And add Refer to your model:
18
23
  ```bash
19
24
  bin/rails generate refer:model User
25
+ bin/rails db:migrate
20
26
  ```
21
27
 
22
28
  ## 🧑‍💻 Usage
@@ -42,7 +48,7 @@ user.referral_codes.create(code: "chris")
42
48
  To customize the referral code generator:
43
49
 
44
50
  ```ruby
45
- Refer.code_generator = ->(referrer) { [id, SecureRandom.alphanumeric(8).join("-") }
51
+ Refer.code_generator = ->(referrer) { [id, SecureRandom.alphanumeric(8)].join("-") }
46
52
  #=> generates codes like "1-7frb5fUw"
47
53
  ```
48
54
 
@@ -54,7 +60,15 @@ To track users, we need to stash the referral code in a cookie when present. By
54
60
 
55
61
  ```ruby
56
62
  class ApplicationController < ActionController::Base
57
- before_action :set_refer_cookie
63
+ set_referral_cookie
64
+ end
65
+ ```
66
+
67
+ Move `set_referral_cookie` to specific controllers if you'd only like cookies set in certain areas like marketing pages for example.
68
+
69
+ ```ruby
70
+ class MarketingController < ActionController::Base
71
+ set_referral_cookie except: [:about_us]
58
72
  end
59
73
  ```
60
74
 
@@ -2,14 +2,22 @@ module Refer
2
2
  module Controller
3
3
  extend ActiveSupport::Concern
4
4
 
5
- def set_refer_cookie
6
- if (code = params[Refer.param_name])
7
- cookies[Refer.cookie_name] = Refer.cookie(code)
5
+ class_methods do
6
+ def set_referral_cookie(param_name: Refer.param_name, cookie_name: Refer.cookie_name, **options)
7
+ before_action -> { set_refer_cookie(param_name: param_name, cookie_name: cookie_name) }, **options
8
8
  end
9
9
  end
10
10
 
11
- def refer(referee)
12
- Refer.refer(code: cookies[Refer.cookie_name], referee: referee)
11
+ def refer(referee, cookie_name: Refer.cookie_name)
12
+ Refer.refer(code: cookies[cookie_name], referee: referee)
13
+ end
14
+
15
+ private
16
+
17
+ def set_refer_cookie(param_name: Refer.param_name, cookie_name: Refer.cookie_name)
18
+ if (code = params[param_name])
19
+ cookies[cookie_name] = Refer.cookie(code)
20
+ end
13
21
  end
14
22
  end
15
23
  end
@@ -3,8 +3,9 @@ module Refer
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- has_many :referrals, inverse_of: :referrer, class_name: "Refer::Referral"
7
- has_one :referral, inverse_of: :referee, class_name: "Refer::Referral"
6
+ has_many :referral_codes, inverse_of: :referrer, class_name: "Refer::ReferralCode", dependent: :destroy
7
+ has_many :referrals, inverse_of: :referrer, class_name: "Refer::Referral", dependent: :destroy
8
+ has_one :referral, inverse_of: :referee, class_name: "Refer::Referral", dependent: :destroy
8
9
  delegate :referrer, to: :referral
9
10
  end
10
11
  end
@@ -1,7 +1,7 @@
1
1
  module Refer
2
2
  class ReferralCode < ApplicationRecord
3
3
  belongs_to :referrer, polymorphic: true
4
- has_many :referrals
4
+ has_many :referrals, dependent: :nullify
5
5
 
6
6
  validates :code, presence: true, uniqueness: true
7
7
 
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Adds Refer to ApplicationController to set referral cookies
3
+
4
+ Example:
5
+ bin/rails generate refer:install
6
+
7
+ This will add `sets_refer_cookie` to the ApplicationController class.
8
+
@@ -0,0 +1,7 @@
1
+ class Refer::InstallGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("templates", __dir__)
3
+
4
+ def add_refer
5
+ inject_into_class File.join("app", "controllers", "application_controller.rb"), "ApplicationController", " set_referral_cookie\n"
6
+ end
7
+ end
@@ -1,8 +1,7 @@
1
1
  Description:
2
- Explain the generator
2
+ Adds Refer associations to a model
3
3
 
4
4
  Example:
5
- bin/rails generate model Thing
5
+ bin/rails generate refer:model User
6
6
 
7
- This will create:
8
- what/will/it/create
7
+ This will add `has_referrals` to the User class.
data/lib/refer/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Refer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 6.1.0
27
- description: Referral codes for Ruby on Rails apps
27
+ description: Referral codes & affiliate links for Ruby on Rails apps
28
28
  email:
29
29
  - excid3@gmail.com
30
30
  executables: []
@@ -43,6 +43,8 @@ files:
43
43
  - config/routes.rb
44
44
  - db/migrate/20240611180738_create_refer_referrals.rb
45
45
  - db/migrate/20240611183349_create_refer_referral_codes.rb
46
+ - lib/generators/refer/install/USAGE
47
+ - lib/generators/refer/install/install_generator.rb
46
48
  - lib/generators/refer/model/USAGE
47
49
  - lib/generators/refer/model/model_generator.rb
48
50
  - lib/refer.rb
@@ -74,5 +76,5 @@ requirements: []
74
76
  rubygems_version: 3.5.11
75
77
  signing_key:
76
78
  specification_version: 4
77
- summary: Referral codes for Ruby on Rails apps
79
+ summary: Referral codes & affiliate links for Ruby on Rails apps
78
80
  test_files: []