class Sp2db::ModelTable
Attributes
import_strategy[RW]
model[RW]
Public Class Methods
new(opts={})
click to toggle source
Calls superclass method
Sp2db::BaseTable::new
# File lib/sp2db/model_table.rb, line 7 def initialize opts={} if opts[:name].blank? && opts[:model].present? opts[:name] = opts[:model].table_name.to_sym end super opts if self.model.blank? if opts[:name].present? && model = self.class.model_find_by(name: opts[:name]) \ || opts[:sheet_name].present? && model = self.class.model_find_by(sheet_name: opts[:sheet_name]) self.model = model end end raise "Model cannot be nil" unless self.model.present? end
Private Class Methods
add_models(*models)
click to toggle source
# File lib/sp2db/model_table.rb, line 133 def add_models *models models.each do |m| m = Object.const_get(m) if m.is_a?(String) || m.is_a?(Symbol) raise "Invalid model" unless m.is_a?(Class) && m < ActiveRecord::Base self.all_models[m.table_name] ||= m end end
all_models()
click to toggle source
# File lib/sp2db/model_table.rb, line 117 def all_models @all_models ||= {}.with_indifferent_access end
all_tables()
click to toggle source
# File lib/sp2db/model_table.rb, line 141 def all_tables all_models.map do |name, model| self.new name: name, model: model end.sort_by(&:priority) end
csv_to_db(*table_names)
click to toggle source
# File lib/sp2db/model_table.rb, line 151 def csv_to_db *table_names to_db table_by_names(*table_names), :csv end
model_find_by(name: nil, sheet_name: nil)
click to toggle source
# File lib/sp2db/model_table.rb, line 121 def model_find_by name: nil, sheet_name: nil if name.present? all_models[name] elsif sheet_name.present? all_models.values.find do |model| model.try(:sp2db_sheet_name) == sheet_name end else raise "Invalid arguments" end end
sp_to_db(*table_names)
click to toggle source
# File lib/sp2db/model_table.rb, line 147 def sp_to_db *table_names to_db table_by_names(*table_names), :sp end
to_db(tables, source=:sp)
click to toggle source
# File lib/sp2db/model_table.rb, line 155 def to_db tables, source=:sp res = [] ActiveRecord::Base.transaction(requires_new: true) do tables.each do |tb| begin res << tb.send("#{source}_to_db") rescue ActiveRecord::ActiveRecordError => e next if ExceptionHandler.table_import_error(e) end end end res end
Public Instance Methods
active_record?()
click to toggle source
# File lib/sp2db/model_table.rb, line 24 def active_record? true end
after_import_row(*args, &block)
click to toggle source
# File lib/sp2db/model_table.rb, line 77 def after_import_row *args, &block call_model_sp2db_method __method__, *args, &block end
after_import_table(*args, &block)
click to toggle source
# File lib/sp2db/model_table.rb, line 81 def after_import_table *args, &block call_model_sp2db_method __method__, *args, &block end
before_import_row(*args, &block)
click to toggle source
# File lib/sp2db/model_table.rb, line 73 def before_import_row *args, &block call_model_sp2db_method __method__, *args, &block end
call_process_data(*args, &block)
click to toggle source
Calls superclass method
Sp2db::BaseTable#call_process_data
# File lib/sp2db/model_table.rb, line 94 def call_process_data *args, &block data = if (method = config[:process_data]).is_a?(Symbol) call_model_sp2db_method :process_data, *args, &block else super *args, &block end data end
config()
click to toggle source
Calls superclass method
Sp2db::BaseTable#config
# File lib/sp2db/model_table.rb, line 34 def config model.try(:sp2db_config) || super end
csv_to_db(opts={})
click to toggle source
# File lib/sp2db/model_table.rb, line 68 def csv_to_db opts={} to_db csv_data, opts end
data_transform(*args, &block)
click to toggle source
Tranform data to standard csv format
Calls superclass method
Sp2db::BaseTable#data_transform
# File lib/sp2db/model_table.rb, line 86 def data_transform *args, &block if (data = call_model_sp2db_method __method__, *args, &block).present? data else super *args, &block end end
model=(m)
click to toggle source
# File lib/sp2db/model_table.rb, line 28 def model=m raise "Invalid arguments" unless m && m < ActiveRecord::Base self.class.add_models m @model = m end
name()
click to toggle source
Table name
# File lib/sp2db/model_table.rb, line 39 def name @name ||= model.table_name.to_sym end
priority()
click to toggle source
# File lib/sp2db/model_table.rb, line 50 def priority @priority ||= config[:priority] || 0 end
sp_to_db(opts={})
click to toggle source
# File lib/sp2db/model_table.rb, line 60 def sp_to_db opts={} data = self.sp_data if Sp2db.config.download_before_import write_csv to_csv(data) end to_db data, opts end
to_db(data, strategy: nil)
click to toggle source
# File lib/sp2db/model_table.rb, line 54 def to_db data, strategy: nil strategy = strategy.present? ? ImportStrategy.strategy_by_name : import_strategy strategy = strategy.new self, data res = strategy.import end
Private Instance Methods
call_model_sp2db_method(method_name, *args, &block)
click to toggle source
# File lib/sp2db/model_table.rb, line 105 def call_model_sp2db_method method_name, *args, &block if (method = config[method_name.to_sym]).present? if method.is_a?(Proc) method.call(*args, &block) else model.send method, *args, &block end end end