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].freeze
Instance Method Summary
collapse
-
#adapter_supports_joins? ⇒ Boolean
-
#all(options = {}, scope = nil) ⇒ Object
-
#associations ⇒ Object
-
#base_class ⇒ 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
94
95
96
|
# File 'lib/rails_admin/adapters/active_record.rb', line 94
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
|
#base_class ⇒ Object
67
68
69
|
# File 'lib/rails_admin/adapters/active_record.rb', line 67
def base_class
model.base_class
end
|
#build_statement(column, type, value, operator) ⇒ Object
155
156
157
|
# File 'lib/rails_admin/adapters/active_record.rb', line 155
def build_statement(column, type, value, operator)
StatementBuilder.new(column, type, value, operator, model.connection.adapter_name).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(:all)
end
|
#cyclic? ⇒ Boolean
90
91
92
|
# File 'lib/rails_admin/adapters/active_record.rb', line 90
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
86
87
88
|
# File 'lib/rails_admin/adapters/active_record.rb', line 86
def embedded?
false
end
|
#encoding ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/rails_admin/adapters/active_record.rb', line 73
def encoding
case ::ActiveRecord::Base.connection_config[:adapter]
when 'postgresql'
::ActiveRecord::Base.connection.select_one("SELECT ''::text AS str;").values.first.encoding
when 'mysql2'
::ActiveRecord::Base.connection.instance_variable_get(:@connection).encoding
when 'oracle_enhanced'
::ActiveRecord::Base.connection.select_one("SELECT dummy FROM DUAL").values.first.encoding
else
::ActiveRecord::Base.connection.select_one("SELECT '' AS str;").values.first.encoding
end
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/rails_admin/adapters/active_record.rb', line 140
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)
field = fields.detect { |f| f.name.to_s == field_name }
value = parse_field_value(field, filter_dump[:v])
wb.add(field, value, (filter_dump[:o] || RailsAdmin::Config.default_search_operator))
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
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/rails_admin/adapters/active_record.rb', line 124
def query_scope(scope, query, fields = config.list.fields.select(&:queryable?))
if config.list.search_by
scope.send(config.list.search_by, query)
else
wb = WhereBuilder.new(scope)
fields.each do |field|
value = parse_field_value(field, query)
wb.add(field, value, field.search_operator)
end
wb.build
end
end
|
#scoped ⇒ Object
20
21
22
|
# File 'lib/rails_admin/adapters/active_record.rb', line 20
def scoped
model.all
end
|