Skip to content

Commit

Permalink
Add as: :authenticatable to sessions association (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
pusewicz authored and mikker committed Jun 6, 2018
1 parent 51ccb1c commit 698a71b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/passwordless/model_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module ModelHelpers
# field name (e.g. `:email`)
# @param field [string] email submitted by user.
def passwordless_with(field)
has_many :passwordless_sessions, class_name: 'Passwordless::Session'
has_many :passwordless_sessions,
class_name: 'Passwordless::Session',
as: :authenticatable

define_singleton_method(:passwordless_email_field) { field }
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/passwordless/sessions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ two:
user_agent: MyText
remote_addr: MyString
token: MyString

john:
timeout_at: 2017-11-04 23:17:35
expires_at: 2017-11-04 23:17:35
user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
remote_addr: 127.0.0.1
token: 3veDCdFw4VedgZtZZkCJ7um7rsc2bFNCZGB51Iih024
authenticatable: john (User)
3 changes: 3 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
alice:
email: alice@example.com

john:
email: john@example.com
10 changes: 10 additions & 0 deletions test/models/authenticatable_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require 'test_helper'

class AuthenticatableTest < ActiveSupport::TestCase
test '#passwordless_sessions' do
user = users(:john)
assert_equal [passwordless_sessions(:john)], user.passwordless_sessions
end
end

0 comments on commit 698a71b

Please sign in to comment.