Skip to content

Commit

Permalink
Allow referencing custom columns on new accounts
Browse files Browse the repository at this point in the history
When using `ActiveRecord::Base.instantiate`, accessing a column
attribute that wasn't present in the given hash will raise a
ActiveModel::MissingAttributeError. This is not problematic for
retrieving existing accounts, as the `account` hash will have all the
columns, but it is for new accounts.

This allows things like running model validations in a
`before_create_account` hook instead of Rodauth validations, which is
especially useful when migrating existing authentication to Rodauth.
  • Loading branch information
janko committed Jan 27, 2024
1 parent edb3ea8 commit 09f2dff
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

* Allow referencing custom column attributes on `rails_account` during account creation (@janko)

* Drop support for Ruby 2.3 and 2.4 (@janko)

## 1.13.0 (2023-12-25) :christmas_tree:
Expand Down
6 changes: 5 additions & 1 deletion lib/rodauth/rails/feature/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def session

def instantiate_rails_account
if defined?(ActiveRecord::Base) && rails_account_model < ActiveRecord::Base
rails_account_model.instantiate(account.stringify_keys)
if account[account_id_column]
rails_account_model.instantiate(account.stringify_keys)
else
rails_account_model.new(account)
end
elsif defined?(Sequel::Model) && rails_account_model < Sequel::Model
rails_account_model.load(account)
else
Expand Down
11 changes: 11 additions & 0 deletions test/integration/model_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "test_helper"

class ModelTest < IntegrationTest
test "allows referencing custom columns for new accounts" do
visit "/create-account"
fill_in "Login", with: "[email protected]"
fill_in "Password", with: "supersecret"
fill_in "Confirm Password", with: "supersecret"
click_on "Create Account"
end
end
1 change: 1 addition & 0 deletions test/rails_app/app/misc/rodauth_main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class RodauthMain < Rodauth::Rails::Auth
end

after_login { remember_login }
before_create_account { rails_account.username }

logout_redirect { rails_routes.root_path }
verify_account_redirect { login_redirect }
Expand Down
1 change: 1 addition & 0 deletions test/rails_app/db/migrate/20200411171322_create_rodauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def change
t.string :email, null: false
t.index :email, unique: true, where: "status IN (1, 2)"
t.string :password_hash
t.string :username
end

# Used if storing password hashes in a separate table (default)
Expand Down

0 comments on commit 09f2dff

Please sign in to comment.