Class: Auth::Model
Defined Under Namespace
Modules: Authenticated
Class Method Summary collapse
Instance Method Summary collapse
- #apply_options! ⇒ Object
- #default_options ⇒ Object
- #default_options=(hash) ⇒ Object
-
#initialize(name_or_constant, options = {}, default_options = {}) ⇒ Model
constructor
Options include: :behaviors => an array of behaviors to override Auth.configuration.behaviors :password_update_frequency => a time (ie 30.days) to override Auth.configuration.password_update_frequency :skip_routes => true if you do not want to generate routes (useful if you need to map them yourself).
-
#key ⇒ Object
The key upon which this model is to be authenticated.
-
#matches?(name_or_constant) ⇒ Boolean
Returns true if the specified String, Symbol or constant resolves to the same constant affected by this Model.
-
#merge_options!(options) ⇒ Object
Merges the specified hash of options with this Model’s hash of options.
-
#method_missing(name, *args, &block) ⇒ Object
check missing methods to see if they’re looking for configuration options.
- #options ⇒ Object
- #set_default_option(key, value) ⇒ Object
- #target ⇒ Object
Methods included from BehaviorLookup
Constructor Details
#initialize(name_or_constant, options = {}, default_options = {}) ⇒ Model
Options include:
:behaviors => an array of behaviors to override Auth.configuration.behaviors
:password_update_frequency => a time (ie 30.days) to override Auth.configuration.password_update_frequency
:skip_routes => true if you do not want to generate routes (useful if you need to map them yourself).
:key => the key upon which to authenticate (the username, basically)
:with => a regular expression used to validate the password
:accounts_controller => the name of the controller to route to for creating accounts, deleting them, etc.
:sessions_controller => the name of the controller to route to for logging in, logging out, etc.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/auth/model.rb', line 53 def initialize(name_or_constant, = {}, = {}) @options = ( || {}).dup send(:default_options).merge! begin @target = resolve(name_or_constant).name rescue NameError # we fail silently because the user might not have generated the model yet. That would mean they're trying # to do it now. @target = name_or_constant.to_s.camelize end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
check missing methods to see if they’re looking for configuration options
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/auth/model.rb', line 18 def method_missing(name, *args, &block) #:nodoc: option_name, assignment = (name.to_s.gsub(/\=$/, '')), $~ # lose the equals sign, and remember if it existed if ( = ).keys.select { |key| key.to_s == option_name }.empty? super else if assignment [option_name.to_sym] = args.flatten else [option_name.to_sym] end end end |
Class Method Details
.option(name, default = name) ⇒ Object
11 12 13 14 15 |
# File 'lib/auth/model.rb', line 11 def self.option(name, default = name) define_method(name) do self.[name] ||= send(default) end end |
Instance Method Details
#apply_options! ⇒ Object
90 91 92 |
# File 'lib/auth/model.rb', line 90 def apply_behaviors! end |
#default_options ⇒ Object
94 95 96 97 98 99 |
# File 'lib/auth/model.rb', line 94 def @default_options ||= { :key => :email, :with => :password }.merge(Auth.configuration.to_hash) end |
#default_options=(hash) ⇒ Object
101 102 103 |
# File 'lib/auth/model.rb', line 101 def (hash) .merge!(hash) end |
#key ⇒ Object
The key upon which this model is to be authenticated. (The username.) Defaults to :email
81 82 83 |
# File 'lib/auth/model.rb', line 81 def key [:key] end |
#matches?(name_or_constant) ⇒ Boolean
Returns true if the specified String, Symbol or constant resolves to the same constant affected by this Model.
76 77 78 |
# File 'lib/auth/model.rb', line 76 def matches?(name_or_constant) target == resolve(name_or_constant) end |
#merge_options!(options) ⇒ Object
Merges the specified hash of options with this Model’s hash of options. Same as model.options.merge!(options)
86 87 88 |
# File 'lib/auth/model.rb', line 86 def () @options.merge! end |
#options ⇒ Object
40 41 42 |
# File 'lib/auth/model.rb', line 40 def @options.reverse_merge() end |
#set_default_option(key, value) ⇒ Object
105 106 107 |
# File 'lib/auth/model.rb', line 105 def set_default_option(key, value) [key] = value end |
#target ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/auth/model.rb', line 66 def target klass = @target.constantize unless klass.include?(Auth::Model::Authenticated) klass.send(:include, Auth::Model::Authenticated) klass.sparkly_config = self end klass end |