Module: RailsAdmin::Adapters::ActiveRecord

Defined in:
lib/rails_admin/adapters/active_record.rb,
lib/rails_admin/adapters/active_record/abstract_object.rb

Defined Under Namespace

Classes: AbstractObject

Constant Summary collapse

DISABLED_COLUMN_TYPES =
[:tsvector, :blob, :binary, :spatial, :hstore]

Instance Method Summary collapse

Instance Method Details

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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_admin/adapters/active_record.rb', line 37

def all(options = {}, scope = nil)
  scope ||= self.scoped
  scope = scope.includes(options[:include]) if options[:include]
  scope = scope.limit(options[:limit]) if options[:limit]
  scope = scope.where(model.primary_key => options[:bulk_ids]) if options[:bulk_ids]
  scope = scope.where(query_conditions(options[:query])) if options[:query]
  scope = scope.where(filter_conditions(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

#ar_adapterObject



9
10
11
# File 'lib/rails_admin/adapters/active_record.rb', line 9

def ar_adapter
  Rails.configuration.database_configuration[Rails.env]['adapter']
end

#associationsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rails_admin/adapters/active_record.rb', line 63

def associations
  model.reflect_on_all_associations.map do |association|
    {
      :name => association.name.to_sym,
      :pretty_name => association.name.to_s.tr('_', ' ').capitalize,
      :type => association.macro,
      :model_proc => Proc.new { association_model_lookup(association) },
      :primary_key_proc => Proc.new { association_primary_key_lookup(association) },
      :foreign_key => association_foreign_key_lookup(association),
      :foreign_type => association_foreign_type_lookup(association),
      :as => association_as_lookup(association),
      :polymorphic => association_polymorphic_lookup(association),
      :inverse_of => association_inverse_of_lookup(association),
      :read_only => association_read_only_lookup(association),
      :nested_form => association_nested_attributes_options_lookup(association)
    }
  end
end

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



51
52
53
# File 'lib/rails_admin/adapters/active_record.rb', line 51

def count(options = {}, scope = nil)
  all(options.merge({:limit => false, :page => false}), scope).count
end

#destroy(objects) ⇒ Object



55
56
57
# File 'lib/rails_admin/adapters/active_record.rb', line 55

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

#embedded?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/rails_admin/adapters/active_record.rb', line 108

def embedded?
  false
end

#encodingObject



104
105
106
# File 'lib/rails_admin/adapters/active_record.rb', line 104

def encoding
  Rails.configuration.database_configuration[Rails.env]['encoding']
end

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



33
34
35
# File 'lib/rails_admin/adapters/active_record.rb', line 33

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

#get(id) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rails_admin/adapters/active_record.rb', line 21

def get(id)
  if object = model.where(model.primary_key => id).first
    AbstractObject.new object
  else
    nil
  end
end

#like_operatorObject



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

def like_operator
  ar_adapter == "postgresql" ? 'ILIKE' : 'LIKE'
end

#new(params = {}) ⇒ Object



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

def new(params = {})
  AbstractObject.new(model.new(params))
end

#primary_keyObject



59
60
61
# File 'lib/rails_admin/adapters/active_record.rb', line 59

def primary_key
  model.primary_key
end

#propertiesObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_admin/adapters/active_record.rb', line 82

def properties
  columns = model.columns.reject {|c| c.type.blank? || DISABLED_COLUMN_TYPES.include?(c.type.to_sym) }
  columns.map do |property|
    {
      :name => property.name.to_sym,
      :pretty_name => property.name.to_s.tr('_', ' ').capitalize,
      :type => property.type,
      :length => property.limit,
      :nullable? => property.null,
      :serial? => property.primary,
    }
  end
end

#scopedObject



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

def scoped
  model.scoped
end

#serialized_attributesObject



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

def serialized_attributes
  model.serialized_attributes.keys
end

#table_nameObject



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

def table_name
  model.table_name
end