active_registration 0.1.2 → 0.1.3

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: 5d91f57419970f798df8aadc2d7fcebf139fdd1f9d215f845389ecc744cbfd0f
4
- data.tar.gz: 9c4149954450c9fd6967e9823e3eed6574e7ae2940da157b870db56cf0f450ac
3
+ metadata.gz: fe9df96cb6561a6aeee5ea549eae7a609c6f968e3a79bec6181a608e2a65a955
4
+ data.tar.gz: 90c93712d5c449df39fee21ddcd97761eac195e63a8628e5dbccef2fca337574
5
5
  SHA512:
6
- metadata.gz: 9928062ea9157a3f3232ea575b2fd48cd3d4312b216c974e491964a8753ead2cd2b478a82e39ea786e7307330bcb6e46d188a813b12008236b48da333057f10a
7
- data.tar.gz: e7bbe018225831ef611ac27bbf42b8536f66dc31d734dee4581af927bac0e17bc46ad89baed73164c380d5ad1e167f2e9e87f627425c6e9105e005726e986d2a
6
+ metadata.gz: d643d3a49acbf71a9f6afae0ee7b40edb7fa32c846becce57477bb110e5d0832bab0ae50db4902f8b6249c8503f6c80c36e024b1b4c2db1c10f09661f9a3f511
7
+ data.tar.gz: bf5abfef36157166fd38571b609f3f0b7bb0fdfac7465e29b9e2ca73f555ba18237c15bec02eb79d611e63b9265031b7e205a02065a11a285e15ddae21da694d
data/README.md CHANGED
@@ -34,11 +34,49 @@ Run the installation generator:
34
34
  ```bash
35
35
  rails generate active_registration:install
36
36
  ```
37
+ This generator will:
38
+ - Add necessary fields to your User model (confirmation_token, confirmation_sent_at, confirmed_at)
39
+ - Create a RegistrationsController for handling user registration
40
+ - Generate view templates for registration forms
41
+ - Add routes for registration and confirmation
42
+ - Create a ConfirmationMailer and associated views
43
+ - Inject necessary methods into your User model
37
44
 
38
45
  Apply database migrations:
39
46
  ```bash
40
47
  rails db:migrate
41
48
  ```
49
+ ## What the Generator Does
50
+
51
+ The `active_registration:install` generator performs the following actions:
52
+
53
+ 1. **Database Migration**: Adds confirmation-related fields to your users table:
54
+ - `confirmation_token`: A unique token for email confirmation
55
+ - `confirmation_sent_at`: When the confirmation email was sent
56
+ - `confirmed_at`: When the user confirmed their email
57
+
58
+ 2. **Controller Generation**: Creates a RegistrationsController that handles:
59
+ - New user registration
60
+ - Email confirmation
61
+
62
+ 3. **View Generation**: Creates view templates for:
63
+ - Registration form
64
+ - Confirmation emails
65
+
66
+ 4. **Route Configuration**: Adds routes for registration and confirmation:
67
+ ```ruby
68
+ resource :registration, only: [ :new, :create ] do
69
+ get :confirm, on: :collection
70
+ end
71
+ ```
72
+
73
+ 5. **Mailer Generation**: Creates a ConfirmationMailer for sending confirmation emails
74
+
75
+ 6. **User Model Extension**: Adds methods to your User model:
76
+ - `confirm!`: Confirms a user's email
77
+ - `confirmed?`: Checks if a user is confirmed
78
+ - `confirmation_period_valid?`: Checks if the confirmation token is still valid
79
+ - `generate_confirmation_token`: Generates a secure confirmation token
42
80
 
43
81
  ## Configuration
44
82
  ### Development Environment
@@ -1,3 +1,3 @@
1
1
  module ActiveRegistration
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -2,5 +2,4 @@ require "active_registration/version"
2
2
  require "active_registration/engine"
3
3
 
4
4
  module ActiveRegistration
5
- autoload :UserExtensions, "active_registration/user_extensions"
6
5
  end
@@ -20,8 +20,6 @@ module ActiveRegistration
20
20
  end
21
21
 
22
22
  def add_routes
23
- # TODO implement edit and update in the future
24
- # resource :registration, only: [:new, :create, :edit, :update] do
25
23
  route <<~ROUTE
26
24
  resource :registration, only: [ :new, :create ] do
27
25
  get :confirm, on: :collection
@@ -34,15 +32,90 @@ module ActiveRegistration
34
32
  directory "views/confirmation_mailer", "app/views/confirmation_mailer"
35
33
  end
36
34
 
37
- def inject_user_extension
35
+ def inject_user_methods
38
36
  user_model_path = "app/models/user.rb"
39
37
  return unless File.exist?(user_model_path)
40
38
 
41
- inject_into_file user_model_path, after: "class User < ApplicationRecord\n" do
42
- " include ActiveRegistration::UserExtensions\n"
39
+ content = File.read(user_model_path)
40
+
41
+ if content.include?("validates :email_address, presence: true, uniqueness: true") &&
42
+ content.include?("def confirm!") &&
43
+ content.include?("def generate_confirmation_token")
44
+ say "User model already contains active_registration methods, skipping injection.", :yellow
45
+ return
43
46
  end
