class RailsAdmin::AbstractModel

Attributes

adapter[R]
model_name[R]

Public Class Methods

all(adapter = nil) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 15
def all(adapter = nil)
  @@all ||= Config.models_pool.collect { |m| new(m) }.compact
  adapter ? @@all.select { |m| m.adapter == adapter } : @@all
end
new(m) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 21
def new(m)
  m = m.constantize unless m.is_a?(Class)
  (am = old_new(m)).model && am.adapter ? am : nil
rescue *([LoadError, NameError] + (defined?(ActiveRecord) ? ['ActiveRecord::NoDatabaseError'.constantize, 'ActiveRecord::ConnectionNotEstablished'.constantize] : []))
  puts "[RailsAdmin] Could not load model #{m}, assuming model is non existing. (#{$ERROR_INFO})" unless Rails.env.test?
  nil
end
Also aliased as: old_new
new(model_or_model_name) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 48
def initialize(model_or_model_name)
  @model_name = model_or_model_name.to_s
  ancestors = model.ancestors.collect(&:to_s)
  if ancestors.include?('ActiveRecord::Base') && !model.abstract_class? && model.table_exists?
    initialize_active_record
  elsif ancestors.include?('Mongoid::Document')
    initialize_mongoid
  end
end
old_new(m)
Alias for: new
polymorphic_parents(adapter, model_name, name) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 31
def polymorphic_parents(adapter, model_name, name)
  @@polymorphic_parents[adapter.to_sym] ||= {}.tap do |hash|
    all(adapter).each do |am|
      am.associations.select(&:as).each do |association|
        (hash[[association.klass.to_s.underscore, association.as].join('_').to_sym] ||= []) << am.model
      end
    end
  end
  @@polymorphic_parents[adapter.to_sym][[model_name.to_s.underscore, name].join('_').to_sym]
end
reset() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 11
def reset
  @@all = nil
end
reset_polymorphic_parents() click to toggle source

For testing

# File lib/rails_admin/abstract_model.rb, line 43
def reset_polymorphic_parents
  @@polymorphic_parents = {}
end

Public Instance Methods

config() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 75
def config
  Config.model self
end
each_associated_children(object) { |association, [child]| ... } click to toggle source
# File lib/rails_admin/abstract_model.rb, line 95
def each_associated_children(object)
  associations.each do |association|
    case association.type
    when :has_one
      child = object.send(association.name)
      yield(association, [child]) if child
    when :has_many
      children = object.send(association.name)
      yield(association, Array.new(children))
    end
  end
end
format_id(id) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 108
def format_id(id)
  id
end
model() click to toggle source

do not store a reference to the model, does not play well with ActiveReload/Rails3.2

# File lib/rails_admin/abstract_model.rb, line 59
def model
  @model_name.constantize
end
param_key() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 83
def param_key
  @model_name.split('::').collect(&:underscore).join('_')
end
parse_id(id) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 112
def parse_id(id)
  id
end
pretty_name() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 87
def pretty_name
  model.model_name.human
end
quote_column_name(name) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 67
def quote_column_name(name)
  name
end
quoted_table_name() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 63
def quoted_table_name
  table_name
end
to_param() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 79
def to_param
  @model_name.split('::').collect(&:underscore).join('~')
end
to_s() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 71
def to_s
  model.to_s
end
where(conditions) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 91
def where(conditions)
  model.where(conditions)
end

Private Instance Methods

initialize_active_record() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 118
def initialize_active_record
  @adapter = :active_record
  require 'rails_admin/adapters/active_record'
  extend Adapters::ActiveRecord
end
initialize_mongoid() click to toggle source
# File lib/rails_admin/abstract_model.rb, line 124
def initialize_mongoid
  @adapter = :mongoid
  require 'rails_admin/adapters/mongoid'
  extend Adapters::Mongoid
end
parse_field_value(field, value) click to toggle source
# File lib/rails_admin/abstract_model.rb, line 130
def parse_field_value(field, value)
  value.is_a?(Array) ? value.map { |v| field.parse_value(v) } : field.parse_value(value)
end