activekit 0.5.0.dev6 → 0.5.0.dev7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_kit/export/exportable.rb +25 -0
- data/lib/active_kit/export/exporter.rb +19 -43
- data/lib/active_kit/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: 2e3830a0b0d888ffcd7a7f921b282fbf27dc0f2bdde5322772a71ed04755add3
|
4
|
+
data.tar.gz: abb0bbead8a270129c04dfb0998e15999df84a6daeb7c28a3237dac161211f43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2f82ef282ec1226ac39d8ac42993f5ed621ea89407c22ebd6a0850d40157f32e20be968a946899c833bb041b2fe258914c64247c7746a968709f17da88db6af
|
7
|
+
data.tar.gz: baea0b85ab9b4adbd8e3c1f1eb1e37d62c673b83fc4858233a96cee9b7721a37f7985479a66a0848b16db33137dda1a48553a5d275da5ab0241aa85eee84ebd3
|
@@ -20,6 +20,31 @@ module ActiveKit
|
|
20
20
|
def export_attribute(name, **options)
|
21
21
|
exporter.create_attribute(name, options)
|
22
22
|
end
|
23
|
+
|
24
|
+
def export_describer_method(describer)
|
25
|
+
case describer.kind
|
26
|
+
when :csv
|
27
|
+
# The 'all' relation must be captured outside the Enumerator,
|
28
|
+
# else it will get reset to all the records of the class.
|
29
|
+
all_activerecord_relation = all.includes(describer.includes)
|
30
|
+
|
31
|
+
Enumerator.new do |yielder|
|
32
|
+
ActiveRecord::Base.connected_to(role: :writing, shard: describer.database.call) do
|
33
|
+
exporting = exporter.new_exporting(describer: describer)
|
34
|
+
|
35
|
+
# Add the headings.
|
36
|
+
yielder << CSV.generate_line(exporting.headings) if exporting.headings?
|
37
|
+
|
38
|
+
# Add the values.
|
39
|
+
# find_each will ignore any order if set earlier.
|
40
|
+
all_activerecord_relation.find_each do |record|
|
41
|
+
lines = exporting.lines_for(record: record)
|
42
|
+
lines.each { |line| yielder << CSV.generate_line(line) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
23
48
|
end
|
24
49
|
end
|
25
50
|
end
|
@@ -13,10 +13,10 @@ module ActiveKit
|
|
13
13
|
unless find_describer_by(describer_name: name)
|
14
14
|
options.store(:attributes, {})
|
15
15
|
@describers.store(name, options)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
@current_class.class_eval do
|
17
|
+
define_singleton_method name do
|
18
|
+
if describer = exporter.find_describer_by(describer_name: name)
|
19
|
+
export_describer_method(describer)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -38,51 +38,27 @@ module ActiveKit
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
def
|
42
|
-
|
43
|
-
|
44
|
-
# The 'all' relation must be captured outside the Enumerator,
|
45
|
-
# else it will get reset to all the records of the class.
|
46
|
-
all_activerecord_relation = all.includes(describer.includes)
|
47
|
-
|
48
|
-
Enumerator.new do |yielder|
|
49
|
-
ActiveRecord::Base.connected_to(role: :writing, shard: describer.database.call) do
|
50
|
-
exporting = Exporting.new(describer: describer)
|
51
|
-
|
52
|
-
# Add the headings.
|
53
|
-
yielder << CSV.generate_line(exporting.headings) if exporting.headings?
|
54
|
-
|
55
|
-
# Add the values.
|
56
|
-
# find_each will ignore any order if set earlier.
|
57
|
-
all_activerecord_relation.find_each do |record|
|
58
|
-
lines = exporting.lines_for(record: record)
|
59
|
-
lines.each { |line| yielder << CSV.generate_line(line) }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
private
|
67
|
-
|
68
|
-
def find_describer_by(describer_name:)
|
69
|
-
describer_options = @describers.dig(describer_name)
|
70
|
-
return nil unless describer_options.present?
|
41
|
+
def find_describer_by(name:)
|
42
|
+
options = @describers.dig(name)
|
43
|
+
return nil unless options.present?
|
71
44
|
|
72
|
-
describer_attributes = describer_options[:attributes]
|
73
|
-
includes = describer_attributes.values.map { |options| options.dig(:includes) }.compact.flatten(1).uniq
|
74
|
-
fields = build_describer_fields(describer_attributes)
|
75
45
|
hash = {
|
76
|
-
name:
|
77
|
-
kind:
|
78
|
-
database:
|
79
|
-
attributes:
|
80
|
-
includes: includes,
|
81
|
-
fields:
|
46
|
+
name: name,
|
47
|
+
kind: options[:kind],
|
48
|
+
database: options[:database],
|
49
|
+
attributes: options[:attributes],
|
50
|
+
includes: options[:attributes].values.map { |options| options.dig(:includes) }.compact.flatten(1).uniq,
|
51
|
+
fields: build_describer_fields(options[:attributes])
|
82
52
|
}
|
83
53
|
OpenStruct.new(hash)
|
84
54
|
end
|
85
55
|
|
56
|
+
def new_exporting(describer:)
|
57
|
+
Exporting.new(describer: describer)
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
86
62
|
def build_describer_fields(describer_attributes)
|
87
63
|
describer_attributes.inject({}) do |fields_hash, (name, options)|
|
88
64
|
enclosed_attributes = Array(options.dig(:attributes))
|
data/lib/active_kit/version.rb
CHANGED