47
+
48
+ inject_validations_and_hooks(user_model_path)
49
+ inject_all_methods(user_model_path)
44
50
  rescue Errno::ENOENT
45
- say "User model not found. Add 'include ActiveRegistration::UserExtensions' to your User model.", :yellow
51
+ say "User model not found. Please add the registration methods to your User model manually.", :yellow
52
+ end
53
+
54
+ private
55
+
56
+ def find_method_end(lines, method_start_index)
57
+ depth = 1 # We're already inside the method definition
58
+ (method_start_index + 1...lines.length).each do |i|
59
+ line = lines[i]
60
+ depth += 1 if line.match(/^\s*(def|class|module|if|unless|case|begin|do\s*$|do\s*\||while|for)\b/)
61
+ depth -= 1 if line.match(/^\s*end\s*$/)
62
+ return i if depth == 0 # Found the matching end for our method
63
+ end
64
+ lines.length - 1
65
+ end
66
+
67
+ def inject_all_methods(user_model_path)
68
+ inject_into_file user_model_path, before: /^end\s*$/ do
69
+ <<RUBY
70
+
71
+ def confirm!
72
+ update(confirmed_at: Time.current, confirmation_token: nil)
73
+ end
74
+
75
+ def confirmed?
76
+ confirmed_at.present?
77
+ end
78
+
79
+ def confirmation_period_valid?
80
+ confirmation_sent_at >= 24.hours.ago
81
+ end
82
+
83
+ private
84
+
85
+ def generate_confirmation_token
86
+ self.confirmation_token = SecureRandom.urlsafe_base64
87
+ self.confirmation_sent_at = Time.current
88
+ end
89
+ RUBY
90
+ end
91
+ end
92
+
93
+ def inject_validations_and_hooks(user_model_path)
94
+ content = File.read(user_model_path)
95
+
96
+ if content.match(/^\s*(validates|before_|after_|around_)/)
97
+ last_validation_line = content.lines.each_with_index.reverse_each.find do |line, _|
98
+ line.match(/^\s*(validates|before_|after_|around_)/)
99
+ end
100
+
101
+ if last_validation_line
102
+ inject_into_file user_model_path, after: last_validation_line[0] do
103
+ <<RUBY
104
+ validates :email_address, presence: true, uniqueness: true
105
+ before_create :generate_confirmation_token
106
+ RUBY
107
+ end
108
+ return
109
+ end
110
+ end
111
+
112
+ inject_into_file user_model_path, after: "class User < ApplicationRecord\n" do
113
+ <<RUBY
114
+ validates :email_address, presence: true, uniqueness: true
115
+ before_create :generate_confirmation_token
116
+
117
+ RUBY
118
+ end
46
119
  end
47
120
  end
48
121
  end
@@ -1,5 +1,5 @@
1
1
  class RegistrationsController < ApplicationController
2
- allow_unauthenticated_access
2
+ allow_unauthenticated_access only: %i[ new create confirm ]
3
3
 
4
4
  def new
5
5
  @user = User.new
@@ -12,7 +12,7 @@ class RegistrationsController < ApplicationController
12
12
  redirect_to root_path, notice: "Confirmation email sent!"
13
13
  else
14
14
  flash[:alert] = @user.errors.full_messages.join(", ")
15
- render :new, status: :unprocessable_entity
15
+ render :new, status: :unprocessable_content
16
16
  end
17
17
  end
18
18
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_registration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salanoid
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
10
+ date: 2025-09-09 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -36,7 +36,6 @@ files:
36
36
  - Rakefile
37
37
  - lib/active_registration.rb
38
38
  - lib/active_registration/engine.rb
39
- - lib/active_registration/user_extensions.rb
40
39
  - lib/active_registration/version.rb
41
40
  - lib/generators/active_registration/install_generator.rb
42
41
  - lib/generators/active_registration/templates/add_active_registration_fields_to_users.rb
@@ -65,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
64
  - !ruby/object:Gem::Version
66
65
  version: '0'
67
66
  requirements: []
68
- rubygems_version: 3.6.9
67
+ rubygems_version: 3.6.2
69
68
  specification_version: 4
70
69
  summary: A simple gem that adds generators for sign up Rails Authentication Generator.
71
70
  test_files: []
@@ -1,29 +0,0 @@
1
- module ActiveRegistration
2
- module UserExtensions
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- validates :email_address, presence: true, uniqueness: true
7
- before_create :generate_confirmation_token
8
- end
9
-
10
- def confirm!
11
- update(confirmed_at: Time.current, confirmation_token: nil)
12
- end
13
-
14
- def confirmed?
15
- confirmed_at.present?
16
- end
17
-
18
- def confirmation_period_valid?
19
- confirmation_sent_at >= 24.hours.ago
20
- end
21
-
22
- private
23
-
24
- def generate_confirmation_token
25
- self.confirmation_token = SecureRandom.urlsafe_base64
26
- self.confirmation_sent_at = Time.current
27
- end
28
- end
29
- end