referral_box 0.1.7 → 0.1.8

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: b232d0c39b152774ce3e521e1bfd5b49e12f4403b913391fdbdc0ef91684c07a
4
- data.tar.gz: 8623def044d02bcfe7aaac01382c25bcac0a78fc87c9cda64f24cba20825cd4f
3
+ metadata.gz: d8ba983787c512d738d87748d8aef25955fc322c491bdabfd47ad8fda6980984
4
+ data.tar.gz: 29a2e05f19614deee0edde3005fc56788efeea814c78bdd0eed3eee37953f054
5
5
  SHA512:
6
- metadata.gz: 15df1bb9559552c3648c30b667c73649b3af655296eb2206e4519779685d0d56733580d4f7d65395245911c6799985a2d432f74ade66652eea35f7f9c65c0d98
7
- data.tar.gz: 1831e708802b59d48cfa08611c0fcc444a96307f42d5e3cdd840052b855449d503e978931b6605d35a406cd0f987cb420e91d4bf7a04935c5881338113258f6f
6
+ metadata.gz: bddb9b69fa3af2ad73bbd3ff32ca2de283611f75853731bf704bcf98b976c5b61da069ff1ce4364fc5382c323fe63aefb9b4b45c5131e5fc556566dfdd3e0f5f
7
+ data.tar.gz: 76dd52bfcbff503d155135d408bb81d4ec1095c3021a642f42d7bbf2abb352ffcdc04519478765e7f0bfcc1291886dcda4b18113eb5054b6c19014cb224f1c54
data/CHANGELOG.md CHANGED
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.1.8] - 2025-01-XX
11
+
12
+ ### Added
13
+ - **Interactive Generator**: Generator now asks for model name and confirms the choice
14
+ - **Model Detection**: Automatically detects existing models in the app
15
+ - **Safe Migrations**: Migration now checks if columns exist before adding them
16
+ - **Duplicate Prevention**: Prevents adding duplicate columns and methods
17
+
18
+ ### Fixed
19
+ - **Migration Errors**: Fixed "duplicate column name" errors by adding existence checks
20
+ - **Generator Flow**: Improved user experience with interactive prompts
21
+ - **Model Flexibility**: Better support for different model names (User, Customer, Account, etc.)
22
+
10
23
  ## [0.1.7] - 2025-01-XX
11
24
 
12
25
  ### Added
@@ -11,37 +11,66 @@ module ReferralBox
11
11
  template "initializer.rb", "config/initializers/referral_box.rb"
12
12
  end
13
13
 
14
- def add_model_migration
15
- # Read the configuration to determine the model class name
16
- model_class = get_model_class_name
14
+ def ask_for_model_name
15
+ puts "\n" + "="*60
16
+ puts "ReferralBox Model Configuration"
17
+ puts "="*60
18
+
19
+ # Detect existing models
20
+ model_files = Dir.glob("app/models/*.rb")
21
+ existing_models = model_files.map { |f| File.basename(f, '.rb').classify }
22
+
23
+ if existing_models.any?
24
+ puts "\nExisting models found: #{existing_models.join(', ')}"
25
+ end
26
+
27
+ @model_class = ask("What is your user model class name? (e.g., User, Customer, Account, Member):", default: "User")
28
+
29
+ # Confirm the choice
30
+ puts "\nYou selected: #{@model_class}"
31
+ confirm = ask("Is this correct? (y/n):", default: "y")
32
+
33
+ unless confirm.downcase.start_with?('y')
34
+ puts "Please run the generator again with the correct model name."
35
+ exit
36
+ end
17
37
 
18
- if model_class.present?
19
- generate_migration = "AddReferralBoxTo#{model_class.pluralize}"
20
- migration_content = generate_migration_content(model_class)
38
+ # Update the initializer with the selected model
39
+ update_initializer_with_model(@model_class)
40
+ end
41
+
42
+ def add_model_migration
43
+ if @model_class.present?
44
+ generate_migration = "AddReferralBoxTo#{@model_class.pluralize}"
45
+ migration_content = generate_migration_content(@model_class)
21
46
 
22
47
  create_file "db/migrate/#{Time.current.strftime('%Y%m%d%H%M%S')}_#{generate_migration.underscore}.rb", migration_content
23
48
 
24
- puts "Migration generated for #{model_class} model!"
49
+ puts "\nMigration generated for #{@model_class} model!"
25
50
  puts "Run 'rails db:migrate' to apply it."
