module RailsAdmin::Adapters::ActiveRecord

Constants

DISABLED_COLUMN_TYPES

Public Instance Methods

adapter_supports_joins?() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 114
def adapter_supports_joins?
  true
end
all(options = {}, scope = nil) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 32
def all(options = {}, scope = nil)
  scope ||= scoped
  scope = scope.includes(options[:include]) if options[:include]
  scope = scope.limit(options[:limit]) if options[:limit]
  scope = bulk_scope(scope, options) 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 = sort_scope(scope, options) if options[:sort]
  scope
end
associations() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 52
def associations
  model.reflect_on_all_associations.collect do |association|
    Association.new(association, model)
  end
end
base_class() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 69
def base_class
  model.base_class
end
count(options = {}, scope = nil) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 44
def count(options = {}, scope = nil)
  all(options.merge(limit: false, page: false), scope).count(:all)
end
cyclic?() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 110
def cyclic?
  false
end
destroy(objects) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 48
def destroy(objects)
  Array.wrap(objects).each(&:destroy)
end
embedded?() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 106
def embedded?
  false
end
encoding() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 83
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
first(options = {}, scope = nil) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 28
def first(options = {}, scope = nil)
  all(options, scope).first
end
format_id(id) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 118
def format_id(id)
  if primary_key.is_a? Array
    RailsAdmin.config.composite_keys_serializer.serialize(id)
  else
    id
  end
end
get(id, scope = scoped) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 17
def get(id, scope = scoped)
  object = primary_key_scope(scope, id).first
  return unless object

  object.extend(ObjectExtension)
end
new(params = {}) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 13
def new(params = {})
  model.new(params).extend(ObjectExtension)
end
parse_id(id) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 126
def parse_id(id)
  if primary_key.is_a?(Array)
    ids = RailsAdmin.config.composite_keys_serializer.deserialize(id)
    primary_key.each_with_index do |key, i|
      ids[i] = model.type_for_attribute(key).cast(ids[i])
    end
    ids
  else
    id
  end
end
properties() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 58
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
quote_column_name(name) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 79
def quote_column_name(name)
  model.connection.quote_column_name(name)
end
quoted_table_name() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 75
def quoted_table_name
  model.quoted_table_name
end
scoped() click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 24
def scoped
  model.all
end

Private Instance Methods

build_statement(column, type, value, operator) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 228
def build_statement(column, type, value, operator)
  StatementBuilder.new(column, type, value, operator, model.connection.adapter_name).to_statement
end
bulk_scope(scope, options) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 148
def bulk_scope(scope, options)
  if primary_key.is_a? Array
    options[:bulk_ids].map { |id| primary_key_scope(scope, id) }.reduce(&:or)
  else
    scope.where(primary_key => options[:bulk_ids])
  end
end
filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?)) click to toggle source

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

# File lib/rails_admin/adapters/active_record.rb, line 213
def filter_scope(scope, filters, fields = config.list.fields.select(&:filterable?))
  filters.each_pair do |field_name, filters_dump|
    filters_dump.each_value 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
primary_key_scope(scope, id) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 140
def primary_key_scope(scope, id)
  if primary_key.is_a? Array
    scope.where(primary_key.zip(parse_id(id)).to_h)
  else
    scope.where(primary_key => id)
  end
end
query_scope(scope, query, fields = config.list.fields.select(&:queryable?)) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 197
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
sort_scope(scope, options) click to toggle source
# File lib/rails_admin/adapters/active_record.rb, line 156
def sort_scope(scope, options)
  direction = options[:sort_reverse] ? :asc : :desc
  case options[:sort]
  when String, Symbol
    scope.reorder("#{options[:sort]} #{direction}")
  when Array
    scope.reorder(options[:sort].zip(Array.new(options[:sort].size) { direction }).to_h)
  when Hash
    scope.reorder(options[:sort].map { |table_name, column| "#{table_name}.#{column}" }.
      zip(Array.new(options[:sort].size) { direction }).to_h)
  else
    raise ArgumentError.new("Unsupported sort value: #{options[:sort]}")
  end
end