Class: RailsAdmin::AbstractModel
- Inherits:
-
Object
- Object
- RailsAdmin::AbstractModel
show all
- Defined in:
- lib/rails_admin/abstract_model.rb
Defined Under Namespace
Classes: StatementBuilder
Constant Summary
collapse
- @@polymorphic_parents =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model_or_model_name) ⇒ AbstractModel
Returns a new instance of AbstractModel.
46
47
48
49
50
51
52
53
54
|
# File 'lib/rails_admin/abstract_model.rb', line 46
def initialize(model_or_model_name)
@model_name = model_or_model_name.to_s
ancestors = model.ancestors.collect(&:to_s)
if ancestors.include?('ActiveRecord::Base') && !model.abstract_class? && model.table_exists?
initialize_active_record
elsif ancestors.include?('Mongoid::Document')
initialize_mongoid
end
end
|
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
6
7
8
|
# File 'lib/rails_admin/abstract_model.rb', line 6
def adapter
@adapter
end
|
#model_name ⇒ Object
Returns the value of attribute model_name.
6
7
8
|
# File 'lib/rails_admin/abstract_model.rb', line 6
def model_name
@model_name
end
|
Class Method Details
.all(adapter = nil) ⇒ Object
13
14
15
16
|
# File 'lib/rails_admin/abstract_model.rb', line 13
def all(adapter = nil)
@@all ||= Config.models_pool.collect { |m| new(m) }.compact
adapter ? @@all.select { |m| m.adapter == adapter } : @@all
end
|
.new(m) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/rails_admin/abstract_model.rb', line 19
def new(m)
m = m.constantize unless m.is_a?(Class)
(am = old_new(m)).model && am.adapter ? am : nil
rescue LoadError, NameError
puts "[RailsAdmin] Could not load model #{m}, assuming model is non existing. (#{$ERROR_INFO})" unless Rails.env.test?
nil
end
|
.old_new ⇒ Object
18
|
# File 'lib/rails_admin/abstract_model.rb', line 18
alias_method :old_new, :new
|
.polymorphic_parents(adapter, model_name, name) ⇒ Object
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/rails_admin/abstract_model.rb', line 29
def polymorphic_parents(adapter, model_name, name)
@@polymorphic_parents[adapter.to_sym] ||= {}.tap do |hash|
all(adapter).each do |am|
am.associations.select(&:as).each do |association|
(hash[[association.klass.to_s.underscore, association.as].join('_').to_sym] ||= []) << am.model
end
end
end
@@polymorphic_parents[adapter.to_sym][[model_name.to_s.underscore, name].join('_').to_sym]
end
|
.reset ⇒ Object
9
10
11
|
# File 'lib/rails_admin/abstract_model.rb', line 9
def reset
@@all = nil
end
|
.reset_polymorphic_parents ⇒ Object
41
42
43
|
# File 'lib/rails_admin/abstract_model.rb', line 41
def reset_polymorphic_parents
@@polymorphic_parents = {}
end
|
Instance Method Details
#config ⇒ Object
65
66
67
|
# File 'lib/rails_admin/abstract_model.rb', line 65
def config
Config.model self
end
|
#each_associated_children(object) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/rails_admin/abstract_model.rb', line 85
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
children = object.send(association.name)
yield(association, Array.new(children))
end
end
end
|
#model ⇒ Object
do not store a reference to the model, does not play well with ActiveReload/Rails3.2
57
58
59
|
# File 'lib/rails_admin/abstract_model.rb', line 57
def model
@model_name.constantize
end
|
#param_key ⇒ Object
73
74
75
|
# File 'lib/rails_admin/abstract_model.rb', line 73
def param_key
@model_name.split('::').collect(&:underscore).join('_')
end
|
#pretty_name ⇒ Object
77
78
79
|
# File 'lib/rails_admin/abstract_model.rb', line 77
def pretty_name
model.model_name.human
end
|
#to_param ⇒ Object
69
70
71
|
# File 'lib/rails_admin/abstract_model.rb', line 69
def to_param
@model_name.split('::').collect(&:underscore).join('~')
end
|
#to_s ⇒ Object
61
62
63
|
# File 'lib/rails_admin/abstract_model.rb', line 61
def to_s
model.to_s
end
|
#where(conditions) ⇒ Object
81
82
83
|
# File 'lib/rails_admin/abstract_model.rb', line 81
def where(conditions)
model.where(conditions)
end
|