Class: Crudboy::Definition

Inherits:
Object show all
Defined in:
lib/crudboy/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Definition

Returns a new instance of Definition.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/crudboy/definition.rb', line 64

def initialize(options)
  @@options = options
  @table_name = @@options[:table_name]
  @model_name = @@options[:model_name]
  ActiveRecord::Base.connection.tap do |conn|
    Object.const_set('CrudboyModel', Class.new(ActiveRecord::Base) do
      include ::Crudboy::Concerns::TableDataDefinition
      self.abstract_class = true
    end)

    raise "Table not exist: #{@table_name}" unless conn.tables.include?(@table_name)

    table_comment = conn.table_comment(@table_name)
    conn.primary_key(@table_name).tap do |pkey|
      Class.new(::CrudboyModel) do
        include Crudboy::Extension
        self.table_name = options[:table_name]
        if pkey.is_a?(Array)
          self.primary_keys = pkey
        else
          self.primary_key = pkey
        end
        self.inheritance_column = nil
        self.default_timezone = :local
        if options[:created_at].present?
          define_singleton_method :timestamp_attributes_for_create do
            options[:created_at]
          end
        end

        if options[:updated_at].present?
          define_singleton_method :timestamp_attributes_for_update do
            options[:updated_at]
          end
        end
      end.tap do |clazz|
        Object.const_set(@model_name, clazz).tap do |const|
          @model = Model.new(const, @table_name, table_comment)
        end
      end
    end
  end
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



62
63
64
# File 'lib/crudboy/definition.rb', line 62

def model
  @model
end

#model_nameObject

Returns the value of attribute model_name.



62
63
64
# File 'lib/crudboy/definition.rb', line 62

def model_name
  @model_name
end

#table_nameObject

Returns the value of attribute table_name.



62
63
64
# File 'lib/crudboy/definition.rb', line 62

def table_name
  @table_name
end