Skip to content

Commit 167e869

Browse files
committed
Define API for classes utilizing OmniAuth::Identity::Model
1 parent dd2c7b3 commit 167e869

File tree

10 files changed

+114
-35
lines changed

10 files changed

+114
-35
lines changed

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require:
77
- 'rubocop-performance'
88
- 'rubocop-rake'
99
- 'rubocop-rspec'
10+
- 'rubocop-sequel'
1011

1112
AllCops:
1213
NewCops: enable
@@ -25,3 +26,9 @@ Metrics/BlockLength:
2526
- shared_examples_for
2627
- namespace
2728
- draw
29+
30+
Sequel/SaveChanges:
31+
Enabled: false
32+
33+
Lint/UselessMethodDefinition:
34+
Enabled: false

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Metrics/AbcSize:
4040
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
4141
# IgnoredMethods: refine
4242
Metrics/BlockLength:
43-
Max: 27
43+
Max: 28
4444

4545
# Offense count: 1
4646
# Configuration parameters: CountComments, CountAsOne.

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
- Fix breaking changes introduced by [#86's](https://github.com/omniauth/omniauth-identity/pull/86) introduction of `:on_validation`
12+
1113
## [3.0.4] - 2021-02-14
1214

1315
### Added

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ group :development, :test do
3939
gem 'rubocop-performance', platform: :mri
4040
gem 'rubocop-rake', platform: :mri
4141
gem 'rubocop-rspec', platform: :mri
42+
gem 'rubocop-sequel', platform: :mri
4243

4344
gem 'simplecov', '~> 0.21', platform: :mri
4445
end

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,21 @@ always break things!
249249

250250
From the code - here are the options we have for you, a couple of which are documented above, and the rest are documented... in the specs we hope!?
251251
```
252-
option :fields, %i[name email]
252+
option :fields, %i[name email]
253253
254254
# Primary Feature Switches:
255-
option :enable_registration, true # See #other_phase and #request_phase
256-
option :enable_login, true # See #other_phase
255+
option :enable_registration, true # See #other_phase and #request_phase
256+
option :enable_login, true # See #other_phase
257257
258258
# Customization Options:
259-
option :on_login, nil # See #request_phase
260-
option :on_validation, nil # See #registration_phase
261-
option :on_registration, nil # See #registration_phase
262-
option :on_failed_registration, nil # See #registration_phase
263-
option :locate_conditions, ->(req) { { model.auth_key => req['auth_key'] } }
259+
option :on_login, nil # See #request_phase
260+
option :on_validation, nil # See #registration_phase
261+
option :on_registration, nil # See #registration_phase
262+
option :on_failed_registration, nil # See #registration_phase
263+
option :locate_conditions, ->(req) { { model.auth_key => req['auth_key'] } }
264264
```
265265

266-
Please contribute some documentation if you have the gumption! The maintainer's time is limited, and sometimes the authors of PRs with new options don't update the _this_ readme. 😭
266+
Please contribute some documentation if you have the gumption! The maintainer's time is limited, and sometimes the authors of PRs with new options don't update the _this_ readme. 😭
267267

268268
## License
269269

lib/omniauth/identity/model.rb

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,13 @@ module Identity
88
# abstract must be implemented in the including class for things to
99
# work properly.
1010
module Model
11+
SCHEMA_ATTRIBUTES = %w[name email nickname first_name last_name location description image phone].freeze
12+
1113
def self.included(base)
1214
base.extend ClassMethods
1315
end
1416

1517
module ClassMethods
16-
# Locate an identity given its unique login key.
17-
#
18-
# @abstract
19-
# @param [String] key The unique login key.
20-
# @return [Model] An instance of the identity model class.
21-
def locate(key)
22-
raise NotImplementedError
23-
end
24-
2518
# Authenticate a user with the given key and password.
2619
#
2720
# @param [String] key The unique login key provided for a given identity.
@@ -43,6 +36,67 @@ def auth_key(method = false)
4336

4437
@auth_key || 'email'
4538
end
39+
40+
### Singleton API for classes utilizing the OmniAuth::Identity::Model
41+
#
42+
# * create(*args)
43+
# * locate(key)
44+
45+
# Persists a new Identity object to the ORM.
46+
# Defaults to calling super. Override as needed per ORM.
47+
#
48+
# @abstract
49+
# @param [Hash] args Attributes of the new instance.
50+
# @return [Model] An instance of the identity model class.
51+
def create(*args)
52+
raise NotImplementedError unless defined?(super)
53+
54+
super
55+
end
56+
57+
# Locate an identity given its unique login key.
58+
#
59+
# @abstract
60+
# @param [String] key The unique login key.
61+
# @return [Model] An instance of the identity model class.
62+
def locate(key)
63+
raise NotImplementedError
64+
end
65+
#
66+
### END Singleton API for classes utilizing the OmniAuth::Identity::Model
67+
end
68+
69+
### Instance API for classes utilizing the OmniAuth::Identity::Model
70+
#
71+
# * save
72+
# * persisted?
73+
# * authenticate(password)
74+
# * uid
75+
# * auth_key - Normally defined by the macro `auth_key`
76+
# * auth_key=(value) - Normally defined by the macro `auth_key`
77+
#
78+
79+
# Persists a new Identity object to the ORM.
80+
# Default raises an error. Override as needed per ORM.
81+
#
82+
# @since 3.0.5
83+
# @abstract
84+
# @return [Model] An instance of the identity model class.
85+
def save
86+
raise NotImplementedError unless defined?(super)
87+
88+
super
89+
end
90+
91+
# Checks if the Identity object is persisted in the ORM.
92+
# Defaults to calling super. Override as needed per ORM.
93+
#
94+
# @abstract
95+
# @return [true or false] true if object exists, false if not.
96+
def persisted?
97+
raise NotImplementedError unless defined?(super)
98+
99+
super
46100
end
47101

48102
# Returns self if the provided password is correct, false
@@ -55,22 +109,6 @@ def authenticate(password)
55109
raise NotImplementedError
56110
end
57111

58-
SCHEMA_ATTRIBUTES = %w[name email nickname first_name last_name location description image phone].freeze
59-
# A hash of as much of the standard OmniAuth schema as is stored
60-
# in this particular model. By default, this will call instance
61-
# methods for each of the attributes it needs in turn, ignoring
62-
# any for which `#respond_to?` is `false`.
63-
#
64-
# If `first_name`, `nickname`, and/or `last_name` is provided but
65-
# `name` is not, it will be automatically calculated.
66-
#
67-
# @return [Hash] A string-keyed hash of user information.
68-
def info
69-
SCHEMA_ATTRIBUTES.each_with_object({}) do |attribute, hash|
70-
hash[attribute] = send(attribute) if respond_to?(attribute)
71-
end
72-
end
73-
74112
# An identifying string that must be globally unique to the
75113
# application. Defaults to stringifying the `id` method.
76114
#
@@ -113,6 +151,23 @@ def auth_key=(value)
113151
raise NotImplementedError
114152
end
115153
end
154+
#
155+
### END Instance API for classes utilizing the OmniAuth::Identity::Model
156+
157+
# A hash of as much of the standard OmniAuth schema as is stored
158+
# in this particular model. By default, this will call instance
159+
# methods for each of the attributes it needs in turn, ignoring
160+
# any for which `#respond_to?` is `false`.
161+
#
162+
# If `first_name`, `nickname`, and/or `last_name` is provided but
163+
# `name` is not, it will be automatically calculated.
164+
#
165+
# @return [Hash] A string-keyed hash of user information.
166+
def info
167+
SCHEMA_ATTRIBUTES.each_with_object({}) do |attribute, hash|
168+
hash[attribute] = send(attribute) if respond_to?(attribute)
169+
end
170+
end
116171
end
117172
end
118173
end

lib/omniauth/identity/models/couch_potato.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module OmniAuth
66
module Identity
77
module Models
88
# can not be named CouchPotato since there is a class with that name
9+
# NOTE: CouchPotato is based on ActiveModel.
910
module CouchPotatoModule
1011
def self.included(base)
1112
base.class_eval do

lib/omniauth/identity/models/mongoid.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module OmniAuth
66
module Identity
77
module Models
8+
# NOTE: Mongoid is based on ActiveModel.
89
module Mongoid
910
def self.included(base)
1011
base.class_eval do

lib/omniauth/identity/models/no_brainer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module OmniAuth
66
module Identity
77
module Models
88
# http://nobrainer.io/ an ORM for RethinkDB
9+
# NOTE: NoBrainer is based on ActiveModel.
910
module NoBrainer
1011
def self.included(base)
1112
base.class_eval do

lib/omniauth/identity/models/sequel.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module OmniAuth
66
module Identity
77
module Models
88
# http://sequel.jeremyevans.net/ an SQL ORM
9+
# NOTE: Sequel is *not* based on ActiveModel, but supports the API we need, except for `persisted`:
10+
# * create
11+
# * save, but save is deprecated in favor of `save_changes`
912
module Sequel
1013
def self.included(base)
1114
base.class_eval do
@@ -29,6 +32,14 @@ def self.auth_key=(key)
2932
def self.locate(search_hash)
3033
where(search_hash).first
3134
end
35+
36+
def persisted?
37+
exists?
38+
end
39+
40+
def save
41+
save_changes
42+
end
3243
end
3344
end
3445
end

0 commit comments

Comments
 (0)