Module: Negroni::Models

Extended by:
ActiveSupport::Autoload
Defined in:
lib/negroni/models.rb,
lib/negroni/models/base.rb,
lib/negroni/models/lockable.rb,
lib/negroni/models/recoverable.rb,
lib/negroni/models/validatable.rb,
lib/negroni/models/omniauthable.rb,
lib/negroni/models/registerable.rb,
lib/negroni/models/authenticable.rb

Overview

Namespace for Model-specific configuration.

Defined Under Namespace

Modules: Authenticable, Base, Lockable, Omniauthable, Recoverable, Registerable, Validatable Classes: MissingAttribute

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_fields!(klass) ⇒ Void

Checks that the including class has the appropriate required fields.

Parameters:

  • klass (Class)

    the including class

Returns:

  • (Void)

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/negroni/models.rb', line 72

def self.check_fields!(klass)
  failed_attributes = []
  instance = klass.new

  klass.negroni_modules.each do |mod|
    constant = const_get(mod.to_s.classify)

    constant.required_fields(klass).each do |field|
      failed_attributes << field unless instance.respond_to?(field)
    end
  end

  return unless failed_attributes.any?
  raise Negroni::Models::MissingAttribute, failed_attributes
end

.config(mod, *accessors) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates configuration values for Negroni and for the given module.

Negroni::Models.config(Negroni::Authenticatable, :authentication_keys)

The line above creates:

1) An accessor called Negroni.authentication_keys, which value is used
   by default;

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

3) And an instance method authentication_keys.

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/negroni/models.rb', line 44

def self.config(mod, *accessors) # rubocop:disable Metrics/MethodLength
  class << mod; attr_accessor :available_configs; end
  mod.available_configs = accessors

  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
          Negroni.#{accessor}
        end
      end

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

Instance Method Details

#aa_module_hook!Object

a hook



132
133
134
# File 'lib/negroni/models.rb', line 132

def aa_module_hook!
  yield
end

#negroni(*modules) ⇒ Void

Include the given Negroni modules in your model. Authenticate is always included.

Parameters:

  • modules (Symbol, Array<Symbol>)

    the modules to include.

Returns:

  • (Void)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/negroni/models.rb', line 95

def negroni(*modules)
  options = modules.extract_options!.dup

  selected_modules = modules.map(&:to_sym).uniq.sort_by do |sym|
    Negroni::ALL.index(sym) != -1
  end

  aa_module_hook! do
    include Negroni::Models::Base

    selected_modules.each do |m|
      mod = Negroni::Models.const_get m.to_s.classify

      if mod.const_defined? 'ClassMethods'
        class_mod = mod.const_get('ClassMethods')
        extend class_mod

        if class_mod.respond_to? :available_configs
          available_configs = class_mod.available_configs

          available_configs.each do |config|
            next unless options.key? config
            send(:"#{config}=", options.delete(config))
          end
        end
      end

      include mod
    end

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