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]
AR_ADAPTER =
::ActiveRecord::Base.configurations[Rails.env]
LIKE_OPERATOR =
AR_ADAPTER == "postgresql" ? 'ILIKE' : 'LIKE'

Instance Method Summary collapse

Instance Method Details

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_admin/adapters/active_record.rb', line 31

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]
  scope = scope.page(options[:page]).per(options[:per]) if options[:page] && options[:per]
  scope = scope.reorder("#{options[:sort]} #{options[:sort_reverse] ? 'asc' : 'desc'}") if options[:sort]
  scope
end

#associationsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_admin/adapters/active_record.rb', line 55

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



43
44
45
# File 'lib/rails_admin/adapters/active_record.rb', line 43

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

#destroy(objects) ⇒ Object



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

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

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



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

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

#get(id) ⇒ Object



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

def get(id)
  if object = model.where(model.primary_key => id).first
    AbstractObject.new object
  else
    nil
  end
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

#primary_keyObject



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

def primary_key
  model.primary_key
end

#propertiesObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rails_admin/adapters/active_record.rb', line 74

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



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

def scoped
  model.scoped
end

#serialized_attributesObject



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

def serialized_attributes
  model.serialized_attributes.keys
end

#table_nameObject



88
89
90
# File 'lib/rails_admin/adapters/active_record.rb', line 88

def table_name
  model.table_name
end