Module: Devise::Models

Defined in:
lib/devise/models.rb,
lib/devise/models/lockable.rb,
lib/devise/models/trackable.rb,
lib/devise/models/activatable.rb,
lib/devise/models/confirmable.rb,
lib/devise/models/recoverable.rb,
lib/devise/models/timeoutable.rb,
lib/devise/models/validatable.rb,
lib/devise/models/registerable.rb,
lib/devise/models/rememberable.rb,
lib/devise/models/authenticatable.rb,
lib/devise/models/http_authenticatable.rb,
lib/devise/models/token_authenticatable.rb

Defined Under Namespace

Modules: Activatable, Authenticatable, Confirmable, HttpAuthenticatable, Lockable, Recoverable, Registerable, Rememberable, Timeoutable, TokenAuthenticatable, Trackable, Validatable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config(mod, *accessors) ⇒ Object

Creates configuration values for Devise and for the given module.

Devise::Models.config(Devise::Authenticable, :stretches, 10)

The line above creates:

1) An accessor called Devise.stretches, which value is used by default;

2) Some class methods for your model Model.stretches and Model.stretches=
   which have higher priority than Devise.stretches;

3) And an instance method stretches.

To add the class methods you need to have a module ClassMethods defined inside the given class.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/devise/models.rb', line 30

def self.config(mod, *accessors) #:nodoc:
  accessors.each do |accessor|
    mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
      def #{accessor}
        if defined?(@#{accessor})
          @#{accessor}
        elsif superclass.respond_to?(:#{accessor})
          superclass.#{accessor}
        else
          Devise.#{accessor}
        end
      end

      def #{accessor}=(value)
        @#{accessor} = value
      end
    METHOD
  end
end

Instance Method Details

#add_error_on(record, attribute, error, add_default = true) ⇒ Object

Wraps add error logic in a method that works for different frameworks.



102
103
104
105
106
107
108
109
110
# File 'lib/devise/models.rb', line 102

def add_error_on(record, attribute, error, add_default=true)
  options = add_default ? { :default => error.to_s.gsub("_", " ") } : {}

  begin
    record.errors.add(attribute, error, options)
  rescue ArgumentError
    record.errors.add(attribute, error.to_s.gsub("_", " "))
  end
end

#devise(*modules) ⇒ Object

Include the chosen devise modules in your model:

devise :authenticatable, :confirmable, :recoverable

You can also give any of the devise configuration values in form of a hash, with specific values for this model. Please check your Devise initializer for a complete description on those values.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/devise/models.rb', line 58

def devise(*modules)
  raise "You need to give at least one Devise module" if modules.empty?
  options  = modules.extract_options!

  @devise_modules = Devise::ALL & modules.map(&:to_sym).uniq

  Devise.orm_class.included_modules_hook(self) do
    devise_modules.each do |m|
      include Devise::Models.const_get(m.to_s.classify)
    end

    options.each { |key, value| send(:"#{key}=", value) }
  end
end

#devise_modulesObject

Stores all modules included inside the model, so we are able to verify which routes are needed.



75
76
77
# File 'lib/devise/models.rb', line 75

def devise_modules
  @devise_modules ||= []
end

#find_or_initialize_with_error_by(attribute, value, error = :invalid) ⇒ Object

Find an initialize a record setting an error if it can’t be found.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/devise/models.rb', line 80

def find_or_initialize_with_error_by(attribute, value, error=:invalid)
  if value.present?
    conditions = { attribute => value }
    record = find(:first, :conditions => conditions)
  end

  unless record
    record = new

    if value.present?
      record.send(:"#{attribute}=", value)
    else
      error, skip_default = :blank, true
    end

    add_error_on(record, attribute, error, !skip_default)
  end

  record
end