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/object_extension.rb
Defined Under Namespace
Modules: ObjectExtension
Classes: Association, Property, StatementBuilder, WhereBuilder
Constant Summary
collapse
- DISABLED_COLUMN_TYPES =
i[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, scope = scoped) ⇒ 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
104
105
106
|
# File 'lib/rails_admin/adapters/active_record.rb', line 104
def adapter_supports_joins?
true
end
|
#all(options = {}, scope = nil) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/rails_admin/adapters/active_record.rb', line 30
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]
scope = scope.send(Kaminari.config.page_method_name, 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
|
#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
165
166
167
|
# File 'lib/rails_admin/adapters/active_record.rb', line 165
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
100
101
102
|
# File 'lib/rails_admin/adapters/active_record.rb', line 100
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
96
97
98
|
# File 'lib/rails_admin/adapters/active_record.rb', line 96
def embedded?
false
end
|
#encoding ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/rails_admin/adapters/active_record.rb', line 73
def encoding
adapter =
if ::ActiveRecord::Base.respond_to?(:connection_db_config)
::ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
else
::ActiveRecord::Base.connection_config[:adapter]
end
case adapter
when 'postgresql'
::ActiveRecord::Base.connection.select_one("SELECT ''::text AS str;").values.first.encoding
when 'mysql2'
if RUBY_ENGINE == 'jruby'
::ActiveRecord::Base.connection.select_one("SELECT '' AS str;").values.first.encoding
else
::ActiveRecord::Base.connection.raw_connection.encoding
end
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/rails_admin/adapters/active_record.rb', line 150
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
26
27
28
|
# File 'lib/rails_admin/adapters/active_record.rb', line 26
def first(options = {}, scope = nil)
all(options, scope).first
end
|
#get(id, scope = scoped) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/rails_admin/adapters/active_record.rb', line 15
def get(id, scope = scoped)
object = scope.where(primary_key => id).first
return unless object
object.extend(ObjectExtension)
end
|
#new(params = {}) ⇒ Object
11
12
13
|
# File 'lib/rails_admin/adapters/active_record.rb', line 11
def new(params = {})
model.new(params).extend(ObjectExtension)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/rails_admin/adapters/active_record.rb', line 134
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
22
23
24
|
# File 'lib/rails_admin/adapters/active_record.rb', line 22
def scoped
model.all
end
|