Class: Auth::Model

Inherits:
Object
  • Object
show all
Includes:
BehaviorLookup
Defined in:
lib/auth/model.rb

Defined Under Namespace

Modules: Authenticated

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BehaviorLookup

#lookup_behavior

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 = {}, default_options = {})
  @options = (options || {}).dup
  send(:default_options).merge! default_options

  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 (_options = options).keys.select { |key| key.to_s == option_name }.empty?
    super
  else
    if assignment
      _options[option_name.to_sym] = args.flatten
    else
      _options[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.options[name] ||= send(default)
  end
end

Instance Method Details

#apply_options!Object



90
91
92
# File 'lib/auth/model.rb', line 90

def apply_options!
  apply_behaviors!
end

#default_optionsObject



94
95
96
97
98
99
# File 'lib/auth/model.rb', line 94

def default_options
  @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 default_options=(hash)
  default_options.merge!(hash)
end

#keyObject

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
  options[: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.

Returns:

  • (Boolean)


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 merge_options!(options)
  @options.merge! options
end

#optionsObject



40
41
42
# File 'lib/auth/model.rb', line 40

def options
  @options.reverse_merge(default_options)
end

#set_default_option(key, value) ⇒ Object



105
106
107
# File 'lib/auth/model.rb', line 105

def set_default_option(key, value)
  default_options[key] = value
end

#targetObject



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