forked from railsadminteam/rails_admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
active_record.rb
193 lines (165 loc) · 5.37 KB
/
active_record.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require 'active_record'
require 'rails_admin/config/sections/list'
require 'rails_admin/abstract_object'
module RailsAdmin
module Adapters
module ActiveRecord
def self.polymorphic_parents(name)
unless @polymorphic_parents
@polymorphic_parents = {}
RailsAdmin::AbstractModel.all.each do |abstract_model|
abstract_model.polymorphic_associations.each do |association|
(@polymorphic_parents[association[:options][:as].to_sym] ||= []) << abstract_model
end
end
end
@polymorphic_parents[name.to_sym]
end
def get(id)
if object = model.find_by_id(id)
RailsAdmin::AbstractObject.new object
else
nil
end
end
def get_bulk(ids, scope = nil)
scope ||= model
scope.find_all_by_id(ids)
end
def count(options = {}, scope = nil)
scope ||= model
scope.count(options.reject{|key, value| [:sort, :sort_reverse].include?(key)})
end
def first(options = {}, scope = nil)
scope ||= model
scope.first(merge_order(options))
end
def all(options = {}, scope = nil)
scope ||= model
scope.all(merge_order(options))
end
def paginated(options = {}, scope = nil)
page = options.delete(:page) || 1
per_page = options.delete(:per_page) || RailsAdmin::Config::Sections::List.default_items_per_page
page_count = (count(options, scope).to_f / per_page).ceil
options.merge!({
:limit => per_page,
:offset => (page - 1) * per_page
})
[page_count, all(options, scope)]
end
def create(params = {})
model.create(params)
end
def new(params = {})
RailsAdmin::AbstractObject.new(model.new)
end
def destroy(ids, scope = nil)
scope ||= model
scope.destroy_all(:id => ids)
end
def destroy_all!
model.all.each do |object|
object.destroy
end
end
def has_and_belongs_to_many_associations
associations.select do |association|
association[:type] == :has_and_belongs_to_many
end
end
def has_many_associations
associations.select do |association|
association[:type] == :has_many
end
end
def has_one_associations
associations.select do |association|
association[:type] == :has_one
end
end
def belongs_to_associations
associations.select do |association|
association[:type] == :belongs_to
end
end
def associations
model.reflect_on_all_associations.map do |association|
{
:name => association.name,
:pretty_name => association.name.to_s.tr('_', ' ').capitalize,
:type => association.macro,
:parent_model => association_parent_model_lookup(association),
:parent_key => association_parent_key_lookup(association),
:child_model => association_child_model_lookup(association),
:child_key => association_child_key_lookup(association),
:options => association.options,
}
end
end
def polymorphic_associations
(has_many_associations + has_one_associations).select do |association|
association[:options][:as]
end
end
def properties
model.columns.map do |property|
{
:name => property.name.to_sym,
:pretty_name => property.name.to_s.tr('_', ' ').capitalize,
:type => property.type,
:length => property.limit,
:nullable? => property.null,
:serial? => property.primary,
}
end
end
def model_store_exists?
model.table_exists?
end
private
def merge_order(options)
@sort ||= options.delete(:sort) || "id"
@sort_order ||= options.delete(:sort_reverse) ? "asc" : "desc"
options.merge(:order => "#{@sort} #{@sort_order}")
end
def association_parent_model_lookup(association)
case association.macro
when :belongs_to
if association.options[:polymorphic]
RailsAdmin::Adapters::ActiveRecord.polymorphic_parents(association.name)
else
association.klass
end
when :has_one, :has_many, :has_and_belongs_to_many
association.active_record
else
raise "Unknown association type: #{association.macro.inspect}"
end
end
def association_parent_key_lookup(association)
[:id]
end
def association_child_model_lookup(association)
case association.macro
when :belongs_to
association.active_record
when :has_one, :has_many, :has_and_belongs_to_many
association.klass
else
raise "Unknown association type: #{association.macro.inspect}"
end
end
def association_child_key_lookup(association)
case association.macro
when :belongs_to
[association.options[:foreign_key] || "#{association.name}_id".to_sym]
when :has_one, :has_many, :has_and_belongs_to_many
[association.primary_key_name.to_sym]
else
raise "Unknown association type: #{association.macro.inspect}"
end
end
end
end
end