rails_authentication 0.5.0 → 0.6.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: 6369064c5074eb057abc1ed169b7efbe7ae0758730dc6c764b86fee19f96b0d3
4
- data.tar.gz: 5bdc5cb01eb60fed2449f0a1544d29102c79f917faf345f86a1a86bca6b6adc6
3
+ metadata.gz: fcff113ea0549fb291f38ebe6d9ef6d7306bb2c4a444cb01d7f2bd99a21626ef
4
+ data.tar.gz: 33fb15023005e91589fb07fe846748e7c7af9876fae8b517bef98dee41994ebf
5
5
  SHA512:
6
- metadata.gz: da49badfb4b8c58c2525329479154961cb10cbd6b6d683a4dbc56171198e9ad1d1f31d097455f106d47d19332a14391ee0e4370394aa23535e85b47be6ebf0f8
7
- data.tar.gz: 4407fbc0f0c7daafc4324fb1e0ba7c366c4c810fa81b579bf464fd2f2e31b7d5e3da6c6e8741b189e0399a28660b4e0a728d80b1bfec63282eee3f8c0d204756
6
+ metadata.gz: 0c0849f4c52e57761a6d43a555b2c70712be764c96dc205ec10805ba983637777e53d1326e1b41b106c4ad7baeae258680e073aec19e47a42f08d9fce2b731f9
7
+ data.tar.gz: 8f1021d0c9e8c7a7ef71f791a83b24948af32b628154671b1dce31ad8cfe2299d5234a278412a45cff9b11fdc81201281ebbdd89ea391a49d7a41664bba14e03
data/README.md CHANGED
@@ -99,9 +99,53 @@ the generated concerns — e.g. `TimeoutableConcern::TIMEOUT_IN`, `LockableConce
99
99
  `ValidatableConcern::PASSWORD_LENGTH`, `ValidatableConcern::PASSWORD_MINIMUM_COMPLEXITY`,
100
100
  `OttConcern::OTT_CODE_LENGTH` — edit them there. Concerns keep the model clean.
101
101
 
102
- ### Generated routes
102
+ ### Inviting users
103
+
104
+ Invitable generates a `User.invite!` class method. It takes a **hash of attributes** for the new
105
+ user (not just an email address) plus an optional `invited_by:`:
103
106
 
107
+ ```ruby
108
+ User.invite!({ email_address: "new@example.com" }, invited_by: Current.user)
109
+
110
+ # Any other attributes your User requires go in the same hash
111
+ User.invite!({ email_address: "new@example.com", name: "Ada Lovelace" }, invited_by: Current.user)
104
112
  ```
113
+
114
+ `invite!` accepts symbol or string keys, or permitted `ActionController::Parameters`. It
115
+ returns the user either way, so check `persisted?` and `errors` to see whether it worked.
116
+
117
+ - **New email address**: the user is built from the attributes and gets a random temporary
118
+ password, a fresh invitation token, and `invited_by`, then is saved. On success the invitation
119
+ email is sent and the inviter's `invitations_count` goes up. On a validation failure it returns
120
+ the unsaved user with `errors` and sends nothing.
121
+ - **Existing pending invitee**: the token is refreshed and the invitation re-sent. The other
122
+ attributes are ignored.
123
+ - **Existing user who isn't a pending invitee**: the user is returned untouched, the other
124
+ attributes are ignored, and no email is sent.
125
+
126
+ Any `password` in the hash is overwritten with the temporary one, because invitees choose their
127
+ own password when they accept. Everything else is mass-assigned, so pass only attributes you've
128
+ permitted. Never pass raw `params`.
129
+
130
+ **Upgrading from the email-only signature.** Earlier versions took the email address as a string,
131
+ `User.invite!(email_address, invited_by: ...)`. That form no longer works, so wrap the address in
132
+ a hash: `User.invite!({ email_address: email_address }, invited_by: ...)`.
133
+
134
+ **Requiring more than an email address.** The generated `InvitationsController` passes
135
+ `invitation_params` (`params.permit(:email_address)`) to `invite!`. To collect more fields, add
136
+ them in three places: the `permit` list, the inputs in `app/views/invitations/new.html.erb`, and
137
+ the validations on your model:
138
+
139
+ ```ruby
140
+ # app/controllers/invitations_controller.rb
141
+ def invitation_params
142
+ params.permit(:email_address, :name)
143
+ end
144
+ ```
145
+
146
+ ### Generated routes
147
+
148
+ ```ruby
105
149
  resource :registration, only: %i[ new create edit update destroy ]
106
150
  resources :confirmations, only: %i[ new create show ], param: :token
107
151
  resources :unlocks, only: %i[ new create show ], param: :token
@@ -6,7 +6,7 @@ class InvitationsController < ApplicationController
6
6
  end
7
7
 
8
8
  def create
9
- user = User.invite!(params[:email_address], invited_by: Current.user)
9
+ user = User.invite!(invitation_params, invited_by: Current.user)
10
10
 
11
11
  if user.persisted?
12
12
  redirect_to new_invitation_path, notice: "Invitation sent to #{user.email_address}."
@@ -29,6 +29,11 @@ class InvitationsController < ApplicationController
29
29
 
30
30
  private
31
31
 
32
+ # Add any other attributes your User requires (e.g. :name) here and to the invitation form.
33
+ def invitation_params
34
+ params.permit(:email_address)
35
+ end
36
+
32
37
  def set_user_by_token
33
38
  @user = User.find_by_valid_invitation_token(params[:token])
34
39
  redirect_to new_session_path, alert: "Invitation link is invalid or has expired." unless @user
@@ -8,8 +8,9 @@ module InvitableConcern
8
8
  end
9
9
 
10
10
  class_methods do
11
- def invite!(email_address, invited_by: nil)
12
- user = find_by(email_address: email_address)
11
+ def invite!(attributes, invited_by: nil)
12
+ attributes = attributes.to_h.symbolize_keys
13
+ user = find_by(email_address: attributes[:email_address])
13
14
 
14
15
  if user
15
16
  # Re-inviting an existing pending invitee refreshes their token; anyone else
@@ -23,8 +24,8 @@ module InvitableConcern
23
24
  return user
24
25
  end
25
26
 
26
- user = new(
27
- email_address: email_address,
27
+ user = new(attributes)
28
+ user.assign_attributes(
28
29
  password: generate_temporary_password,
29
30
  invited_by: invited_by,
30
31
  invitation_token: generate_invitable_token,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsAuthentication
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Freerksen
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  - !ruby/object:Gem::Version
133
133
  version: '0'
134
134
  requirements: []
135
- rubygems_version: 4.0.16
135
+ rubygems_version: 4.0.20
136
136
  specification_version: 4
137
137
  summary: Devise-style features on top of Rails 8's built-in authentication generator
138
138
  test_files: []