sp2db 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +180 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +180 -0
- data/Rakefile +10 -0
- data/lib/generators/sp2db/config_generator.rb +13 -0
- data/lib/generators/sp2db/templates/sp2db.rb +27 -0
- data/lib/sp2db/base_table.rb +238 -0
- data/lib/sp2db/client.rb +56 -0
- data/lib/sp2db/config.rb +82 -0
- data/lib/sp2db/exception_handler.rb +26 -0
- data/lib/sp2db/import_concern.rb +84 -0
- data/lib/sp2db/import_strategy.rb +157 -0
- data/lib/sp2db/logging.rb +29 -0
- data/lib/sp2db/model_table.rb +171 -0
- data/lib/sp2db/non_model_table.rb +17 -0
- data/lib/sp2db/spreadsheet.rb +19 -0
- data/lib/sp2db/version.rb +3 -0
- data/lib/sp2db.rb +55 -0
- data/lib/tasks/sp2db.rake +12 -0
- data/sp2db.gemspec +32 -0
- metadata +153 -0
@@ -0,0 +1,171 @@
|
|
1
|
+
module Sp2db
|
2
|
+
class ModelTable < BaseTable
|
3
|
+
|
4
|
+
attr_accessor :model,
|
5
|
+
:import_strategy
|
6
|
+
|
7
|
+
def initialize opts={}
|
8
|
+
if opts[:name].blank? && opts[:model].present?
|
9
|
+
opts[:name] = opts[:model].table_name.to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
super opts
|
13
|
+
|
14
|
+
if self.model.blank?
|
15
|
+
if opts[:name].present? && model = self.class.model_find_by(name: opts[:name]) \
|
16
|
+
|| opts[:sheet_name].present? && model = self.class.model_find_by(sheet_name: opts[:sheet_name])
|
17
|
+
self.model = model
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
raise "Model cannot be nil" unless self.model.present?
|
22
|
+
end
|
23
|
+
|
24
|
+
def active_record?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def model=m
|
29
|
+
raise "Invalid arguments" unless m && m < ActiveRecord::Base
|
30
|
+
self.class.add_models m
|
31
|
+
@model = m
|
32
|
+
end
|
33
|
+
|
34
|
+
def config
|
35
|
+
model.try(:sp2db_config) || super
|
36
|
+
end
|
37
|
+
|
38
|
+
# Table name
|
39
|
+
def name
|
40
|
+
@name ||= model.table_name.to_sym
|
41
|
+
end
|
42
|
+
|
43
|
+
def import_strategy
|
44
|
+
return @import_strategy if @import_strategy.present?
|
45
|
+
strategy_name = config[:import_strategy] ||
|
46
|
+
Sp2db.config.import_strategy
|
47
|
+
@import_strategy = ImportStrategy.strategy_by_name(strategy_name)
|
48
|
+
end
|
49
|
+
|
50
|
+
def priority
|
51
|
+
@priority ||= config[:priority] || 0
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_db data, strategy: nil
|
55
|
+
strategy = strategy.present? ? ImportStrategy.strategy_by_name : import_strategy
|
56
|
+
strategy = strategy.new self, data
|
57
|
+
res = strategy.import
|
58
|
+
end
|
59
|
+
|
60
|
+
def sp_to_db opts={}
|
61
|
+
data = self.sp_data
|
62
|
+
if Sp2db.config.download_before_import
|
63
|
+
write_csv to_csv(data)
|
64
|
+
end
|
65
|
+
to_db data, opts
|
66
|
+
end
|
67
|
+
|
68
|
+
def csv_to_db opts={}
|
69
|
+
to_db csv_data, opts
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def before_import_row *args, &block
|
74
|
+
call_model_sp2db_method __method__, *args, &block
|
75
|
+
end
|
76
|
+
|
77
|
+
def after_import_row *args, &block
|
78
|
+
call_model_sp2db_method __method__, *args, &block
|
79
|
+
end
|
80
|
+
|
81
|
+
def after_import_table *args, &block
|
82
|
+
call_model_sp2db_method __method__, *args, &block
|
83
|
+
end
|
84
|
+
|
85
|
+
# Tranform data to standard csv format
|
86
|
+
def data_transform *args, &block
|
87
|
+
if (data = call_model_sp2db_method __method__, *args, &block).present?
|
88
|
+
data
|
89
|
+
else
|
90
|
+
super *args, &block
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def call_process_data *args, &block
|
95
|
+
data = if (method = config[:process_data]).is_a?(Symbol)
|
96
|
+
call_model_sp2db_method :process_data, *args, &block
|
97
|
+
else
|
98
|
+
super *args, &block
|
99
|
+
end
|
100
|
+
data
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def call_model_sp2db_method method_name, *args, &block
|
106
|
+
if (method = config[method_name.to_sym]).present?
|
107
|
+
if method.is_a?(Proc)
|
108
|
+
method.call(*args, &block)
|
109
|
+
else
|
110
|
+
model.send method, *args, &block
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
class << self
|
116
|
+
|
117
|
+
def all_models
|
118
|
+
@all_models ||= {}.with_indifferent_access
|
119
|
+
end
|
120
|
+
|
121
|
+
def model_find_by name: nil, sheet_name: nil
|
122
|
+
if name.present?
|
123
|
+
all_models[name]
|
124
|
+
elsif sheet_name.present?
|
125
|
+
all_models.values.find do |model|
|
126
|
+
model.try(:sp2db_sheet_name) == sheet_name
|
127
|
+
end
|
128
|
+
else
|
129
|
+
raise "Invalid arguments"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def add_models *models
|
134
|
+
models.each do |m|
|
135
|
+
m = Object.const_get(m) if m.is_a?(String) || m.is_a?(Symbol)
|
136
|
+
raise "Invalid model" unless m.is_a?(Class) && m < ActiveRecord::Base
|
137
|
+
self.all_models[m.table_name] ||= m
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def all_tables
|
142
|
+
all_models.map do |name, model|
|
143
|
+
self.new name: name, model: model
|
144
|
+
end.sort_by(&:priority)
|
145
|
+
end
|
146
|
+
|
147
|
+
def sp_to_db *table_names
|
148
|
+
to_db table_by_names(*table_names), :sp
|
149
|
+
end
|
150
|
+
|
151
|
+
def csv_to_db *table_names
|
152
|
+
to_db table_by_names(*table_names), :csv
|
153
|
+
end
|
154
|
+
|
155
|
+
def to_db tables, source=:sp
|
156
|
+
res = []
|
157
|
+
ActiveRecord::Base.transaction(requires_new: true) do
|
158
|
+
tables.each do |tb|
|
159
|
+
begin
|
160
|
+
res << tb.send("#{source}_to_db")
|
161
|
+
rescue ActiveRecord::ActiveRecordError => e
|
162
|
+
next if ExceptionHandler.table_import_error(e)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
res
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Sp2db
|
2
|
+
class NonModelTable < BaseTable
|
3
|
+
|
4
|
+
def config
|
5
|
+
@config ||= Sp2db.config.non_model_tables[self.name] || super
|
6
|
+
end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def all_tables
|
10
|
+
Sp2db.config.non_model_tables.map do |name, config|
|
11
|
+
self.new name: name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Sp2db
|
2
|
+
class Spreadsheet
|
3
|
+
|
4
|
+
attr_accessor :sheet
|
5
|
+
|
6
|
+
def initialize sheet
|
7
|
+
self.sheet = sheet
|
8
|
+
end
|
9
|
+
|
10
|
+
def worksheets
|
11
|
+
sheet.worksheets.index_by(&:title).with_indifferent_access
|
12
|
+
end
|
13
|
+
|
14
|
+
def worksheet_by_name ws_name
|
15
|
+
sheet.worksheet_by_title ws_name
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/lib/sp2db.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require "google_drive"
|
2
|
+
require "sp2db/logging"
|
3
|
+
|
4
|
+
require "sp2db/config"
|
5
|
+
require "sp2db/client"
|
6
|
+
require "sp2db/exception_handler"
|
7
|
+
require "sp2db/import_strategy"
|
8
|
+
require "sp2db/base_table"
|
9
|
+
require "sp2db/non_model_table"
|
10
|
+
require "sp2db/model_table"
|
11
|
+
require "sp2db/spreadsheet"
|
12
|
+
require "sp2db/import_concern"
|
13
|
+
require "sp2db/version"
|
14
|
+
|
15
|
+
module Sp2db
|
16
|
+
|
17
|
+
include Logging
|
18
|
+
extend self
|
19
|
+
|
20
|
+
# @!attribute [rw] config
|
21
|
+
def config
|
22
|
+
@config ||= Config.new
|
23
|
+
yield @config if block_given?
|
24
|
+
Rails.application.eager_load!
|
25
|
+
@config
|
26
|
+
end
|
27
|
+
|
28
|
+
# Reload all configs and sessions
|
29
|
+
def reload!
|
30
|
+
@client = nil
|
31
|
+
end
|
32
|
+
|
33
|
+
# return [Gclient]
|
34
|
+
def client c=nil
|
35
|
+
Client.new
|
36
|
+
end
|
37
|
+
|
38
|
+
# Default sheet
|
39
|
+
def spreadsheet
|
40
|
+
client.spreadsheet Sp2db.config.spreadsheet_id
|
41
|
+
end
|
42
|
+
|
43
|
+
delegate :sp_to_csv,
|
44
|
+
:sp_to_db,
|
45
|
+
:csv_to_db,
|
46
|
+
to: BaseTable
|
47
|
+
end
|
48
|
+
|
49
|
+
class Railtie < Rails::Railtie
|
50
|
+
railtie_name :sp2db
|
51
|
+
|
52
|
+
rake_tasks do
|
53
|
+
load "tasks/sp2db.rake"
|
54
|
+
end
|
55
|
+
end
|
data/sp2db.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "sp2db/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sp2db"
|
8
|
+
spec.version = Sp2db::VERSION
|
9
|
+
spec.authors = ["KhiemNS"]
|
10
|
+
spec.email = ["khiemns.k54@gmail.com.com"]
|
11
|
+
|
12
|
+
spec.summary = "Google Spreadsheet importer for Rails app."
|
13
|
+
spec.description = "Google Spreadsheet importer for Rails app."
|
14
|
+
spec.homepage = "https://github.com/khiemns54/sp2db"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "rails", "~> 5.0"
|
25
|
+
spec.add_dependency "google_drive", "~> 2.1"
|
26
|
+
spec.add_dependency "google-api-client", "~> 0.11"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sp2db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- KhiemNS
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: google_drive
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: google-api-client
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.11'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.16'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.16'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
description: Google Spreadsheet importer for Rails app.
|
98
|
+
email:
|
99
|
+
- khiemns.k54@gmail.com.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
|
+
- CODE_OF_CONDUCT.md
|
107
|
+
- Gemfile
|
108
|
+
- Gemfile.lock
|
109
|
+
- LICENSE
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- lib/generators/sp2db/config_generator.rb
|
114
|
+
- lib/generators/sp2db/templates/sp2db.rb
|
115
|
+
- lib/sp2db.rb
|
116
|
+
- lib/sp2db/base_table.rb
|
117
|
+
- lib/sp2db/client.rb
|
118
|
+
- lib/sp2db/config.rb
|
119
|
+
- lib/sp2db/exception_handler.rb
|
120
|
+
- lib/sp2db/import_concern.rb
|
121
|
+
- lib/sp2db/import_strategy.rb
|
122
|
+
- lib/sp2db/logging.rb
|
123
|
+
- lib/sp2db/model_table.rb
|
124
|
+
- lib/sp2db/non_model_table.rb
|
125
|
+
- lib/sp2db/spreadsheet.rb
|
126
|
+
- lib/sp2db/version.rb
|
127
|
+
- lib/tasks/sp2db.rake
|
128
|
+
- sp2db.gemspec
|
129
|
+
homepage: https://github.com/khiemns54/sp2db
|
130
|
+
licenses:
|
131
|
+
- MIT
|
132
|
+
metadata: {}
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
requirements: []
|
148
|
+
rubyforge_project:
|
149
|
+
rubygems_version: 2.5.1
|
150
|
+
signing_key:
|
151
|
+
specification_version: 4
|
152
|
+
summary: Google Spreadsheet importer for Rails app.
|
153
|
+
test_files: []
|