Module: RailsAdmin::Adapters::ActiveRecord
- Defined in:
- lib/rails_admin/adapters/active_record.rb,
lib/rails_admin/adapters/active_record/property.rb,
lib/rails_admin/adapters/active_record/association.rb,
lib/rails_admin/adapters/active_record/abstract_object.rb
Defined Under Namespace
Classes: AbstractObject, Association, Property, StatementBuilder, WhereBuilder
Constant Summary
collapse
- DISABLED_COLUMN_TYPES =
[:tsvector, :blob, :binary, :spatial, :hstore, :geometry]
Instance Method Summary
collapse
-
#adapter_supports_joins? ⇒ Boolean
-
#all(options = {}, scope = nil) ⇒ Object
-
#associations ⇒ Object
-
#build_statement(column, type, value, operator) ⇒ Object
-
#count(options = {}, scope = nil) ⇒ Object
-
#cyclic? ⇒ Boolean
-
#destroy(objects) ⇒ Object
-
#embedded? ⇒ Boolean
-
#encoding ⇒ Object
-
#filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?)) ⇒ Object
filters example => “v”=>“test_value”}, …} “0055” is the filter index, no use here.
-
#first(options = {}, scope = nil) ⇒ Object
-
#get(id) ⇒ Object
-
#new(params = {}) ⇒ Object
-
#properties ⇒ Object
-
#query_scope(scope, query, fields = config.list.fields.select(&:queryable?)) ⇒ Object
-
#scoped ⇒ Object
Instance Method Details
#adapter_supports_joins? ⇒ Boolean
83
84
85
|
# File 'lib/rails_admin/adapters/active_record.rb', line 83
def adapter_supports_joins?
true
end
|
#all(options = {}, scope = nil) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/rails_admin/adapters/active_record.rb', line 28
def all(options = {}, scope = nil)
scope ||= scoped
scope = scope.includes(options[:include]) if options[:include]
scope = scope.limit(options[:limit]) if options[:limit]
scope = scope.where(primary_key => options[:bulk_ids]) if options[:bulk_ids]
scope = query_scope(scope, options[:query]) if options[:query]
scope = filter_scope(scope, options[:filters]) if options[:filters]
if options[:page] && options[:per]
scope = scope.send(Kaminari.config.page_method_name, options[:page]).per(options[:per])
end
scope = scope.reorder("#{options[:sort]} #{options[:sort_reverse] ? 'asc' : 'desc'}") if options[:sort]
scope
end
|
#associations ⇒ Object
50
51
52
53
54
|
# File 'lib/rails_admin/adapters/active_record.rb', line 50
def associations
model.reflect_on_all_associations.collect do |association|
Association.new(association, model)
end
end
|
#build_statement(column, type, value, operator) ⇒ Object
136
137
138
|
# File 'lib/rails_admin/adapters/active_record.rb', line 136
def build_statement(column, type, value, operator)
StatementBuilder.new(column, type, value, operator).to_statement
end
|
#count(options = {}, scope = nil) ⇒ Object
42
43
44
|
# File 'lib/rails_admin/adapters/active_record.rb', line 42
def count(options = {}, scope = nil)
all(options.merge(limit: false, page: false), scope).count
end
|
#cyclic? ⇒ Boolean
79
80
81
|
# File 'lib/rails_admin/adapters/active_record.rb', line 79
def cyclic?
false
end
|
#destroy(objects) ⇒ Object
46
47
48
|
# File 'lib/rails_admin/adapters/active_record.rb', line 46
def destroy(objects)
Array.wrap(objects).each(&:destroy)
end
|
#embedded? ⇒ Boolean
75
76
77
|
# File 'lib/rails_admin/adapters/active_record.rb', line 75
def embedded?
false
end
|
#encoding ⇒ Object
69
70
71
72
73
|
# File 'lib/rails_admin/adapters/active_record.rb', line 69
def encoding
encoding = ::ActiveRecord::Base.connection.try(:encoding)
encoding ||= ::ActiveRecord::Base.connection.try(:charset) encoding || 'UTF-8'
end
|
#filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?)) ⇒ Object
filters example => “v”=>“test_value”}, …} “0055” is the filter index, no use here. o is the operator, v the value
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/rails_admin/adapters/active_record.rb', line 124
def filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?))
filters.each_pair do |field_name, filters_dump|
filters_dump.each do |_, filter_dump|
wb = WhereBuilder.new(scope)
wb.add(fields.detect { |f| f.name.to_s == field_name }, filter_dump[:v], (filter_dump[:o] || 'default'))
scope = wb.build
end
end
scope
end
|
#first(options = {}, scope = nil) ⇒ Object
24
25
26
|
# File 'lib/rails_admin/adapters/active_record.rb', line 24
def first(options = {}, scope = nil)
all(options, scope).first
end
|
#get(id) ⇒ Object
15
16
17
18
|
# File 'lib/rails_admin/adapters/active_record.rb', line 15
def get(id)
return unless object = model.where(primary_key => id).first
AbstractObject.new object
end
|
#new(params = {}) ⇒ Object
11
12
13
|
# File 'lib/rails_admin/adapters/active_record.rb', line 11
def new(params = {})
AbstractObject.new(model.new(params))
end
|
#properties ⇒ Object
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/rails_admin/adapters/active_record.rb', line 56
def properties
columns = model.columns.reject do |c|
c.type.blank? ||
DISABLED_COLUMN_TYPES.include?(c.type.to_sym) ||
c.try(:array)
end
columns.collect do |property|
Property.new(property, model)
end
end
|
#query_scope(scope, query, fields = config.list.fields.select(&:queryable?)) ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/rails_admin/adapters/active_record.rb', line 113
def query_scope(scope, query, fields = config.list.fields.select(&:queryable?))
wb = WhereBuilder.new(scope)
fields.each do |field|
wb.add(field, query, field.search_operator)
end
wb.build
end
|
#scoped ⇒ Object
20
21
22
|
# File 'lib/rails_admin/adapters/active_record.rb', line 20
def scoped
model.all
end
|