rails-exporter 0.0.2
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/MIT-LICENSE +20 -0
- data/README.md +82 -0
- data/Rakefile +29 -0
- data/config/initializers/rails_exporter.rb +6 -0
- data/config/locales/exporters.en.yml +8 -0
- data/lib/generators/rails_exporter/exporter_generator.rb +12 -0
- data/lib/generators/rails_exporter/install_generator.rb +16 -0
- data/lib/generators/templates/generic_exporter.erb +21 -0
- data/lib/rails-exporter.rb +1 -0
- data/lib/rails_exporter/base.rb +58 -0
- data/lib/rails_exporter/engine.rb +4 -0
- data/lib/rails_exporter/exporter.rb +112 -0
- data/lib/rails_exporter/helper.rb +36 -0
- data/lib/rails_exporter/version.rb +3 -0
- data/lib/rails_exporter.rb +7 -0
- data/lib/tasks/rails_exporter_tasks.rake +4 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 48735310c85efae63e71d3e708ae1ca823fca999
|
4
|
+
data.tar.gz: 4e368937e4d37ff016e9c7b51e0126d1000eb3b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af019d22486e8ef81d42f0a63736cd0d8e3df0a49ce30ef5b591a3b9e9e0ba24106d72aaadcfc7b9d2051e7a358c1112a79bf1dac720fe9869f6323b78a29329
|
7
|
+
data.tar.gz: 4df6e08bed7e5a1d873e6be0409a8451dacb7ef437a9780b2e4fc7250d6b093703f08f4307a7bef1bed62e15a2be60201d712881d0422d28278943c9abba269d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Bruno Porto
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Rails Exporter
|
2
|
+
|
3
|
+
Rails Exporter
|
4
|
+
|
5
|
+
## How to install
|
6
|
+
|
7
|
+
Add it to your **Gemfile**:
|
8
|
+
```ruby
|
9
|
+
gem 'rails-exporter'
|
10
|
+
```
|
11
|
+
|
12
|
+
Run the following command to install it:
|
13
|
+
```sh
|
14
|
+
$ bundle install
|
15
|
+
$ rails generate rails_exporter:install
|
16
|
+
```
|
17
|
+
|
18
|
+
## Generators
|
19
|
+
|
20
|
+
You can generate exporters `app/exporters/example_exporter.rb`
|
21
|
+
|
22
|
+
```sh
|
23
|
+
$ rails generate rails_exporter:exporter example
|
24
|
+
```
|
25
|
+
|
26
|
+
Generator will make a file with content like:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class ExampleExporter < RailsExporter::Base
|
30
|
+
|
31
|
+
exporter do
|
32
|
+
column :name
|
33
|
+
column :email
|
34
|
+
column :price => :currency
|
35
|
+
column :is_admin => :boolean
|
36
|
+
column :birthday => :date
|
37
|
+
column :created_at => :datetime
|
38
|
+
column(:account) {|record| record.account.name}
|
39
|
+
column(:updated_at) do |record|
|
40
|
+
record.updated_at.strftime("%d/%m/%Y %H:%M:%S")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# exporter :simple do
|
45
|
+
# column :nome
|
46
|
+
# column :email
|
47
|
+
# end
|
48
|
+
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
### How to use
|
53
|
+
|
54
|
+
You can call `export_to` from **Array** or **ActiveRecord::Relation** objects:
|
55
|
+
```erb
|
56
|
+
records = MyModel.all
|
57
|
+
records.export_to(:csv) # or MyModelExporter.export_to_csv(records)
|
58
|
+
|
59
|
+
records = [MyModel.first, MyModel.last]
|
60
|
+
records.export_to(:xml) # or MyModelExporter.export_to_xml(records)
|
61
|
+
```
|
62
|
+
|
63
|
+
### Controller Example
|
64
|
+
|
65
|
+
```erb
|
66
|
+
class UsersController < ApplicationController
|
67
|
+
def index
|
68
|
+
@users = User.all
|
69
|
+
respond_to do |format|
|
70
|
+
format.html
|
71
|
+
format.csv { send_data @users.export_to(:csv) }
|
72
|
+
format.xml { send_data @users.export_to(:xml) }
|
73
|
+
format.xls { send_data @users.export_to(:xls) }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
For `format.xls` maybe you need to declare XLS mimetype in `config/initializers/mime_types` :
|
80
|
+
```erb
|
81
|
+
Mime::Type.register "application/xls", :xls
|
82
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
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 = 'RailsExporter'
|
12
|
+
# rdoc.options << '--line-numbers'
|
13
|
+
# rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
# end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
load 'rails/tasks/statistics.rake'
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
# require 'rake/testtask'
|
23
|
+
# Rake::TestTask.new(:test) do |t|
|
24
|
+
# t.libs << 'lib'
|
25
|
+
# t.libs << 'test'
|
26
|
+
# t.pattern = 'test/**/*_test.rb'
|
27
|
+
# t.verbose = false
|
28
|
+
# end
|
29
|
+
# task default: :test
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module RailsExporter
|
4
|
+
class ExporterGenerator < Rails::Generators::NamedBase
|
5
|
+
source_root File.expand_path("../../templates", __FILE__)
|
6
|
+
|
7
|
+
def generate_exporter
|
8
|
+
@exporter_name = file_name.classify
|
9
|
+
template "generic_exporter.erb", File.join('app/exporters', "#{file_name.underscore}_exporter.rb")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module RailsExporter
|
4
|
+
module Generators
|
5
|
+
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
source_root File.expand_path("../../../../config", __FILE__)
|
8
|
+
|
9
|
+
def copy_locales
|
10
|
+
directory 'locales', 'config/locales'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class <%= @exporter_name %>Exporter < RailsExporter::Base
|
2
|
+
|
3
|
+
exporter do
|
4
|
+
column :name
|
5
|
+
column :email
|
6
|
+
column :price => :currency
|
7
|
+
column :is_admin => :boolean
|
8
|
+
column :birthday => :date
|
9
|
+
column :created_at => :datetime
|
10
|
+
column(:account) {|record| record.account.name}
|
11
|
+
column(:updated_at) do |record|
|
12
|
+
record.updated_at.strftime("%d/%m/%Y %H:%M:%S")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# exporter :simple do
|
17
|
+
# column :nome
|
18
|
+
# column :email
|
19
|
+
# end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails_exporter'
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rails_exporter/exporter'
|
2
|
+
|
3
|
+
module RailsExporter
|
4
|
+
class Base
|
5
|
+
include RailsExporter::Exporter
|
6
|
+
|
7
|
+
class_attribute :exporters
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def file_types
|
11
|
+
[:csv, :xls, :xml]
|
12
|
+
end
|
13
|
+
|
14
|
+
# def method_missing(m, *args, &block)
|
15
|
+
# if m =~ /_url|_path/
|
16
|
+
# Rails.application.routes.url_helpers.send(m, args)
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
|
20
|
+
def exporter(name=:default, &block)
|
21
|
+
(self.exporters ||= {})[name] ||= []
|
22
|
+
@exporter_name = name
|
23
|
+
block.call if block_given?
|
24
|
+
self.exporters[name]
|
25
|
+
end
|
26
|
+
|
27
|
+
def column(attr, &block)
|
28
|
+
if attr.is_a?(Hash)
|
29
|
+
attribute = attr.keys.first.to_s.to_sym
|
30
|
+
type = attr.values.first.to_s.to_sym
|
31
|
+
else
|
32
|
+
attribute = attr.to_sym
|
33
|
+
type = :string
|
34
|
+
end
|
35
|
+
|
36
|
+
label = I18n.t(attribute, default: [attribute.to_s.humanize], scope: [:exporters, @exporter_name])
|
37
|
+
self.exporters[@exporter_name] << {column: attribute, label: label, type: normalize_type(type), block: block}
|
38
|
+
end
|
39
|
+
|
40
|
+
def columns(exporter_name=:default)
|
41
|
+
self.exporters[exporter_name].map do |attribute|
|
42
|
+
attribute.slice(:column, :label, :type)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def normalize_type(type)
|
48
|
+
if [:currency, :boolean, :date, :datetime, :string].include?(type.to_s.to_sym)
|
49
|
+
type.to_s.to_sym
|
50
|
+
else
|
51
|
+
:string
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'builder'
|
2
|
+
|
3
|
+
module RailsExporter
|
4
|
+
module Exporter
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def export_to_csv(records, context=:default)
|
12
|
+
#Gerando CSV com separador ; e usando " nos campos por padrão.
|
13
|
+
CSV.generate({:col_sep => ';', :force_quotes => true}) do |csv|
|
14
|
+
#Cabeçalho
|
15
|
+
csv << get_columns(context).map do |attr|
|
16
|
+
attr_name(attr)
|
17
|
+
end
|
18
|
+
#DADOS
|
19
|
+
records.each do |record|
|
20
|
+
csv << get_values(record, context)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def export_to_xml(records, context=:default)
|
26
|
+
#Gerando arquivo XML
|
27
|
+
xml = Builder::XmlMarkup.new(:indent => 2)
|
28
|
+
#Formando tipo de saida de dados
|
29
|
+
xml.instruct! :xml, :encoding => "UTF-8"
|
30
|
+
xml.records do
|
31
|
+
#Gerando cada linha de record
|
32
|
+
records.each do |record|
|
33
|
+
get_values = get_values(record, context)
|
34
|
+
xml.record do |r|
|
35
|
+
i = 0
|
36
|
+
get_columns(context).map do |attr|
|
37
|
+
attr = attr.keys.first if attr.is_a?(Hash)
|
38
|
+
xml.tag!(attr, get_values[i], {title: attr_name(attr)})
|
39
|
+
i+=1
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def export_to_xls(records, context=:default)
|
47
|
+
#FILE
|
48
|
+
file_contents = StringIO.new
|
49
|
+
#CHARSET
|
50
|
+
Spreadsheet.client_encoding = 'UTF-8'
|
51
|
+
#NEW document/spreadsheet
|
52
|
+
document = Spreadsheet::Workbook.new
|
53
|
+
spreadsheet = document.create_worksheet
|
54
|
+
spreadsheet.name = I18n.t(:spreadsheet_name, default: ['Spreadsheet'], scope: [:exporters])
|
55
|
+
#HEADER FORMAT
|
56
|
+
spreadsheet.row(0).default_format = Spreadsheet::Format.new :weight => :bold
|
57
|
+
#HEADER
|
58
|
+
get_columns(context).each_with_index do |attr, i|
|
59
|
+
attr = attr.keys.first if attr.is_a?(Hash)
|
60
|
+
spreadsheet.row(0).insert i, (self.human_attribute_name(attr) || attr)
|
61
|
+
end
|
62
|
+
#ROWS
|
63
|
+
records.each_with_index do |record, i|
|
64
|
+
values = get_values(record, context)
|
65
|
+
spreadsheet.row(i+1).push(*values)
|
66
|
+
end
|
67
|
+
#SAVE spreadsheet
|
68
|
+
document.write file_contents
|
69
|
+
#RETURN STRING
|
70
|
+
file_contents.string.force_encoding('binary')
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
def get_columns(context)
|
75
|
+
self.send(:columns, context) || []
|
76
|
+
end
|
77
|
+
|
78
|
+
def attr_name(attr)
|
79
|
+
attr[:label] || attr[:column]
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_values(record, context)
|
83
|
+
get_columns(context).map do |attribute|
|
84
|
+
if attribute[:block].is_a?(Proc)
|
85
|
+
value = attribute[:block].call(record)
|
86
|
+
value.to_s
|
87
|
+
else
|
88
|
+
value = (record.send(attribute[:column]) rescue '')
|
89
|
+
normalize_value(value, attribute[:type])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def normalize_value(value, type=nil)
|
95
|
+
type = type.present? ? type.to_sym : :unknown
|
96
|
+
if type==:currency
|
97
|
+
ActionController::Base.helpers.number_to_currency(value)
|
98
|
+
elsif type==:boolean
|
99
|
+
(value==true or value=='true' or value=='1') ? 'S' : 'N'
|
100
|
+
elsif type==:date
|
101
|
+
(I18n.l(value, format: '%d/%m/%Y') rescue value).to_s
|
102
|
+
elsif type==:datetime
|
103
|
+
(I18n.l(value, format: '%d/%m/%Y %H:%i:%s') rescue value).to_s
|
104
|
+
else
|
105
|
+
(I18n.l(value) rescue value)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rails_exporter/base'
|
2
|
+
|
3
|
+
module RailsExporter
|
4
|
+
module Helper
|
5
|
+
|
6
|
+
define_method "export_to" do |ext, context=:default|
|
7
|
+
exec_method(ext, context)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
def get_obj_class
|
12
|
+
(self.first.class rescue nil)
|
13
|
+
end
|
14
|
+
def exec_method(ext, context)
|
15
|
+
klass = get_obj_class
|
16
|
+
exporter_klass = "#{klass.name}_exporter".classify.constantize
|
17
|
+
# RailsExporter::Base.file_types
|
18
|
+
if exporter_klass and exporter_klass.respond_to?("export_to_#{ext}")
|
19
|
+
exporter_klass.send("export_to_#{ext}", self, context)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
#INJETANDO METODO para_* em um conjunto de array
|
27
|
+
class Array
|
28
|
+
include RailsExporter::Helper
|
29
|
+
end
|
30
|
+
|
31
|
+
#INJETANDO METODO para_* em um conjunto de registros ActiveRecord::Relation
|
32
|
+
module ActiveRecord
|
33
|
+
class Relation
|
34
|
+
include RailsExporter::Helper
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-exporter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bruno Porto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: spreadsheet
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Rails Exporter
|
28
|
+
email:
|
29
|
+
- brunotporto@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- config/initializers/rails_exporter.rb
|
38
|
+
- config/locales/exporters.en.yml
|
39
|
+
- lib/generators/rails_exporter/exporter_generator.rb
|
40
|
+
- lib/generators/rails_exporter/install_generator.rb
|
41
|
+
- lib/generators/templates/generic_exporter.erb
|
42
|
+
- lib/rails-exporter.rb
|
43
|
+
- lib/rails_exporter.rb
|
44
|
+
- lib/rails_exporter/base.rb
|
45
|
+
- lib/rails_exporter/engine.rb
|
46
|
+
- lib/rails_exporter/exporter.rb
|
47
|
+
- lib/rails_exporter/helper.rb
|
48
|
+
- lib/rails_exporter/version.rb
|
49
|
+
- lib/tasks/rails_exporter_tasks.rake
|
50
|
+
homepage: https://github.com/brunoporto/rails-exporter
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata: {}
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 2.5.1
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Rails Exporter
|
74
|
+
test_files: []
|