rails_importer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +4 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.md +21 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +125 -0
- data/Rakefile +30 -0
- data/app/controllers/rails_importer/imports_controller.rb +40 -0
- data/app/jobs/import_job.rb +8 -0
- data/app/models/import.rb +11 -0
- data/app/views/rails_importer/imports/new.html.erb +8 -0
- data/bin/console +14 -0
- data/bin/rails +12 -0
- data/bin/setup +8 -0
- data/config/locales/rails_importer.en.yml +4 -0
- data/config/locales/rails_importer.es.yml +4 -0
- data/config/routes.rb +5 -0
- data/lib/generators/rails_importer/importer_generator.rb +17 -0
- data/lib/generators/rails_importer/install_generator.rb +22 -0
- data/lib/generators/rails_importer/templates/importer.rb +9 -0
- data/lib/generators/rails_importer/templates/rails_importer.rb +5 -0
- data/lib/rails_importer.rb +9 -0
- data/lib/rails_importer/base.rb +81 -0
- data/lib/rails_importer/controllers/helpers.rb +18 -0
- data/lib/rails_importer/engine.rb +9 -0
- data/lib/rails_importer/factories.rb +13 -0
- data/lib/rails_importer/setup.rb +18 -0
- data/lib/rails_importer/templates/importer.xlsx +0 -0
- data/lib/rails_importer/version.rb +3 -0
- data/rails_importer.gemspec +35 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2efa0069a20c4c032a168de757a72f39a75c85e3
|
4
|
+
data.tar.gz: af122e4cd9a794292f9218c214d2ae2f7433d1de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54cfa3a1e61009617e04270ac55671263bd020b6e4eb6cd67018ecdf1f714ef81bda6f8e06ffe7210ab4750f829b5ee3a5a46c65f7d98d51d038e7b406211b0a
|
7
|
+
data.tar.gz: babd7b55764ed5f371affd5985dac0a5f48fd57191221951788574652fb6b103e74ec1e0746fbf98556d1f850f7a63f82599cb99bffb745a012322bba5cb4884
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Rails Importer Changelog
|
2
|
+
|
3
|
+
## Keys
|
4
|
+
|
5
|
+
* `[CHANGE]`: A breaking change. After an upgrade, your app may need
|
6
|
+
modifications to keep working correctly.
|
7
|
+
* `[FEATURE]`: A non-breaking improvement to the app. Either introduces new
|
8
|
+
functionality, or improves on an existing feature.
|
9
|
+
* `[UI]`: Non-breaking changes to the default user interface (HTML/CSS).
|
10
|
+
* `[BUGFIX]`: Fixes a bug with a non-breaking change.
|
11
|
+
* `[COMPAT]`: Compatibility improvements - changes to make Administrate more
|
12
|
+
compatible with different dependency versions.
|
13
|
+
* `[I18n]`: Internationalization. Changes to translations or localizations.
|
14
|
+
* `[OPTIM]`: Optimization or performance increase.
|
15
|
+
* `[DOC]`: Documentation changes. No changes to the library's behavior.
|
16
|
+
|
17
|
+
## Changes
|
18
|
+
|
19
|
+
### 0.1.0 (February 12, 2016)
|
20
|
+
|
21
|
+
* [FEATURE] First release
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at sivicencio@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in rails_importer.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development, :test do
|
7
|
+
gem 'sqlite3'
|
8
|
+
gem 'rspec-rails'
|
9
|
+
gem 'shoulda-matchers'
|
10
|
+
gem 'rspec-collection_matchers'
|
11
|
+
gem 'factory_girl_rails'
|
12
|
+
gem 'ffaker'
|
13
|
+
gem "pry"
|
14
|
+
end
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem 'capybara', require: false
|
18
|
+
gem 'selenium-webdriver'
|
19
|
+
|
20
|
+
# Clean database after running test suite
|
21
|
+
gem 'database_cleaner'
|
22
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sebastián Vicencio
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# Rails Importer
|
2
|
+
[](https://travis-ci.org/sivicencio/rails_importer)
|
3
|
+
|
4
|
+
Rails Importer is an engine where importer classes can be defined for massive spreadsheet/csv load of data, based on your needs.
|
5
|
+
|
6
|
+
It allows you to load a spreadsheet using the browser and process it, but you can also use the import logic in a browser-less way. Spreadsheets are loaded and processed using your own **custom rules**. It is flexible enough to let you do things like creating an ActiveRecord model, call third-party services, or anything you wish to do with each row of data.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rails_importer'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install rails_importer
|
23
|
+
|
24
|
+
## Getting Started
|
25
|
+
|
26
|
+
First you need to install gem
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
rails generate rails_importer:install
|
30
|
+
```
|
31
|
+
|
32
|
+
This will add basic setup and an initializer at `config/initializers/rails_importer.rb`
|
33
|
+
|
34
|
+
Here you can basically add new generated importer classes (check [this](#importers) for more details).
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# config/initializers/rails_importer.rb
|
38
|
+
|
39
|
+
RailsImporter.setup do |config|
|
40
|
+
config.importers << ExampleImporter
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Importers
|
45
|
+
An importer can be defined as a class inheriting from `RailsImporter::Base`, and has to at least implement the `load_data` method.
|
46
|
+
|
47
|
+
You can generate an importer class as shown below:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
rails generate rails_importer:importer Example
|
51
|
+
```
|
52
|
+
|
53
|
+
This will do the following:
|
54
|
+
|
55
|
+
- Create new *importer* file at `lib/rails_importer/example_importer.rb`
|
56
|
+
- Update *importers* list inside the initializer
|
57
|
+
|
58
|
+
Importer behavior must be defined in order to correctly import spreadsheet rows. You must implement the `load_data` method, which receives a `row` array, with each element containing the associated column.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
# lib/rails_importer/example_importer.rb
|
62
|
+
|
63
|
+
class ExampleImporter < RailsImporter::Base
|
64
|
+
# Load a file and the get data from each file row
|
65
|
+
#
|
66
|
+
# @params
|
67
|
+
# row => A row to be processed
|
68
|
+
def load_data(row:)
|
69
|
+
# Implement this with custom logic. Otherwise, it will raise an exception
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Note: you can give the importer any name you want, but it is highly recommended to use one that describes its purpose.
|
75
|
+
|
76
|
+
## Routes
|
77
|
+
|
78
|
+
You can mount the engine routes inside your routes file:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# config/routes.rb
|
82
|
+
|
83
|
+
mount RailsImporter::Engine, at: '/'
|
84
|
+
|
85
|
+
# If you need to mount it as a nested resource
|
86
|
+
resources :resource do
|
87
|
+
mount RailsImporter::Engine, at: '/'
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
You'll have basically two routes:
|
92
|
+
|
93
|
+
- `GET importers/:importer_key/new` which defaults to a form with a file field
|
94
|
+
- `POST importers/:importer_key` which tries to process the spreadsheet
|
95
|
+
|
96
|
+
The `importer_key` param is used to determine which importer class must be used.
|
97
|
+
|
98
|
+
|
99
|
+
## Controller
|
100
|
+
|
101
|
+
Routes described above are handled by the `RailsImporter::ImportsController` controller. If you need custom logic, you can override it.
|
102
|
+
|
103
|
+
Besides that, the `after_import_path` controller method is used to redirect after successful imports. It defaults to `root_path`, but you can override it if you wish a custom path:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# app/controllers/application_controller.rb
|
107
|
+
|
108
|
+
def after_import_path
|
109
|
+
# Change this if you wish to set a different path
|
110
|
+
context.root_path
|
111
|
+
end
|
112
|
+
```
|
113
|
+
The `context` variable used above contains the app routes, so don't forget to use it.
|
114
|
+
|
115
|
+
## Import Job
|
116
|
+
When processing an import file, an ActiveJob instance is used. If you're deploying to production, you will want to use some active job adapter, such as [Sidekiq](https://github.com/mperham/sidekiq).
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sivicencio/rails_importer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
121
|
+
|
122
|
+
|
123
|
+
## License
|
124
|
+
|
125
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsImporter'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/example_app/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
Bundler::GemHelper.install_tasks
|
25
|
+
|
26
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
27
|
+
t.verbose = false
|
28
|
+
end
|
29
|
+
|
30
|
+
task default: :spec
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module RailsImporter
|
2
|
+
class ImportsController < ::ApplicationController
|
3
|
+
before_filter :set_importer_class
|
4
|
+
|
5
|
+
def new
|
6
|
+
@import = Import.new(importer_key: @importer_class.key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def create
|
10
|
+
@import = Import.new(import_params)
|
11
|
+
if @import.valid?
|
12
|
+
ImportJob.perform_later @import.importer_key, @import.filepath
|
13
|
+
flash[:notice] = I18n.t(:processing_import, scope: :rails_importer)
|
14
|
+
redirect_to after_import_path
|
15
|
+
else
|
16
|
+
render 'new'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# def template
|
21
|
+
# file = File.open(@importer_class.sample_file)
|
22
|
+
|
23
|
+
# send_data file.read, :filename => File.basename(@importer.sample_file)
|
24
|
+
# end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def set_importer_class
|
29
|
+
@importer_class = RailsImporter.importer_class(params[:importer_key].to_s)
|
30
|
+
if @importer_class.nil?
|
31
|
+
flash[:error] = I18n.t(:importer_does_not_exist, scope: :rails_importer)
|
32
|
+
redirect_to after_import_path
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def import_params
|
37
|
+
params.require(:import).permit(:importer_key, :import_file)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rails_importer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/rails
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application.
|
3
|
+
|
4
|
+
ENGINE_ROOT = File.expand_path('../..', __FILE__)
|
5
|
+
ENGINE_PATH = File.expand_path('../../lib/rails_importer/engine', __FILE__)
|
6
|
+
|
7
|
+
# Set up gems listed in the Gemfile.
|
8
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
|
9
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
10
|
+
|
11
|
+
require 'rails/all'
|
12
|
+
require 'rails/engine/commands'
|
data/bin/setup
ADDED
data/config/routes.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module RailsImporter
|
2
|
+
module Generators
|
3
|
+
class ImporterGenerator < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
desc "Creates an importer class inheriting from RailsImporter::Base"
|
7
|
+
|
8
|
+
def add_importer_class
|
9
|
+
template('importer.rb', "lib/rails_importer/#{file_name}_importer.rb")
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_to_importers
|
13
|
+
insert_into_file "config/initializers/rails_importer.rb", "\tconfig.importers << #{class_name}Importer\n", :before => "end"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RailsImporter
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
desc "Creates a RailsImporter initializer file at config/initializers"
|
7
|
+
|
8
|
+
def copy_initializer
|
9
|
+
template "rails_importer.rb", "config/initializers/rails_importer.rb"
|
10
|
+
end
|
11
|
+
|
12
|
+
def include_spree_importer_core_from_lib
|
13
|
+
inject_into_file 'config/application.rb',
|
14
|
+
"\n\t\t# Load application's importers"\
|
15
|
+
"\n\t\tDir.glob(File.join(File.dirname(__FILE__), \"../lib/rails_importer/*_importer.rb\")) do |c|"\
|
16
|
+
"\n\t\t\tRails.configuration.cache_classes ? require(c) : load(c)"\
|
17
|
+
"\n\t\tend\n",
|
18
|
+
:after => "< Rails::Application", :verbose => true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class <%= class_name %>Importer < RailsImporter::Base
|
2
|
+
# Load a file and the get data from each file row
|
3
|
+
#
|
4
|
+
# @params
|
5
|
+
# row => A row to be processed
|
6
|
+
def load_data(row:)
|
7
|
+
# Implement this with custom logic. Otherwise, it will raise an exception
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'roo'
|
2
|
+
|
3
|
+
module RailsImporter
|
4
|
+
class Base
|
5
|
+
attr_reader :messages
|
6
|
+
|
7
|
+
def initialize filepath, options={}
|
8
|
+
if File.exists?(filepath)
|
9
|
+
@filepath = filepath
|
10
|
+
end
|
11
|
+
|
12
|
+
@spreadsheet = nil
|
13
|
+
|
14
|
+
@messages = []
|
15
|
+
|
16
|
+
@rows = 0
|
17
|
+
|
18
|
+
begin
|
19
|
+
open_spreadsheet
|
20
|
+
|
21
|
+
@rows = @spreadsheet.last_row
|
22
|
+
rescue => e
|
23
|
+
add_error message: e.message, backtrace: e.backtrace, row_index: nil, data: {}
|
24
|
+
end
|
25
|
+
|
26
|
+
# Custom behavior
|
27
|
+
end
|
28
|
+
|
29
|
+
# A unique key identifier for importer
|
30
|
+
def self.key
|
31
|
+
self.to_s.gsub('Importer', '').demodulize.underscore
|
32
|
+
end
|
33
|
+
|
34
|
+
# Load a file and the get data from each file row
|
35
|
+
def process
|
36
|
+
# Load each row element
|
37
|
+
2.upto(@rows).each do |row_index|
|
38
|
+
ActiveRecord::Base.transaction do
|
39
|
+
begin
|
40
|
+
load_data row: @spreadsheet.row(row_index)
|
41
|
+
|
42
|
+
rescue => e
|
43
|
+
add_error message: e.message, backtrace: e.backtrace, row_index: row_index, data: @spreadsheet.row(row_index)
|
44
|
+
|
45
|
+
raise ActiveRecord::Rollback
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Load a file and the get data from each file row
|
52
|
+
#
|
53
|
+
# @params
|
54
|
+
# row => A row to be processed
|
55
|
+
def load_data(row:)
|
56
|
+
raise "#{__FILE__}:#{__LINE__} You must define it"
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# Returns a Roo instance acording the file extension.
|
62
|
+
def open_spreadsheet sheet_name = ''
|
63
|
+
extension = File.extname(@filepath.split("/").last)
|
64
|
+
@spreadsheet = Roo::Spreadsheet.open(@filepath, extension: extension)
|
65
|
+
@spreadsheet.default_sheet = @spreadsheet.sheets.include?(sheet_name) ? sheet_name : @spreadsheet.sheets.first
|
66
|
+
rescue => e
|
67
|
+
add_error message: e.message, backtrace: e.backtrace, row_index: nil, data: {}
|
68
|
+
end
|
69
|
+
|
70
|
+
# Add errors to import
|
71
|
+
#
|
72
|
+
# @params
|
73
|
+
# message => Error message
|
74
|
+
# backtrace => Error Bactrace
|
75
|
+
# row_index => Failed row index
|
76
|
+
# data => Readed data
|
77
|
+
def add_error(message:, backtrace:, row_index:, data:)
|
78
|
+
@messages << {message: message, backtrace: backtrace, row_index: row_index, data: data}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module RailsImporter
|
2
|
+
module Controllers
|
3
|
+
module Helpers
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def after_import_path
|
7
|
+
context.root_path
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def context
|
13
|
+
router_name = RailsImporter.router_name
|
14
|
+
context = router_name ? send(router_name) : self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
FactoryGirl.define do
|
2
|
+
factory :import do
|
3
|
+
importer_key "importer"
|
4
|
+
import_file {
|
5
|
+
ActionDispatch::Http::UploadedFile.new({ filename: 'importer.xlsx',
|
6
|
+
tempfile: File.new("lib/rails_importer/templates/importer.xlsx")
|
7
|
+
})}
|
8
|
+
end
|
9
|
+
|
10
|
+
factory :invalid_import, class: Import do
|
11
|
+
importer_key nil
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module RailsImporter
|
2
|
+
module Setup
|
3
|
+
# Importer classes
|
4
|
+
mattr_accessor :importers
|
5
|
+
@@importers = []
|
6
|
+
|
7
|
+
mattr_accessor :router_name
|
8
|
+
@@router_name = :main_app
|
9
|
+
|
10
|
+
def setup
|
11
|
+
yield self if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def importer_class(importer_key)
|
15
|
+
@@importers.select{|i| i.key.to_s == importer_key.to_s}.first
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
Binary file
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_importer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_importer"
|
8
|
+
spec.version = RailsImporter::VERSION
|
9
|
+
spec.authors = ["Sebastián Vicencio"]
|
10
|
+
spec.email = ["sivicencio@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Import engine for Rails}
|
13
|
+
spec.description = %q{Rails Engine where importers can be defined for massive spreadsheet/csv load of data, based on your needs}
|
14
|
+
spec.homepage = "https://github.com/sivicencio/rails_importer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "rails", "~> 4.2"
|
31
|
+
spec.add_dependency "roo", "~> 2.0"
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_importer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sebastián Vicencio
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-12 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: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: roo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Rails Engine where importers can be defined for massive spreadsheet/csv
|
70
|
+
load of data, based on your needs
|
71
|
+
email:
|
72
|
+
- sivicencio@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- CODE_OF_CONDUCT.md
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- app/controllers/rails_importer/imports_controller.rb
|
87
|
+
- app/jobs/import_job.rb
|
88
|
+
- app/models/import.rb
|
89
|
+
- app/views/rails_importer/imports/new.html.erb
|
90
|
+
- bin/console
|
91
|
+
- bin/rails
|
92
|
+
- bin/setup
|
93
|
+
- config/locales/rails_importer.en.yml
|
94
|
+
- config/locales/rails_importer.es.yml
|
95
|
+
- config/routes.rb
|
96
|
+
- lib/generators/rails_importer/importer_generator.rb
|
97
|
+
- lib/generators/rails_importer/install_generator.rb
|
98
|
+
- lib/generators/rails_importer/templates/importer.rb
|
99
|
+
- lib/generators/rails_importer/templates/rails_importer.rb
|
100
|
+
- lib/rails_importer.rb
|
101
|
+
- lib/rails_importer/base.rb
|
102
|
+
- lib/rails_importer/controllers/helpers.rb
|
103
|
+
- lib/rails_importer/engine.rb
|
104
|
+
- lib/rails_importer/factories.rb
|
105
|
+
- lib/rails_importer/setup.rb
|
106
|
+
- lib/rails_importer/templates/importer.xlsx
|
107
|
+
- lib/rails_importer/version.rb
|
108
|
+
- rails_importer.gemspec
|
109
|
+
homepage: https://github.com/sivicencio/rails_importer
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata:
|
113
|
+
allowed_push_host: https://rubygems.org
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.5.1
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Import engine for Rails
|
134
|
+
test_files: []
|