Class: RailsAdmin::AbstractModel
- Inherits:
-
Object
- Object
- RailsAdmin::AbstractModel
- Defined in:
- lib/rails_admin/abstract_model.rb
Constant Summary collapse
- @@polymorphic_parents =
{}
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
Class Method Summary collapse
- .all(adapter = nil) ⇒ Object
- .new(m) ⇒ Object
- .old_new ⇒ Object
- .polymorphic_parents(adapter, model_name, name) ⇒ Object
- .reset ⇒ Object
-
.reset_polymorphic_parents ⇒ Object
For testing.
Instance Method Summary collapse
- #config ⇒ Object
- #each_associated_children(object) ⇒ Object
-
#initialize(model_or_model_name) ⇒ AbstractModel
constructor
A new instance of AbstractModel.
-
#model ⇒ Object
do not store a reference to the model, does not play well with ActiveReload/Rails3.2.
- #param_key ⇒ Object
- #pretty_name ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
- #where(conditions) ⇒ Object
Constructor Details
#initialize(model_or_model_name) ⇒ AbstractModel
Returns a new instance of AbstractModel.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rails_admin/abstract_model.rb', line 44 def initialize(model_or_model_name) @model_name = model_or_model_name.to_s if model.ancestors.map(&:to_s).include?('ActiveRecord::Base') && !model.abstract_class? # ActiveRecord @adapter = :active_record require 'rails_admin/adapters/active_record' extend Adapters::ActiveRecord elsif model.ancestors.map(&:to_s).include?('Mongoid::Document') # Mongoid @adapter = :mongoid require 'rails_admin/adapters/mongoid' extend Adapters::Mongoid end end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
4 5 6 |
# File 'lib/rails_admin/abstract_model.rb', line 4 def adapter @adapter end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
4 5 6 |
# File 'lib/rails_admin/abstract_model.rb', line 4 def model_name @model_name end |
Class Method Details
.all(adapter = nil) ⇒ Object
11 12 13 14 |
# File 'lib/rails_admin/abstract_model.rb', line 11 def all(adapter = nil) @@all ||= Config.models_pool.map{ |m| new(m) }.compact adapter ? @@all.select{|m| m.adapter == adapter} : @@all end |
.new(m) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rails_admin/abstract_model.rb', line 17 def new(m) m = m.is_a?(Class) ? m : m.constantize (am = old_new(m)).model && am.adapter ? am : nil rescue LoadError, NameError puts "[RailsAdmin] Could not load model #{m}, assuming model is non existing. (#{$!})" unless Rails.env.test? nil end |
.old_new ⇒ Object
16 |
# File 'lib/rails_admin/abstract_model.rb', line 16 alias_method :old_new, :new |
.polymorphic_parents(adapter, model_name, name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rails_admin/abstract_model.rb', line 27 def polymorphic_parents(adapter, model_name, name) @@polymorphic_parents[adapter.to_sym] ||= {}.tap do |hash| all(adapter).each do |am| am.associations.select{|r| r[:as] }.each do |association| (hash[[association[:model_proc].call.to_s.underscore, association[:as]].join('_').to_sym] ||= []) << am.model end end end @@polymorphic_parents[adapter.to_sym][[model_name.underscore, name].join('_').to_sym] end |
.reset ⇒ Object
7 8 9 |
# File 'lib/rails_admin/abstract_model.rb', line 7 def reset @@all = nil end |
.reset_polymorphic_parents ⇒ Object
For testing
39 40 41 |
# File 'lib/rails_admin/abstract_model.rb', line 39 def reset_polymorphic_parents @@polymorphic_parents = {} end |
Instance Method Details
#config ⇒ Object
68 69 70 |
# File 'lib/rails_admin/abstract_model.rb', line 68 def config Config.model self end |
#each_associated_children(object) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rails_admin/abstract_model.rb', line 88 def each_associated_children(object) associations.each do |association| case association[:type] when :has_one if child = object.send(association[:name]) yield(association, child) end when :has_many object.send(association[:name]).each do |child| yield(association, child) end end end end |
#model ⇒ Object
do not store a reference to the model, does not play well with ActiveReload/Rails3.2
60 61 62 |
# File 'lib/rails_admin/abstract_model.rb', line 60 def model @model_name.constantize end |
#param_key ⇒ Object
76 77 78 |
# File 'lib/rails_admin/abstract_model.rb', line 76 def param_key @model_name.split("::").map(&:underscore).join("_") end |
#pretty_name ⇒ Object
80 81 82 |
# File 'lib/rails_admin/abstract_model.rb', line 80 def pretty_name model.model_name.human end |
#to_param ⇒ Object
72 73 74 |
# File 'lib/rails_admin/abstract_model.rb', line 72 def to_param @model_name.split("::").map(&:underscore).join("~") end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/rails_admin/abstract_model.rb', line 64 def to_s model.to_s end |
#where(conditions) ⇒ Object
84 85 86 |
# File 'lib/rails_admin/abstract_model.rb', line 84 def where(conditions) model.where(conditions) end |