26
51
  else
27
- puts "Warning: Could not determine model class name from configuration."
52
+ puts "Warning: Could not determine model class name."
28
53
  puts "Please manually create migration for your model."
29
54
  end
30
55
  end
31
56
 
32
57
  def add_model_methods
33
- model_class = get_model_class_name
34
-
35
- if model_class.present?
36
- model_file = "app/models/#{model_class.underscore}.rb"
58
+ if @model_class.present?
59
+ model_file = "app/models/#{@model_class.underscore}.rb"
37
60
 
38
61
  if File.exist?(model_file)
39
- inject_into_file model_file, after: "class #{model_class} < ApplicationRecord" do
40
- "\n " + generate_model_methods(model_class)
62
+ # Check if methods already exist
63
+ content = File.read(model_file)
64
+ if content.include?('has_many :referral_box_transactions')
65
+ puts "ReferralBox methods already exist in #{@model_class} model."
66
+ else
67
+ inject_into_file model_file, after: "class #{@model_class} < ApplicationRecord" do
68
+ "\n " + generate_model_methods(@model_class)
69
+ end
70
+ puts "Added ReferralBox methods to #{@model_class} model."
41
71
  end
42
- puts "Added ReferralBox methods to #{model_class} model."
43
72
  else
44
- puts "Warning: #{model_file} not found. Please manually add ReferralBox methods to your #{model_class} model."
73
+ puts "Warning: #{model_file} not found. Please manually add ReferralBox methods to your #{@model_class} model."
45
74
  end
46
75
  end
47
76
  end
@@ -60,19 +89,19 @@ module ReferralBox
60
89
 
61
90
  private
62
91
 
63
- def get_model_class_name
64
- # Try to read from existing initializer first
92
+ def update_initializer_with_model(model_class)
65
93
  initializer_path = "config/initializers/referral_box.rb"
66
94
 
67
95
  if File.exist?(initializer_path)
68
96
  content = File.read(initializer_path)
69
- if match = content.match(/config\.reference_class_name\s*=\s*['"]([^'"]+)['"]/)
70
- return match[1]
71
- end
97
+ updated_content = content.gsub(
98
+ /config\.reference_class_name\s*=\s*['"][^'"]*['"]/,
99
+ "config.reference_class_name = '#{model_class}'"
100
+ )
101
+
102
+ File.write(initializer_path, updated_content)
103
+ puts "Updated initializer with #{model_class} model."
72
104
  end
73
-
74
- # Default to 'User' if not found
75
- 'User'
76
105
  end
77
106
 
78
107
  def generate_migration_content(model_class)
@@ -81,11 +110,25 @@ module ReferralBox
81
110
 
82
111
  class AddReferralBoxTo#{model_class.pluralize} < ActiveRecord::Migration[#{get_rails_version}]
83
112
  def change
84
- add_column :#{model_class.underscore.pluralize}, :referral_code, :string
85
- add_column :#{model_class.underscore.pluralize}, :tier, :string
86
- add_reference :#{model_class.underscore.pluralize}, :referrer, null: true, foreign_key: { to_table: :#{model_class.underscore.pluralize} }
113
+ # Add referral_code column if it doesn't exist
114
+ unless column_exists?(:#{model_class.underscore.pluralize}, :referral_code)
115
+ add_column :#{model_class.underscore.pluralize}, :referral_code, :string
116
+ end
117
+
118
+ # Add tier column if it doesn't exist
119
+ unless column_exists?(:#{model_class.underscore.pluralize}, :tier)
120
+ add_column :#{model_class.underscore.pluralize}, :tier, :string
121
+ end
122
+
123
+ # Add referrer reference if it doesn't exist
124
+ unless column_exists?(:#{model_class.underscore.pluralize}, :referrer_id)
125
+ add_reference :#{model_class.underscore.pluralize}, :referrer, null: true, foreign_key: { to_table: :#{model_class.underscore.pluralize} }
126
+ end
87
127
 
88
- add_index :#{model_class.underscore.pluralize}, :referral_code, unique: true
128
+ # Add unique index on referral_code if it doesn't exist
129
+ unless index_exists?(:#{model_class.underscore.pluralize}, :referral_code)
130
+ add_index :#{model_class.underscore.pluralize}, :referral_code, unique: true
131
+ end
89
132
  end
90
133
  end
91
134
  MIGRATION
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReferralBox
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: referral_box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kapil Dev Pal