Class: Administer::Model

Inherits:
Object
  • Object
show all
Includes:
Fields
Defined in:
lib/administer/model.rb

Constant Summary collapse

@@models =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fields

#fields

Constructor Details

#initialize(model_class) ⇒ Model

Returns a new instance of Model.



33
34
35
36
# File 'lib/administer/model.rb', line 33

def initialize(model_class)
  @entity = model_class
  @config = Administer::Config.for(@entity)
end

Instance Attribute Details

#entityObject

Returns the value of attribute entity.



31
32
33
# File 'lib/administer/model.rb', line 31

def entity
  @entity
end

Class Method Details

.allObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/administer/model.rb', line 8

def all
  if @@models.empty?
    Rails.application.paths.app.models.paths.each do |models_path|
      Dir[File.expand_path("**/*.rb", models_path)].each do |path|
        model_name = path.split('/').last.gsub(/\.rb$/, '')
        @@models << Administer::Model.for(model_name)
      end
    end
  end
  @@models
end

.for(model_name) ⇒ Object



20
21
22
23
# File 'lib/administer/model.rb', line 20

def for(model_name)
  klass = model_name.is_a?(Class) ? model_name : lookup(model_name)
  Model.new(klass)
end

Instance Method Details

#display_name(object) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/administer/model.rb', line 38

def display_name(object)
  display_function = @config.display_name
  if display_function.is_a? Symbol
    return object.send(display_function)
  else
    return object.instance_eval(&display_function)
  end
end