module Negroni::Models
Namespace for Model-specific configuration.
Public Class Methods
check_fields!(klass)
click to toggle source
Checks that the including class has the appropriate required fields.
@param klass [Class] the including class @raise [Negroni::Models::MissingAttribute] if the check fails. @return [Void]
# 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)
click to toggle source
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.
@api private
# 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
Public Instance Methods
aa_module_hook!() { || ... }
click to toggle source
a hook
# File lib/negroni/models.rb, line 132 def aa_module_hook! yield end
negroni(*modules)
click to toggle source
Include the given Negroni
modules in your model. Authenticate is always included.
@param modules [Symbol, Array<Symbol>] the modules to include. @return [Void]
# 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