Class: SimpleMaster::Loader::QueryLoader
- Inherits:
-
SimpleMaster::Loader
- Object
- SimpleMaster::Loader
- SimpleMaster::Loader::QueryLoader
- Defined in:
- lib/simple_master/loader/query_loader.rb
Instance Attribute Summary
Attributes inherited from SimpleMaster::Loader
Instance Method Summary collapse
Methods inherited from SimpleMaster::Loader
#globalize, #initialize, #load_records
Constructor Details
This class inherits a constructor from SimpleMaster::Loader
Instance Method Details
#build_records(klass, raw) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simple_master/loader/query_loader.rb', line 17 def build_records(klass, raw) columns = raw[:columns].map(&:to_sym) column_assign_methods = columns.map { :"#{_1}=" } columns_hash = klass.columns_hash sti_column_index = columns.find_index { columns_hash[_1].is_a?(Master::Column::StiTypeColumn) } columns.each do |column_name| unless klass.method_defined?(:"#{column_name}=") if ENV["RAILS_ENV"] == "development" # In local/dev, define a no-op setter so loading does not raise klass.define_method(:"#{column_name}=", &:itself) warn "#{klass}.#{column_name} column is not defined!" else fail "#{klass}.#{column_name} column is not defined!" end end end raw[:rows].filter_map { |row| begin record = if sti_column_index child_klass = ActiveSupport::Inflector.constantize(row[sti_column_index]) if child_klass == klass || child_klass.sti_base_class == klass child_klass.new else warn "[#{klass}] Invalid value in the type column: #{row}" next end else klass.new end column_assign_methods.zip(row) do |assign_method, value| record.send(assign_method, value) end record rescue nil end }.freeze end |
#read_raw(table) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/simple_master/loader/query_loader.rb', line 6 def read_raw(table) klass = table.klass unless klass.table_available? return { columns: EMPTY_ARRAY, rows: EMPTY_ARRAY } end result = klass.query_select_all { columns: result.columns, rows: result.rows } end |