magic-report 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -18
- data/lib/magic_report/report/class_helpers.rb +1 -1
- data/lib/magic_report/report/csv.rb +8 -2
- data/lib/magic_report/report.rb +13 -2
- data/lib/magic_report/utils.rb +4 -1
- data/lib/magic_report/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: f213aa09ce963abf162471ccdae3a9aecbbbfce8d6aed1dfabf673eb88bebedf
|
4
|
+
data.tar.gz: cf763202fc99278741b64898a6809929d7dfac3ea08b7c25f48870e11e4729a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aea3c759c88f5381ce9df62ca88ef8242d7307a16d06f1340578f2856896f087f4dfc5d31e7aed08490910301b89450fb704d3a0cdc05802c7fb16109d5047bd
|
7
|
+
data.tar.gz: 0dac70e8c03ec49759b1f1f462b787c304d8a33acdc1885213cc52a0e9d2f646592caf4c69bd692ae371f88bc7f4e54301bcdc256b05aed5abf927ed28f6e557
|
data/README.md
CHANGED
@@ -9,7 +9,7 @@ An easy way to export data to CSV
|
|
9
9
|
Add this line to your application’s Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem "magic-
|
12
|
+
gem "magic-report", require: "magic_report"
|
13
13
|
```
|
14
14
|
|
15
15
|
## Getting Started
|
@@ -39,10 +39,9 @@ Also, for each report you must provide locales file:
|
|
39
39
|
```yaml
|
40
40
|
en:
|
41
41
|
magic_report:
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
is_admin: Admin?
|
42
|
+
user:
|
43
|
+
id: ID
|
44
|
+
is_admin: Admin?
|
46
45
|
```
|
47
46
|
|
48
47
|
CSV will be
|
@@ -80,19 +79,18 @@ Because we have explicitly said that the user `has_many :cars`, the number of li
|
|
80
79
|
```yaml
|
81
80
|
en:
|
82
81
|
magic_report:
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
city: City
|
82
|
+
user:
|
83
|
+
id: ID
|
84
|
+
is_admin: Admin?
|
85
|
+
address: User address
|
86
|
+
cars: Car
|
87
|
+
shipping_address: Shipping address
|
88
|
+
billing_address: Billing address
|
89
|
+
car:
|
90
|
+
name: Name
|
91
|
+
address:
|
92
|
+
address_line_1: Line 1
|
93
|
+
city: City
|
96
94
|
```
|
97
95
|
|
98
96
|
CSV will be
|
@@ -19,14 +19,20 @@ module MagicReport
|
|
19
19
|
report.result.each do |row|
|
20
20
|
row.to_h.each { |nested_row| csv << nested_row.values }
|
21
21
|
end
|
22
|
-
# ensure
|
23
|
-
# file.close
|
24
22
|
end
|
25
23
|
|
24
|
+
# Don't forget to unlink in production code
|
26
25
|
def unlink
|
26
|
+
file.close
|
27
27
|
file.unlink
|
28
28
|
end
|
29
29
|
|
30
|
+
def io
|
31
|
+
io = csv.to_io
|
32
|
+
io.rewind
|
33
|
+
io
|
34
|
+
end
|
35
|
+
|
30
36
|
private
|
31
37
|
|
32
38
|
def write_headers
|
data/lib/magic_report/report.rb
CHANGED
@@ -29,6 +29,13 @@ module MagicReport
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
def as_attachment
|
33
|
+
{
|
34
|
+
mime_type: "text/csv",
|
35
|
+
content: as_csv.io
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
32
39
|
def headings
|
33
40
|
@headings ||= (fields.map { |field| t(field.key) } + has_one.map { |association| association.report.headings } + has_many.map { |association| association.report.headings }).flatten
|
34
41
|
end
|
@@ -60,8 +67,10 @@ module MagicReport
|
|
60
67
|
|
61
68
|
klass = ::MagicReport::Utils.derive_class(opts, &block)
|
62
69
|
|
70
|
+
opts[:name] = ::MagicReport::Utils.underscore(opts[:name].to_s) if opts[:name]
|
71
|
+
|
63
72
|
if (prefix = opts[:prefix])
|
64
|
-
opts[:prefix] = new.instance_exec(&prefix)
|
73
|
+
opts[:prefix] = new(name: ::MagicReport::Utils.underscore(name.to_s)).instance_exec(&prefix)
|
65
74
|
end
|
66
75
|
|
67
76
|
@has_one << Configuration::HasOne.new(klass: klass, opts: opts, key: coerced_attribute)
|
@@ -74,8 +83,10 @@ module MagicReport
|
|
74
83
|
|
75
84
|
klass = ::MagicReport::Utils.derive_class(opts, &block)
|
76
85
|
|
86
|
+
opts[:name] = ::MagicReport::Utils.underscore(opts[:name].to_s) if opts[:name]
|
87
|
+
|
77
88
|
if (prefix = opts[:prefix])
|
78
|
-
opts[:prefix] = new.instance_exec(&prefix)
|
89
|
+
opts[:prefix] = new(name: ::MagicReport::Utils.underscore(name.to_s)).instance_exec(&prefix)
|
79
90
|
end
|
80
91
|
|
81
92
|
@has_many << Configuration::HasMany.new(klass: klass, opts: opts, key: coerced_attribute)
|
data/lib/magic_report/utils.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
module MagicReport
|
4
4
|
module Utils
|
5
5
|
def underscore(klass)
|
6
|
+
return "ded" unless klass
|
7
|
+
|
6
8
|
klass.gsub(/::/, "/")
|
7
9
|
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
8
10
|
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
@@ -15,6 +17,7 @@ module MagicReport
|
|
15
17
|
raise "name option must be provided" unless opts[:name]
|
16
18
|
|
17
19
|
cloned_klass = ::MagicReport::Report.clone
|
20
|
+
cloned_klass.define_singleton_method(:name) { opts[:name] }
|
18
21
|
cloned_klass.class_eval(&block)
|
19
22
|
cloned_klass
|
20
23
|
else
|
@@ -25,7 +28,7 @@ module MagicReport
|
|
25
28
|
# @param name is the report name
|
26
29
|
# @key is a field
|
27
30
|
def t(name:, key:)
|
28
|
-
I18n.translate!("magic_report
|
31
|
+
I18n.translate!("magic_report.#{name}.#{key}")
|
29
32
|
end
|
30
33
|
|
31
34
|
module_function :underscore
|
data/lib/magic_report/version.rb
CHANGED