Class: Plog::RecordAbstraction

Inherits:
Object
  • Object
show all
Defined in:
lib/plog/record_abstraction.rb

Class Method Summary collapse

Class Method Details

.migration_classObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plog/record_abstraction.rb', line 27

def migration_class
  "
  class ::#{migration_name} < ActiveRecord::Migration
    def self.up
      create_table :#{@table[:table_name]} do |t|
        #{@table[:fields].map { |field, type| 't.' + type.to_s + ' :' + field.to_s } * "\n            " }
        t.timestamps
      end
    end
  end
  "
end

.migration_constantObject



45
46
47
48
# File 'lib/plog/record_abstraction.rb', line 45

def migration_constant
  eval(migration_class)
  eval('::' + migration_name)
end

.migration_nameObject



11
12
13
# File 'lib/plog/record_abstraction.rb', line 11

def migration_name
  "#{record_name}Migration"
end

.record_classObject



19
20
21
22
23
24
25
# File 'lib/plog/record_abstraction.rb', line 19

def record_class
  "
  class ::#{record_name} < ActiveRecord::Base
    set_table_name :#{record_name.underscore}
  end
  "
end

.record_constantObject



40
41
42
43
# File 'lib/plog/record_abstraction.rb', line 40

def record_constant
  eval(record_class)
  eval('::' + record_name)
end

.record_exists?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/plog/record_abstraction.rb', line 50

def record_exists?
  ActiveRecord::Base.connection.execute('show tables').each do |record|
    return true if record.first == @table[:table_name].to_s
  end
  false
end

.record_nameObject



15
16
17
# File 'lib/plog/record_abstraction.rb', line 15

def record_name
  "#{@table[:table_name].to_s.camelize}"
end

.setup(table) ⇒ Object



7
8
9
# File 'lib/plog/record_abstraction.rb', line 7

def setup(table)
  @table = table
end