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

Instance Method Details

#adapter_supports_joins?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/rails_admin/adapters/active_record.rb', line 114

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)


110
111
112
# File 'lib/rails_admin/adapters/active_record.rb', line 110

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)


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

def embedded?
  false
end

#encodingObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object



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

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

#format_id(id) ⇒ Object



118
119
120
121
122
123
124
# 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) ⇒ Object



17
18
19
20
21
22
# 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 = {}) ⇒ Object



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

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

#parse_id(id) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# 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

#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

#quote_column_name(name) ⇒ Object



79
80
81
# File 'lib/rails_admin/adapters/active_record.rb', line 79

def quote_column_name(name)
  model.connection.quote_column_name(name)
end

#quoted_table_nameObject



75
76
77
# File 'lib/rails_admin/adapters/active_record.rb', line 75

def quoted_table_name
  model.quoted_table_name
end

#scopedObject



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

def scoped
  model.all
end