Class: Sp2db::ModelTable
Instance Attribute Summary collapse
Attributes inherited from BaseTable
#client, #find_columns, #sheet_name, #spreadsheet_id, #worksheet
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from BaseTable
#csv_data, #csv_file, #csv_folder, #header_row, model_table_class, #process_data, #required_columns, #sp_data, #sp_to_csv, sp_to_csv, #spreadsheet, table_by_names, #to_csv, #write_csv
Constructor Details
#initialize(opts = {}) ⇒ ModelTable
Returns a new instance of ModelTable.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 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
|
Instance Attribute Details
#import_strategy ⇒ Object
Returns the value of attribute import_strategy.
4
5
6
|
# File 'lib/sp2db/model_table.rb', line 4
def import_strategy
@import_strategy
end
|
#model ⇒ Object
Returns the value of attribute model.
4
5
6
|
# File 'lib/sp2db/model_table.rb', line 4
def model
@model
end
|
Class Method Details
.add_models(*models) ⇒ Object
133
134
135
136
137
138
139
|
# 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 ⇒ Object
117
118
119
|
# File 'lib/sp2db/model_table.rb', line 117
def all_models
@all_models ||= {}.with_indifferent_access
end
|
.all_tables ⇒ Object
141
142
143
144
145
|
# 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) ⇒ Object
151
152
153
|
# 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) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
|
# 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) ⇒ Object
147
148
149
|
# 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) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# 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
|
Instance Method Details
#active_record? ⇒ Boolean
24
25
26
|
# File 'lib/sp2db/model_table.rb', line 24
def active_record?
true
end
|
#after_import_row(*args, &block) ⇒ Object
77
78
79
|
# 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) ⇒ Object
81
82
83
|
# 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) ⇒ Object
73
74
75
|
# 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) ⇒ Object
94
95
96
97
98
99
100
101
|
# 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 ⇒ Object
34
35
36
|
# File 'lib/sp2db/model_table.rb', line 34
def config
model.try(:sp2db_config) || super
end
|
#csv_to_db(opts = {}) ⇒ Object
68
69
70
|
# File 'lib/sp2db/model_table.rb', line 68
def csv_to_db opts={}
to_db csv_data, opts
end
|
Tranform data to standard csv format
86
87
88
89
90
91
92
|
# 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
|
#name ⇒ Object
39
40
41
|
# File 'lib/sp2db/model_table.rb', line 39
def name
@name ||= model.table_name.to_sym
end
|
#priority ⇒ Object
50
51
52
|
# File 'lib/sp2db/model_table.rb', line 50
def priority
@priority ||= config[:priority] || 0
end
|
#sp_to_db(opts = {}) ⇒ Object
60
61
62
63
64
65
66
|
# 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) ⇒ Object
54
55
56
57
58
|
# 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
|