Class: Auth::Model

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

Defined Under Namespace

Modules: Authenticated

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BehaviorLookup

#lookup_behavior

Constructor Details

#initialize(name_or_constant, 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.


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/auth/model.rb', line 32

def initialize(name_or_constant, 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
  
  @options = options.reverse_merge(default_options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/auth/model.rb', line 16

def options
  @options
end

Class Method Details

.option(name, default = name) ⇒ Object



9
10
11
12
13
# File 'lib/auth/model.rb', line 9

def self.option(name, default = name)
  define_method(name) do
    self.options[name] ||= Auth.configuration.send(default)
  end
end

Instance Method Details

#apply_options!Object



68
69
70
# File 'lib/auth/model.rb', line 68

def apply_options!
  apply_behaviors!
end

#keyObject

The key upon which this model is to be authenticated. (The username.) Defaults to :email



59
60
61
# File 'lib/auth/model.rb', line 59

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)


54
55
56
# File 'lib/auth/model.rb', line 54

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)



64
65
66
# File 'lib/auth/model.rb', line 64

def merge_options!(options)
  self.options.merge! options
end

#targetObject



44
45
46
47
48
49
50
51
# File 'lib/auth/model.rb', line 44

def target
  returning @target.constantize do |klass|
    unless klass.include?(Auth::Model::Authenticated)
      klass.send(:include, Auth::Model::Authenticated)
      klass.sparkly_config = self
    end
  end
end