Class: MerbAdmin::AbstractModel

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

Defined Under Namespace

Modules: ActiverecordSupport, DatamapperSupport, GenericSupport

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ AbstractModel

Returns a new instance of AbstractModel.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/abstract_model.rb', line 53

def initialize(model)
  model = self.class.lookup(model.to_s.camel_case) unless model.is_a?(Class)
  @model = model
  self.extend(GenericSupport)
  case Merb.orm
  when :activerecord
    self.extend(ActiverecordSupport)
  when :datamapper
    self.extend(DatamapperSupport)
  else
    raise "MerbAdmin does not support the #{Merb.orm} ORM"
  end
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



51
52
53
# File 'lib/abstract_model.rb', line 51

def model
  @model
end

Class Method Details

.allObject

Returns all models for a given Merb app



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/abstract_model.rb', line 8

def self.all
  return @models if @models
  @models ||= []
  case Merb.orm
  when :activerecord
    Dir.glob(Merb.dir_for(:model) / Merb.glob_for(:model)).each do |filename|
      # FIXME: This heuristic for finding ActiveRecord models is too strict
      File.read(filename).scan(/^class ([\w\d_\-:]+) < ActiveRecord::Base$/).flatten.each do |m|
        model = lookup(m.to_s.to_sym)
        @models << new(model) if model
      end
    end
    @models.sort!{|a, b| a.model.to_s <=> b.model.to_s}
  when :datamapper
    DataMapper::Resource.descendants.each do |m|
      # Remove DataMapperSessionStore because it's included by default
      next if m == Merb::DataMapperSessionStore if Merb.const_defined?(:DataMapperSessionStore)
      model = lookup(m.to_s.to_sym)
      @models << new(model) if model
    end
    @models.sort!{|a, b| a.model.to_s <=> b.model.to_s}
  else
    raise "MerbAdmin does not support the #{Merb.orm} ORM"
  end
end

.lookup(model_name) ⇒ Object

Given a symbol model_name, finds the corresponding model class



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/abstract_model.rb', line 35

def self.lookup(model_name)
  begin
    model = const_get(model_name)
  rescue NameError
    raise "MerbAdmin could not find model #{model_name}"
  end

  case Merb.orm
  when :activerecord
    return model if model.superclass == ActiveRecord::Base
  when :datamapper
    return model if model.include?(DataMapper::Resource)
  end
  nil
end