refer 0.1.1 → 0.2.1

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: e93783086c4e2c9c226868f818915d002d13258e4a1dc024759921b75763fa5e
4
- data.tar.gz: 0477c633957d41edad55c6da672b2ca3fcca1b9a87ea2414e65cf0c6309a458a
3
+ metadata.gz: c2b67d5a90aba044db91c58de5ba63120d31b89e4826545e3589388eae68922a
4
+ data.tar.gz: bccf49ec61313f94f7cf842920249d0af40bda63dd632fef70694701912b1e63
5
5
  SHA512:
6
- metadata.gz: 6149724a00b0600711afc17d9a035c3f048134b41873f0b44d036cfdd4fbd787182e31f607397034f2b65279eb6130e9c5702677fa0987e32a98bd284d26c32b
7
- data.tar.gz: '0562961b931e111932027ba64bbce8f598ad2a85098ac308a874ec5429549d5e6bfdbfca1c8df00d16a7e9d08e154a8043d4d11ccf1b32ea1920e0b0f2376c4e'
6
+ metadata.gz: d96dbd8d446574bf29433a558f9b342e385a0af450d2885d37db182960ef3cb1a1fe38d7be0f9eeac7ace6eeb77b7d7117c30f606eb220726764ed6b95ea7b45
7
+ data.tar.gz: 597c07e84d93b9b6ad2d487a61ad456c20f0b4e687a8fe5b2b6e5184327e11ae79b68b7426acb7d266d047b2b6259d4c9d8f3e583e6f4e97e17ea6ca630028b7
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
@@ -1,4 +1,4 @@
1
- class CreateReferReferrals < ActiveRecord::Migration[7.2]
1
+ class CreateReferReferrals < ActiveRecord::Migration[6.1]
2
2
  def change
3
3
  create_table :refer_referrals do |t|
4
4
  t.belongs_to :referrer, polymorphic: true, null: false
@@ -1,4 +1,4 @@
1
- class CreateReferReferralCodes < ActiveRecord::Migration[7.2]
1
+ class CreateReferReferralCodes < ActiveRecord::Migration[6.1]
2
2
  def change
3
3
  create_table :refer_referral_codes do |t|
4
4
  t.belongs_to :referrer, polymorphic: true, null: false
@@ -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.1"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-12 00:00:00.000000000 Z
11
+ date: 2024-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -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
@@ -71,8 +73,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
73
  - !ruby/object:Gem::Version
72
74
  version: '0'
73
75
  requirements: []
74
- rubygems_version: 3.5.11
76
+ rubygems_version: 3.5.13
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: []