forked from railsadminteam/rails_admin
/
active_record.rb
271 lines (236 loc) · 9.71 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require 'active_record'
require 'rails_admin/adapters/active_record/abstract_object'
module RailsAdmin
module Adapters
module ActiveRecord
DISABLED_COLUMN_TYPES = [:tsvector, :blob, :binary, :spatial]
LIKE_OPERATOR = ::ActiveRecord::Base.configurations[Rails.env]['adapter'] == "postgresql" ? 'ILIKE' : 'LIKE'
def new(params = {})
AbstractObject.new(model.new(params))
end
def get(id)
if object = model.where(model.primary_key => id).first
AbstractObject.new object
else
nil
end
end
def scoped
model.scoped
end
def first(options = {}, scope = nil)
all(options, scope).first
end
def all(options = {}, scope = nil)
scope ||= self.scoped
scope = scope.includes(options[:include]) if options[:include]
scope = scope.limit(options[:limit]) if options[:limit]
scope = scope.where(model.primary_key => options[:bulk_ids]) if options[:bulk_ids]
scope = scope.where(query_conditions(options[:query])) if options[:query]
scope = scope.where(filter_conditions(options[:filters])) if options[:filters]
scope = scope.page(options[:page]).per(options[:per]) if options[:page] && options[:per]
scope = scope.reorder("#{options[:sort]} #{options[:sort_reverse] ? 'asc' : 'desc'}") if options[:sort]
scope
end
def count(options = {}, scope = nil)
all(options.merge({:limit => false, :page => false}), scope).count
end
def destroy(objects)
Array.wrap(objects).each &:destroy
end
def associations
model.reflect_on_all_associations.map do |association|
{
:name => association.name.to_sym,
:pretty_name => association.name.to_s.tr('_', ' ').capitalize,
:type => association.macro,
:parent_model_proc => Proc.new { association_parent_model_lookup(association) },
:parent_key => association_parent_key_lookup(association),
:child_model_proc => Proc.new { association_child_model_lookup(association) },
:child_key => association_child_key_lookup(association),
:foreign_type => association_foreign_type_lookup(association),
:as => association_as_lookup(association),
:polymorphic => association_polymorphic_lookup(association),
:inverse_of => association_inverse_of_lookup(association),
:read_only => association_read_only_lookup(association),
:nested_form => association_nested_attributes_options_lookup(association)
}
end
end
def properties
columns = model.columns.reject {|c| c.type.blank? || DISABLED_COLUMN_TYPES.include?(c.type.to_sym) }
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
def table_name
model.table_name
end
def serialized_attributes
model.serialized_attributes
end
def accessible_by(*args)
model.accessible_by(*args)
end
private
def query_conditions(query, fields = config.list.fields.select(&:queryable?))
statements = []
values = []
fields.each do |field|
field.searchable_columns.flatten.each do |column_infos|
statement, value1, value2 = build_statement(column_infos[:column], column_infos[:type], query, field.search_operator)
statements << statement if statement
values << value1 unless value1.nil?
values << value2 unless value2.nil?
end
end
[statements.join(' OR '), *values]
end
# filters example => {"string_field"=>{"0055"=>{"o"=>"like", "v"=>"test_value"}}, ...}
# "0055" is the filter index, no use here. o is the operator, v the value
def filter_conditions(filters, fields = config.list.fields.select(&:filterable?))
statements = []
values = []
filters.each_pair do |field_name, filters_dump|
filters_dump.each do |filter_index, filter_dump|
field_statements = []
fields.find{|f| f.name.to_s == field_name}.searchable_columns.each do |column_infos|
statement, value1, value2 = build_statement(column_infos[:column], column_infos[:type], filter_dump[:v], (filter_dump[:o] || 'default'))
field_statements << statement if statement.present?
values << value1 unless value1.nil?
values << value2 unless value2.nil?
end
statements << "(#{field_statements.join(' OR ')})" unless field_statements.empty?
end
end
[statements.join(' AND '), *values]
end
private
def build_statement(column, type, value, operator)
# this operator/value has been discarded (but kept in the dom to override the one stored in the various links of the page)
return if operator == '_discard' || value == '_discard'
# filtering data with unary operator, not type dependent
if operator == '_blank' || value == '_blank'
return ["(#{column} IS NULL OR #{column} = '')"]
elsif operator == '_present' || value == '_present'
return ["(#{column} IS NOT NULL AND #{column} != '')"]
elsif operator == '_null' || value == '_null'
return ["(#{column} IS NULL)"]
elsif operator == '_not_null' || value == '_not_null'
return ["(#{column} IS NOT NULL)"]
elsif operator == '_empty' || value == '_empty'
return ["(#{column} = '')"]
elsif operator == '_not_empty' || value == '_not_empty'
return ["(#{column} != '')"]
end
# now we go type specific
case type
when :boolean
return ["(#{column} IS NULL OR #{column} = ?)", false] if ['false', 'f', '0'].include?(value)
return ["(#{column} = ?)", true] if ['true', 't', '1'].include?(value)
when :integer, :belongs_to_association
return if value.blank?
["(#{column} = ?)", value.to_i] if value.to_i.to_s == value
when :string, :text
return if value.blank?
value = case operator
when 'default', 'like'
"%#{value}%"
when 'starts_with'
"#{value}%"
when 'ends_with'
"%#{value}"
when 'is', '='
"#{value}"
end
["(#{column} #{LIKE_OPERATOR} ?)", value]
when :datetime, :timestamp, :date
return unless operator != 'default'
values = case operator
when 'today'
[Date.today.beginning_of_day, Date.today.end_of_day]
when 'yesterday'
[Date.yesterday.beginning_of_day, Date.yesterday.end_of_day]
when 'this_week'
[Date.today.beginning_of_week.beginning_of_day, Date.today.end_of_week.end_of_day]
when 'last_week'
[1.week.ago.to_date.beginning_of_week.beginning_of_day, 1.week.ago.to_date.end_of_week.end_of_day]
when 'less_than'
return if value.blank?
[value.to_i.days.ago, DateTime.now]
when 'more_than'
return if value.blank?
[2000.years.ago, value.to_i.days.ago]
when 'mmddyyyy'
return if (value.blank? || value.match(/([0-9]{8})/).nil?)
[Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').beginning_of_day, Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').end_of_day]
end
["(#{column} BETWEEN ? AND ?)", *values]
when :enum
return if value.blank?
["(#{column} IN (?))", Array.wrap(value)]
end
end
def association_parent_model_lookup(association)
case association.macro
when :belongs_to
if association.options[:polymorphic]
RailsAdmin::AbstractModel.polymorphic_parents(:active_record, 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_foreign_type_lookup(association)
if association.options[:polymorphic]
association.options[:foreign_type].try(:to_sym) || :"#{association.name}_type"
end
end
def association_nested_attributes_options_lookup(association)
model.nested_attributes_options.try { |o| o[association.name.to_sym] }
end
def association_as_lookup(association)
association.options[:as].try :to_sym
end
def association_polymorphic_lookup(association)
association.options[:polymorphic]
end
def association_parent_key_lookup(association)
[:id]
end
def association_inverse_of_lookup(association)
association.options[:inverse_of].try :to_sym
end
def association_read_only_lookup(association)
association.options[:readonly]
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)
association.foreign_key.to_sym
end
end
end
end