rails-exporter 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +34 -9
- data/Rakefile +12 -11
- data/config/locales/exporters.en.yml +9 -2
- data/lib/rails_exporter/base.rb +8 -7
- data/lib/rails_exporter/engine.rb +5 -0
- data/lib/rails_exporter/exporter.rb +42 -8
- data/lib/rails_exporter/helper.rb +4 -4
- data/lib/rails_exporter/version.rb +1 -1
- data/spec/rails_exporter/base_spec.rb +11 -0
- data/spec/rails_exporter/engine_spec.rb +5 -0
- data/spec/rails_exporter/exporter_spec.rb +9 -0
- data/spec/rails_exporter/helper_spec.rb +5 -0
- data/spec/rails_exporter_spec.rb +7 -0
- data/spec/rails_helper.rb +66 -0
- data/spec/spec_helper.rb +100 -0
- data/spec/test_app/Rakefile +6 -0
- data/spec/test_app/app/assets/config/manifest.js +4 -0
- data/spec/test_app/app/assets/javascripts/application.js +13 -0
- data/spec/test_app/app/assets/javascripts/cable.js +13 -0
- data/spec/test_app/app/assets/stylesheets/application.css +15 -0
- data/spec/test_app/app/channels/application_cable/channel.rb +4 -0
- data/spec/test_app/app/channels/application_cable/connection.rb +4 -0
- data/spec/test_app/app/controllers/application_controller.rb +3 -0
- data/spec/test_app/app/helpers/application_helper.rb +2 -0
- data/spec/test_app/app/jobs/application_job.rb +2 -0
- data/spec/test_app/app/mailers/application_mailer.rb +4 -0
- data/spec/test_app/app/models/application_record.rb +3 -0
- data/spec/test_app/app/views/layouts/application.html.erb +14 -0
- data/spec/test_app/app/views/layouts/mailer.html.erb +13 -0
- data/spec/test_app/app/views/layouts/mailer.text.erb +1 -0
- data/spec/test_app/bin/bundle +3 -0
- data/spec/test_app/bin/rails +4 -0
- data/spec/test_app/bin/rake +4 -0
- data/spec/test_app/bin/setup +38 -0
- data/spec/test_app/bin/update +29 -0
- data/spec/test_app/bin/yarn +11 -0
- data/spec/test_app/config.ru +5 -0
- data/spec/test_app/config/application.rb +26 -0
- data/spec/test_app/config/boot.rb +5 -0
- data/spec/test_app/config/cable.yml +10 -0
- data/spec/test_app/config/database.yml +25 -0
- data/spec/test_app/config/environment.rb +5 -0
- data/spec/test_app/config/environments/development.rb +54 -0
- data/spec/test_app/config/environments/production.rb +91 -0
- data/spec/test_app/config/environments/test.rb +42 -0
- data/spec/test_app/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/test_app/config/initializers/assets.rb +14 -0
- data/spec/test_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/test_app/config/initializers/cookies_serializer.rb +5 -0
- data/spec/test_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/test_app/config/initializers/inflections.rb +16 -0
- data/spec/test_app/config/initializers/mime_types.rb +4 -0
- data/spec/test_app/config/initializers/wrap_parameters.rb +14 -0
- data/spec/test_app/config/locales/en.yml +33 -0
- data/spec/test_app/config/puma.rb +56 -0
- data/spec/test_app/config/routes.rb +3 -0
- data/spec/test_app/config/secrets.yml +32 -0
- data/spec/test_app/config/spring.rb +6 -0
- data/spec/test_app/db/schema.rb +15 -0
- data/spec/test_app/package.json +5 -0
- data/spec/test_app/public/404.html +67 -0
- data/spec/test_app/public/422.html +67 -0
- data/spec/test_app/public/500.html +66 -0
- data/spec/test_app/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/test_app/public/apple-touch-icon.png +0 -0
- data/spec/test_app/public/favicon.ico +0 -0
- metadata +184 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7e91ed677d4f3fd9f576dd1e4330cbc8e5f8646d0a98bfde55054f345a0a9088
|
4
|
+
data.tar.gz: b45c4a7a8ff446845e70a3267fae455f564a15e751e662dc9abf5286755528ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad6e274f71662e663c9b7f7174e5a9165ed2776c897f455124443c302f02abd4db2f528fd34e341c922c6963b6713c4ce6cf9cd95f5d983f92b0a7e51a548ee5
|
7
|
+
data.tar.gz: 8a3cc162e5401baf43d001e98d990fcfd88c42aa67bee68d9a4d4c114424b384ffc28d3c776dc61fc59f49385be87304a920f1a7f4a856deb7f7006b572ea483
|
data/README.md
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# Rails Exporter
|
2
2
|
|
3
|
-
Rails Exporter (XML, XLS,
|
3
|
+
Rails Exporter (CSV, XML, XLS, XSLX)
|
4
4
|
|
5
5
|
## How to install
|
6
6
|
|
7
|
-
Add it to your
|
7
|
+
Add it to your `Gemfile`:
|
8
8
|
```ruby
|
9
9
|
gem 'rails-exporter'
|
10
10
|
```
|
11
11
|
|
12
12
|
Run the following command to install it:
|
13
|
-
```
|
13
|
+
```shell
|
14
14
|
$ bundle install
|
15
15
|
$ rails generate rails_exporter:install
|
16
16
|
```
|
@@ -19,7 +19,7 @@ $ rails generate rails_exporter:install
|
|
19
19
|
|
20
20
|
You can generate exporters `app/exporters/example_exporter.rb`
|
21
21
|
|
22
|
-
```
|
22
|
+
```bash
|
23
23
|
$ rails generate rails_exporter:exporter example
|
24
24
|
```
|
25
25
|
|
@@ -33,6 +33,7 @@ class ExampleExporter < RailsExporter::Base
|
|
33
33
|
column :email
|
34
34
|
column :price => :currency
|
35
35
|
column :is_admin => :boolean
|
36
|
+
column :any_time => :time
|
36
37
|
column :birthday => :date
|
37
38
|
column :created_at => :datetime
|
38
39
|
column(:account) {|record| record.account.name}
|
@@ -52,17 +53,37 @@ end
|
|
52
53
|
### How to use
|
53
54
|
|
54
55
|
You can call `export_to` from **Array** or **ActiveRecord::Relation** objects:
|
55
|
-
```
|
56
|
+
```ruby
|
56
57
|
records = MyModel.all
|
57
|
-
records.export_to(:csv) # or MyModelExporter.export_to_csv(records)
|
58
|
+
records.export_to(:csv) # or MyModelExporter.export_to_csv(records)
|
58
59
|
|
59
60
|
records = [MyModel.first, MyModel.last]
|
60
61
|
records.export_to(:xml) # or MyModelExporter.export_to_xml(records)
|
62
|
+
|
63
|
+
## With context :simple
|
64
|
+
# records.export_to(:csv, :simple)
|
65
|
+
# or MyModelExporter.export_to_csv(records, :simple)
|
66
|
+
|
67
|
+
## CSV Custom Params
|
68
|
+
# records.export_to(:csv, params: {col_sep: ','}))
|
61
69
|
```
|
62
70
|
|
71
|
+
### Avaliable Types
|
72
|
+
|
73
|
+
You can call `RailsExporter::Base.file_types` to list all supported file types:
|
74
|
+
```ruby
|
75
|
+
RailsExporter::Base.file_types.each do |type|
|
76
|
+
puts type
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
### I18n Configuration
|
81
|
+
|
82
|
+
Modify you `config/locales/exporters.en.yml`
|
83
|
+
|
63
84
|
### Controller Example
|
64
85
|
|
65
|
-
```
|
86
|
+
```ruby
|
66
87
|
class UsersController < ApplicationController
|
67
88
|
def index
|
68
89
|
@users = User.all
|
@@ -71,12 +92,16 @@ class UsersController < ApplicationController
|
|
71
92
|
format.csv { send_data @users.export_to(:csv) }
|
72
93
|
format.xml { send_data @users.export_to(:xml) }
|
73
94
|
format.xls { send_data @users.export_to(:xls) }
|
95
|
+
format.xlsx { send_data @users.export_to(:xlsx) }
|
74
96
|
end
|
75
97
|
end
|
76
98
|
end
|
77
99
|
```
|
78
100
|
|
101
|
+
### MimeTypes
|
102
|
+
|
79
103
|
Declare XLS as a new mimetype in `config/initializers/mime_types`:
|
80
|
-
```
|
81
|
-
Mime::Type.register "application/
|
104
|
+
```ruby
|
105
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
106
|
+
Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
|
82
107
|
```
|
data/Rakefile
CHANGED
@@ -4,20 +4,21 @@ rescue LoadError
|
|
4
4
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
5
|
end
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsExporter'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
16
|
|
17
|
-
APP_RAKEFILE = File.expand_path("../
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/test_app/Rakefile", __FILE__)
|
18
18
|
load 'rails/tasks/engine.rake'
|
19
19
|
load 'rails/tasks/statistics.rake'
|
20
|
-
Bundler::GemHelper.install_tasks
|
20
|
+
# Bundler::GemHelper.install_tasks
|
21
|
+
require 'bundler/gem_tasks'
|
21
22
|
|
22
23
|
# require 'rake/testtask'
|
23
24
|
# Rake::TestTask.new(:test) do |t|
|
@@ -1,8 +1,15 @@
|
|
1
1
|
en:
|
2
2
|
exporters:
|
3
3
|
spreadsheet_name: 'Spreadsheet'
|
4
|
+
time_format: "%I:%M %P"
|
5
|
+
date_format: "%m/%d/%Y"
|
6
|
+
datetime_format: "%m/%d/%Y, %I:%M %P"
|
4
7
|
example:
|
5
8
|
name: 'Name'
|
6
9
|
another_example:
|
7
|
-
|
8
|
-
|
10
|
+
default:
|
11
|
+
name: 'My Name'
|
12
|
+
age: 'My Age'
|
13
|
+
context_name:
|
14
|
+
name: 'Name'
|
15
|
+
age: 'Age'
|
data/lib/rails_exporter/base.rb
CHANGED
@@ -8,7 +8,7 @@ module RailsExporter
|
|
8
8
|
|
9
9
|
class << self
|
10
10
|
def file_types
|
11
|
-
[:csv, :xls, :xml]
|
11
|
+
[:csv, :xls, :xlsx, :xml]
|
12
12
|
end
|
13
13
|
|
14
14
|
# def method_missing(m, *args, &block)
|
@@ -17,11 +17,11 @@ module RailsExporter
|
|
17
17
|
# end
|
18
18
|
# end
|
19
19
|
|
20
|
-
def exporter(
|
21
|
-
(self.exporters ||= {})[
|
22
|
-
@
|
20
|
+
def exporter(context=:default, &block)
|
21
|
+
(self.exporters ||= {})[context] ||= []
|
22
|
+
@exporter_context = context
|
23
23
|
block.call if block_given?
|
24
|
-
self.exporters[
|
24
|
+
self.exporters[context]
|
25
25
|
end
|
26
26
|
|
27
27
|
def column(attr, &block)
|
@@ -33,8 +33,9 @@ module RailsExporter
|
|
33
33
|
type = :string
|
34
34
|
end
|
35
35
|
|
36
|
-
|
37
|
-
|
36
|
+
exporter_name_without_suffix = self.to_s.underscore.gsub('_exporter','')
|
37
|
+
label = I18n.t("#{@exporter_context}.#{attribute}", default: [attribute.to_sym, attribute.to_s.humanize], scope: [:exporters, exporter_name_without_suffix])
|
38
|
+
self.exporters[@exporter_context] << {column: attribute, label: label, type: normalize_type(type), block: (block_given? ? block : nil)}
|
38
39
|
end
|
39
40
|
|
40
41
|
def columns(exporter_name=:default)
|
@@ -1,5 +1,9 @@
|
|
1
1
|
require 'builder'
|
2
2
|
require 'spreadsheet'
|
3
|
+
require 'rubyXL'
|
4
|
+
require 'rubyXL/convenience_methods/font'
|
5
|
+
require 'rubyXL/convenience_methods/workbook'
|
6
|
+
require 'rubyXL/convenience_methods/worksheet'
|
3
7
|
|
4
8
|
module RailsExporter
|
5
9
|
module Exporter
|
@@ -9,8 +13,9 @@ module RailsExporter
|
|
9
13
|
end
|
10
14
|
|
11
15
|
module ClassMethods
|
12
|
-
def export_to_csv(records, context=:default)
|
13
|
-
|
16
|
+
def export_to_csv(records, context=:default, params: {})
|
17
|
+
custom_params = {col_sep: ';', force_quotes: true}.merge(params)
|
18
|
+
CSV.generate(custom_params) do |csv|
|
14
19
|
# HEADER
|
15
20
|
csv << get_columns(context).map do |attr|
|
16
21
|
attr_name(attr)
|
@@ -22,9 +27,10 @@ module RailsExporter
|
|
22
27
|
end
|
23
28
|
end
|
24
29
|
|
25
|
-
def export_to_xml(records, context=:default)
|
30
|
+
def export_to_xml(records, context=:default, params: nil)
|
31
|
+
#TODO: custom params
|
26
32
|
#File XML
|
27
|
-
xml = Builder::XmlMarkup.new(:
|
33
|
+
xml = Builder::XmlMarkup.new(indent: 2)
|
28
34
|
#Format
|
29
35
|
xml.instruct! :xml, :encoding => "UTF-8"
|
30
36
|
xml.records do
|
@@ -42,7 +48,8 @@ module RailsExporter
|
|
42
48
|
end
|
43
49
|
end
|
44
50
|
|
45
|
-
def export_to_xls(records, context=:default)
|
51
|
+
def export_to_xls(records, context=:default, params: nil)
|
52
|
+
#TODO: custom params
|
46
53
|
#FILE
|
47
54
|
file_contents = StringIO.new
|
48
55
|
#CHARSET
|
@@ -68,6 +75,28 @@ module RailsExporter
|
|
68
75
|
file_contents.string.force_encoding('binary')
|
69
76
|
end
|
70
77
|
|
78
|
+
def export_to_xlsx(records, context=:default, params: nil)
|
79
|
+
#TODO: custom params
|
80
|
+
#NEW document/spreadsheet
|
81
|
+
workbook = RubyXL::Workbook.new
|
82
|
+
worksheet = workbook[0]
|
83
|
+
# worksheet = workbook.add_worksheet(I18n.t(:spreadsheet_name, default: ['Spreadsheet'], scope: [:exporters]))
|
84
|
+
worksheet.sheet_name = I18n.t(:spreadsheet_name, default: ['Spreadsheet'], scope: [:exporters])
|
85
|
+
#HEADER FORMAT
|
86
|
+
worksheet.change_row_bold(0, true)
|
87
|
+
#HEADER (ROW=0)
|
88
|
+
get_columns(context).each_with_index do |attr, i|
|
89
|
+
worksheet.add_cell(0, i, attr_name(attr))
|
90
|
+
end
|
91
|
+
#ROWS
|
92
|
+
records.each_with_index do |record, row_index|
|
93
|
+
values = get_values(record, context)
|
94
|
+
values.each_with_index {|value, col_index| worksheet.add_cell(row_index+1, col_index, value)}
|
95
|
+
end
|
96
|
+
#RETURN STRING
|
97
|
+
workbook.stream.string.force_encoding('binary')
|
98
|
+
end
|
99
|
+
|
71
100
|
private
|
72
101
|
def get_columns(context)
|
73
102
|
self.send(:columns, context) || []
|
@@ -94,12 +123,17 @@ module RailsExporter
|
|
94
123
|
ActionController::Base.helpers.number_to_currency(value)
|
95
124
|
elsif type==:boolean
|
96
125
|
(value==true or value=='true' or value=='1') ? 'S' : 'N'
|
126
|
+
elsif type==:time
|
127
|
+
locale_format = I18n.t(:time_format, default: ['%H:%M:%S'], scope: [:exporters])
|
128
|
+
(I18n.l(value, format: locale_format) rescue value).to_s
|
97
129
|
elsif type==:date
|
98
|
-
|
130
|
+
locale_format = I18n.t(:date_format, default: ['%d/%m/%Y'], scope: [:exporters])
|
131
|
+
(I18n.l(value, format: locale_format) rescue value).to_s
|
99
132
|
elsif type==:datetime
|
100
|
-
|
133
|
+
locale_format = I18n.t(:datetime_format, default: ['%d/%m/%Y %H:%M:%S'], scope: [:exporters])
|
134
|
+
(I18n.l(value, format: locale_format) rescue value).to_s
|
101
135
|
else
|
102
|
-
(I18n.l(value) rescue value)
|
136
|
+
(I18n.l(value) rescue value).to_s
|
103
137
|
end
|
104
138
|
end
|
105
139
|
|
@@ -3,20 +3,20 @@ require 'rails_exporter/base'
|
|
3
3
|
module RailsExporter
|
4
4
|
module Helper
|
5
5
|
|
6
|
-
define_method "export_to" do |ext, context=:default|
|
7
|
-
exec_method(ext, context)
|
6
|
+
define_method "export_to" do |ext, context=:default, params: nil|
|
7
|
+
exec_method(ext, context, params: params)
|
8
8
|
end
|
9
9
|
|
10
10
|
private
|
11
11
|
def get_obj_class
|
12
12
|
(self.first.class rescue nil)
|
13
13
|
end
|
14
|
-
def exec_method(ext, context)
|
14
|
+
def exec_method(ext, context, params: nil)
|
15
15
|
klass = get_obj_class
|
16
16
|
exporter_klass = "#{klass.name}_exporter".classify.constantize
|
17
17
|
# RailsExporter::Base.file_types
|
18
18
|
if exporter_klass and exporter_klass.respond_to?("export_to_#{ext}")
|
19
|
-
exporter_klass.send("export_to_#{ext}", self, context)
|
19
|
+
exporter_klass.send("export_to_#{ext}", self, context, params: params)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
SimpleCov.start do
|
4
|
+
add_group 'Files', ['rails_exporter', 'lib/rails_exporter']
|
5
|
+
add_filter ['spec','rails-exporter']
|
6
|
+
end
|
7
|
+
|
8
|
+
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
9
|
+
require 'spec_helper'
|
10
|
+
ENV['RAILS_ENV'] ||= 'test'
|
11
|
+
require File.expand_path('../../spec/test_app/config/environment', __FILE__)
|
12
|
+
# Prevent database truncation if the environment is production
|
13
|
+
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
14
|
+
require 'rspec/rails'
|
15
|
+
# Add additional requires below this line. Rails is not loaded until this point!
|
16
|
+
|
17
|
+
# Requires supporting ruby files with custom matchers and macros, etc, in
|
18
|
+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
|
19
|
+
# run as spec files by default. This means that files in spec/support that end
|
20
|
+
# in _spec.rb will both be required and run as specs, causing the specs to be
|
21
|
+
# run twice. It is recommended that you do not name files matching this glob to
|
22
|
+
# end with _spec.rb. You can configure this pattern with the --pattern
|
23
|
+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
|
24
|
+
#
|
25
|
+
# The following line is provided for convenience purposes. It has the downside
|
26
|
+
# of increasing the boot-up time by auto-requiring all files in the support
|
27
|
+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
|
28
|
+
# require only the support files necessary.
|
29
|
+
#
|
30
|
+
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
|
31
|
+
|
32
|
+
# Checks for pending migration and applies them before tests are run.
|
33
|
+
# If you are not using ActiveRecord, you can remove this line.
|
34
|
+
ActiveRecord::Migration.maintain_test_schema!
|
35
|
+
ActiveJob::Base.queue_adapter = :test
|
36
|
+
|
37
|
+
RSpec.configure do |config|
|
38
|
+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
39
|
+
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
40
|
+
|
41
|
+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
42
|
+
# examples within a transaction, remove the following line or assign false
|
43
|
+
# instead of true.
|
44
|
+
config.use_transactional_fixtures = true
|
45
|
+
|
46
|
+
# RSpec Rails can automatically mix in different behaviours to your tests
|
47
|
+
# based on their file location, for example enabling you to call `get` and
|
48
|
+
# `post` in specs under `spec/controllers`.
|
49
|
+
#
|
50
|
+
# You can disable this behaviour by removing the line below, and instead
|
51
|
+
# explicitly tag your specs with their type, e.g.:
|
52
|
+
#
|
53
|
+
# RSpec.describe UsersController, :type => :controller do
|
54
|
+
# # ...
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# The different available types are documented in the features, such as in
|
58
|
+
# https://relishapp.com/rspec/rspec-rails/docs
|
59
|
+
config.infer_spec_type_from_file_location!
|
60
|
+
|
61
|
+
# Filter lines from Rails gems in backtraces.
|
62
|
+
config.filter_rails_from_backtrace!
|
63
|
+
# arbitrary gems may also be filtered via:
|
64
|
+
# config.filter_gems_from_backtrace("gem name")
|
65
|
+
|
66
|
+
end
|