Module: RailsAdmin::Adapters::ActiveRecord

Included in:
CompositePrimaryKeys
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

Instance Method Details

#adapter_supports_joins?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rails_admin/adapters/active_record.rb', line 106

def adapter_supports_joins?
  true
end

#all(options = {}, scope = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# 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

#associationsObject



52
53
54
55
56
# 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_classObject



69
70
71
# File 'lib/rails_admin/adapters/active_record.rb', line 69

def base_class
  model.base_class
end

#count(options = {}, scope = nil) ⇒ Object



44
45
46
# 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?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/rails_admin/adapters/active_record.rb', line 102

def cyclic?
  false
end

#destroy(objects) ⇒ Object



48
49
50
# File 'lib/rails_admin/adapters/active_record.rb', line 48

def destroy(objects)
  Array.wrap(objects).each(&:destroy)
end

#embedded?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/rails_admin/adapters/active_record.rb', line 98

def embedded?
  false
end

#encodingObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rails_admin/adapters/active_record.rb', line 75

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) ⇒ Object



28
29
30
# File 'lib/rails_admin/adapters/active_record.rb', line 28

def first(options = {}, scope = nil)
  all(options, scope).first
end

#get(id, scope = scoped) ⇒ Object



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

def get(id, scope = scoped)
  object = scope.where(primary_key => id).first
  return unless object

  object.extend(ObjectExtension)
end

#new(params = {}) ⇒ Object



13
14
15
# File 'lib/rails_admin/adapters/active_record.rb', line 13

def new(params = {})
  model.new(params).extend(ObjectExtension)
end

#propertiesObject



58
59
60
61
62
63
64
65
66
67
# 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

#scopedObject



24
25
26
# File 'lib/rails_admin/adapters/active_record.rb', line 24

def scoped
  model.all
end