Module: Kingsman::Models
- Defined in:
- lib/kingsman/models.rb,
lib/kingsman/models/lockable.rb,
lib/kingsman/models/trackable.rb,
lib/kingsman/models/confirmable.rb,
lib/kingsman/models/recoverable.rb,
lib/kingsman/models/timeoutable.rb,
lib/kingsman/models/validatable.rb,
lib/kingsman/models/omniauthable.rb,
lib/kingsman/models/registerable.rb,
lib/kingsman/models/rememberable.rb,
lib/kingsman/models/authenticatable.rb,
lib/kingsman/models/database_authenticatable.rb
Defined Under Namespace
Modules: Authenticatable, Confirmable, DatabaseAuthenticatable, Lockable, Omniauthable, Recoverable, Registerable, Rememberable, Timeoutable, Trackable, Validatable Classes: MissingAttribute
Class Method Summary collapse
- .check_fields!(klass) ⇒ Object
-
.config(mod, *accessors) ⇒ Object
Creates configuration values for Kingsman and for the given module.
Instance Method Summary collapse
-
#kingsman(*modules) ⇒ Object
Include the chosen kingsman modules in your model:.
-
#kingsman_modules_hook! ⇒ Object
The hook which is called inside kingsman.
Class Method Details
.check_fields!(klass) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kingsman/models.rb', line 54 def self.check_fields!(klass) failed_attributes = [] instance = klass.new klass.kingsman_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 if failed_attributes.any? fail Kingsman::Models::MissingAttribute.new(failed_attributes) end end |
.config(mod, *accessors) ⇒ Object
Creates configuration values for Kingsman and for the given module.
Kingsman::Models.config(Kingsman::Models::DatabaseAuthenticatable, :stretches)
The line above creates:
1) An accessor called Kingsman.stretches, which value is used by default;
2) Some class methods for your model Model.stretches and Model.stretches=
which have higher priority than Kingsman.stretches;
3) And an instance method stretches.
To add the class methods you need to have a module ClassMethods defined inside the given class.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kingsman/models.rb', line 31 def self.config(mod, *accessors) #:nodoc: 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 Kingsman.#{accessor} end end def #{accessor}=(value) @#{accessor} = value end METHOD end end |
Instance Method Details
#kingsman(*modules) ⇒ Object
Include the chosen kingsman modules in your model:
kingsman :database_authenticatable, :confirmable, :recoverable
You can also give any of the kingsman configuration values in form of a hash, with specific values for this model. Please check your Kingsman initializer for a complete description on those values.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/kingsman/models.rb', line 79 def kingsman(*modules) = modules..dup selected_modules = modules.map(&:to_sym).uniq.sort_by do |s| Kingsman::ALL.index(s) || -1 # follow Kingsman::ALL order end kingsman_modules_hook! do include Kingsman::Orm include Kingsman::Models::Authenticatable selected_modules.each do |m| mod = Kingsman::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 .key?(config) send(:"#{config}=", .delete(config)) end end end include mod end self.kingsman_modules |= selected_modules .each { |key, value| send(:"#{key}=", value) } end end |
#kingsman_modules_hook! ⇒ Object
The hook which is called inside kingsman. So your ORM can include kingsman compatibility stuff.
116 117 118 |
# File 'lib/kingsman/models.rb', line 116 def kingsman_modules_hook! yield end |