magic-report 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b11733b1128011acfba29bf24cbf2846ce42e226aeccadfcdfc6ce713c2b1b3e
4
- data.tar.gz: 0dbec29ea7c05acf3744fc1a62d4b200455a88926d1e938826243df683d9ce93
3
+ metadata.gz: f213aa09ce963abf162471ccdae3a9aecbbbfce8d6aed1dfabf673eb88bebedf
4
+ data.tar.gz: cf763202fc99278741b64898a6809929d7dfac3ea08b7c25f48870e11e4729a1
5
5
  SHA512:
6
- metadata.gz: ad63138a1fe01308e5ddfef0f50d3feb04ae5dd49e9fe8c2e81dcac6ed5f376daf32de12baf1912695c798ade584d96652d7c1c0a0fc6f801992fd41c0a09bce
7
- data.tar.gz: 2fff4d4f90864fded9a18846e718fbc82671907f106ce16c5240df94d57d2e92c27c2f7e423d02da1ee88a93b4f49a21db201636ffc04bb1b9c020c04a74d562
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-operation"
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
- headings:
43
- user:
44
- id: ID
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
- headings:
84
- user:
85
- id: ID
86
- is_admin: Admin?
87
- address: User address
88
- cars: Car
89
- shipping_address: Shipping address
90
- billing_address: Billing address
91
- car:
92
- name: Name
93
- address:
94
- address_line_1: Line 1
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
@@ -4,7 +4,7 @@ module MagicReport
4
4
  class Report
5
5
  module ClassHelpers
6
6
  def name_from_class
7
- ::MagicReport::Utils.underscore(self.class.name)
7
+ ::MagicReport::Utils.underscore(self.class.name).tr("/", ".")
8
8
  end
9
9
 
10
10
  def fields_from_class
@@ -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
@@ -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)
@@ -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.headings.#{name}.#{key}")
31
+ I18n.translate!("magic_report.#{name}.#{key}")
29
32
  end
30
33
 
31
34
  module_function :underscore
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MagicReport
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic-report
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Pankratev