Class: SqlView::ClassBuilder

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

Class Method Summary collapse

Class Method Details

.create_model(parent) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sql_view.rb', line 105

def ClassBuilder.create_model(parent)
  class_name = "#{parent}Model"
  klass = Class.new(ActiveRecord::Base) do
    def self.model_name
      ActiveModel::Name.new(self, nil, parent.view_name)
    end
    def readonly?
      true
    end
    self.table_name = parent.view_name
    self.inheritance_column = nil
  end
  if parent.sql_view_options[:extend_model_with].present?
    klass.class_eval(&parent.sql_view_options[:extend_model_with])
  end
  # to use e.associations.count for example
  # because of the error undefined scan for nil class
  klass.class_eval %Q{
    def self.name
      "#{class_name}"
    end
  }
  Object.const_set(class_name, klass) unless const_defined?(class_name)
  Object.const_get(class_name)
end