rails-exporter 0.0.13 → 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 +4 -4
- data/README.md +3 -0
- data/lib/rails_exporter/exporter.rb +10 -6
- data/lib/rails_exporter/helper.rb +4 -4
- data/lib/rails_exporter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
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
@@ -63,6 +63,9 @@ You can call `export_to` from **Array** or **ActiveRecord::Relation** objects:
|
|
63
63
|
## With context :simple
|
64
64
|
# records.export_to(:csv, :simple)
|
65
65
|
# or MyModelExporter.export_to_csv(records, :simple)
|
66
|
+
|
67
|
+
## CSV Custom Params
|
68
|
+
# records.export_to(:csv, params: {col_sep: ','}))
|
66
69
|
```
|
67
70
|
|
68
71
|
### Avaliable Types
|
@@ -13,8 +13,9 @@ module RailsExporter
|
|
13
13
|
end
|
14
14
|
|
15
15
|
module ClassMethods
|
16
|
-
def export_to_csv(records, context=:default)
|
17
|
-
|
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|
|
18
19
|
# HEADER
|
19
20
|
csv << get_columns(context).map do |attr|
|
20
21
|
attr_name(attr)
|
@@ -26,9 +27,10 @@ module RailsExporter
|
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
|
-
def export_to_xml(records, context=:default)
|
30
|
+
def export_to_xml(records, context=:default, params: nil)
|
31
|
+
#TODO: custom params
|
30
32
|
#File XML
|
31
|
-
xml = Builder::XmlMarkup.new(:
|
33
|
+
xml = Builder::XmlMarkup.new(indent: 2)
|
32
34
|
#Format
|
33
35
|
xml.instruct! :xml, :encoding => "UTF-8"
|
34
36
|
xml.records do
|
@@ -46,7 +48,8 @@ module RailsExporter
|
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
49
|
-
def export_to_xls(records, context=:default)
|
51
|
+
def export_to_xls(records, context=:default, params: nil)
|
52
|
+
#TODO: custom params
|
50
53
|
#FILE
|
51
54
|
file_contents = StringIO.new
|
52
55
|
#CHARSET
|
@@ -72,7 +75,8 @@ module RailsExporter
|
|
72
75
|
file_contents.string.force_encoding('binary')
|
73
76
|
end
|
74
77
|
|
75
|
-
def export_to_xlsx(records, context=:default)
|
78
|
+
def export_to_xlsx(records, context=:default, params: nil)
|
79
|
+
#TODO: custom params
|
76
80
|
#NEW document/spreadsheet
|
77
81
|
workbook = RubyXL::Workbook.new
|
78
82
|
worksheet = workbook[0]
|
@@ -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
|