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, name) ⇒ Object
- .reset ⇒ Object
-
.reset_polymorphic_parents ⇒ Object
For testing.
Instance Method Summary collapse
- #config ⇒ Object
-
#initialize(m) ⇒ 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
- #where(conditions) ⇒ Object
Constructor Details
#initialize(m) ⇒ AbstractModel
Returns a new instance of AbstractModel.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rails_admin/abstract_model.rb', line 43 def initialize(m) @model_name = m.to_s if m.ancestors.map(&:to_s).include?('ActiveRecord::Base') && !m.abstract_class? # ActiveRecord @adapter = :active_record require 'rails_admin/adapters/active_record' extend Adapters::ActiveRecord elsif m.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 |
# 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 nil end |
.old_new ⇒ Object
16 |
# File 'lib/rails_admin/abstract_model.rb', line 16 alias_method :old_new, :new |
.polymorphic_parents(adapter, name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rails_admin/abstract_model.rb', line 26 def polymorphic_parents(adapter, 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[:as].to_sym] ||= []) << am.model end end end @@polymorphic_parents[adapter.to_sym][name.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
38 39 40 |
# File 'lib/rails_admin/abstract_model.rb', line 38 def reset_polymorphic_parents @@polymorphic_parents = {} end |
Instance Method Details
#config ⇒ Object
63 64 65 |
# File 'lib/rails_admin/abstract_model.rb', line 63 def config Config.model self end |
#model ⇒ Object
do not store a reference to the model, does not play well with ActiveReload/Rails3.2
59 60 61 |
# File 'lib/rails_admin/abstract_model.rb', line 59 def model @model_name.try :constantize end |
#param_key ⇒ Object
71 72 73 |
# File 'lib/rails_admin/abstract_model.rb', line 71 def param_key model.to_s.split("::").map(&:underscore).join("_") end |
#pretty_name ⇒ Object
75 76 77 |
# File 'lib/rails_admin/abstract_model.rb', line 75 def pretty_name model.model_name.human end |
#to_param ⇒ Object
67 68 69 |
# File 'lib/rails_admin/abstract_model.rb', line 67 def to_param model.to_s.split("::").map(&:underscore).join("~") end |
#where(conditions) ⇒ Object
79 80 81 |
# File 'lib/rails_admin/abstract_model.rb', line 79 def where(conditions) model.where(conditions) end |