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

Instance Method Details

#adapter_supports_joins?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rails_admin/adapters/active_record.rb', line 100

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

#associationsObject



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_classObject



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



161
162
163
# File 'lib/rails_admin/adapters/active_record.rb', line 161

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

Returns:

  • (Boolean)


96
97
98
# File 'lib/rails_admin/adapters/active_record.rb', line 96

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/rails_admin/adapters/active_record.rb', line 92

def embedded?
  false
end

#encodingObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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'
    ::ActiveRecord::Base.connection.raw_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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rails_admin/adapters/active_record.rb', line 146

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))
      # AND current filter statements to other filter statements
      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

#propertiesObject



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



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rails_admin/adapters/active_record.rb', line 130

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
    # OR all query statements
    wb.build
  end
end

#scopedObject



20
21
22
# File 'lib/rails_admin/adapters/active_record.rb', line 20

def scoped
  model.all
end