Class: RailsAdmin::AbstractModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin/abstract_model.rb,
lib/rails_admin/generic_support.rb

Defined Under Namespace

Modules: GenericSupport

Constant Summary collapse

@@all_models =
nil
@@all_abstract_models =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ AbstractModel

Returns a new instance of AbstractModel.



45
46
47
48
49
50
51
52
# File 'lib/rails_admin/abstract_model.rb', line 45

def initialize(model)
  model = self.class.lookup(model.to_s.camelize) unless model.is_a?(Class)
  @model_name = model.name
  self.extend(GenericSupport)
  ### TODO more ORMs support
  require 'rails_admin/adapters/active_record'
  self.extend(RailsAdmin::Adapters::ActiveRecord)
end

Class Method Details

.allObject

self.all_abstract_models



12
13
14
# File 'lib/rails_admin/abstract_model.rb', line 12

def self.all
  @@all_abstract_models ||= all_models.map{ |model| new(model) }
end

.all_modelsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_admin/abstract_model.rb', line 16

def self.all_models
  @@all_models ||= (
    possible_models = RailsAdmin::Config.included_models.map(&:to_s).presence || ([Rails.application] + Rails::Application::Railties.engines).map do |app|
      (app.paths['app/models'] + app.config.autoload_paths).map do |load_path|
        Dir.glob(app.root.join(load_path)).map do |load_dir|
          Dir.glob(load_dir + "/**/*.rb").map do |filename|
            lchomp(filename, "#{app.root.join(load_dir)}/").chomp('.rb').camelize  # app/models/module/class.rb => module/class.rb => module/class => Module::Class
          end
        end
      end
    end.flatten
    excluded_models = (RailsAdmin::Config.excluded_models.map(&:to_s) + ['RailsAdmin::History'])
    (possible_models - excluded_models).uniq.sort{|x, y| x.to_s <=> y.to_s}.map{|model| lookup(model) }.compact
  )
end

.lookup(model_name) ⇒ Object

Given a string model_name, finds the corresponding model class



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rails_admin/abstract_model.rb', line 33

def self.lookup(model_name)
  model = model_name.constantize rescue nil
  if model && model.is_a?(Class) && superclasses(model).include?(ActiveRecord::Base) && !model.abstract_class?
    model
  else
    nil
  end
rescue LoadError
  Rails.logger.error "Error while loading '#{model_name}': #{$!}"
  nil
end

Instance Method Details

#modelObject



54
55
56
# File 'lib/rails_admin/abstract_model.rb', line 54

def model
  @model_name.constantize
end