Module: AutoAR

Defined in:
lib/auto_ar.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/auto_ar.rb', line 6

def self.included(base)
  base.class_eval do
    unless defined? const_missing_without_auto_ar
      alias_method_chain :const_missing, :auto_ar
    end
  end
end

Instance Method Details

#const_missing_with_auto_ar(const_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/auto_ar.rb', line 13

def const_missing_with_auto_ar(const_name)
  begin
    const_missing_without_auto_ar(const_name)
  rescue NameError => e
    begin
      conn = ActiveRecord::Base.connection
      if conn.tables.include?(const_name.to_s.tableize)
        Object.const_set(const_name, Class.new(ActiveRecord::Base))
      else
        raise NameError
      end
    rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::StatementInvalid
      raise NameError
    end
  end
end