activekit 0.5.0.dev5 → 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 +27 -2
- data/lib/active_kit/export/exporter.rb +24 -51
- 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
|
@@ -14,11 +14,36 @@ module ActiveKit
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def export_describer(name, **options)
|
17
|
-
exporter.
|
17
|
+
exporter.create_describer(name, options)
|
18
18
|
end
|
19
19
|
|
20
20
|
def export_attribute(name, **options)
|
21
|
-
exporter.
|
21
|
+
exporter.create_attribute(name, options)
|
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
|
22
47
|
end
|
23
48
|
end
|
24
49
|
end
|
@@ -6,23 +6,28 @@ module ActiveKit
|
|
6
6
|
@describers = {}
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
9
|
+
def create_describer(name, options)
|
10
10
|
name = name.to_sym
|
11
11
|
options.deep_symbolize_keys!
|
12
12
|
|
13
13
|
unless find_describer_by(describer_name: name)
|
14
14
|
options.store(:attributes, {})
|
15
15
|
@describers.store(name, options)
|
16
|
-
|
17
|
-
|
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
|
+
end
|
21
|
+
end
|
22
|
+
end
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
21
|
-
def
|
22
|
-
create_export_describer(:to_csv, kind: :csv, database: -> { ActiveRecord::Base.connection_db_config.database.to_sym }) unless @describers.present?
|
23
|
-
|
26
|
+
def create_attribute(name, options)
|
24
27
|
options.deep_symbolize_keys!
|
25
28
|
|
29
|
+
create_describer(:to_csv, kind: :csv, database: -> { ActiveRecord::Base.connection_db_config.database.to_sym }) unless @describers.present?
|
30
|
+
|
26
31
|
describer_names = Array(options.delete(:describers))
|
27
32
|
describer_names = @describers.keys if describer_names.blank?
|
28
33
|
|
@@ -33,58 +38,26 @@ module ActiveKit
|
|
33
38
|
end
|
34
39
|
end
|
35
40
|
|
36
|
-
|
41
|
+
def find_describer_by(name:)
|
42
|
+
options = @describers.dig(name)
|
43
|
+
return nil unless options.present?
|
37
44
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
Enumerator.new do |yielder|
|
48
|
-
ActiveRecord::Base.connected_to(role: :writing, shard: describer.database.call) do
|
49
|
-
exporting = exporter.new_exporting(describer: describer)
|
50
|
-
|
51
|
-
# Add the headings.
|
52
|
-
yielder << CSV.generate_line(exporting.headings) if exporting.headings?
|
53
|
-
|
54
|
-
# Add the values.
|
55
|
-
# find_each will ignore any order if set earlier.
|
56
|
-
all_activerecord_relation.find_each do |record|
|
57
|
-
lines = exporting.lines_for(record: record)
|
58
|
-
lines.each { |line| yielder << CSV.generate_line(line) }
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
45
|
+
hash = {
|
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])
|
52
|
+
}
|
53
|
+
OpenStruct.new(hash)
|
65
54
|
end
|
66
55
|
|
67
56
|
def new_exporting(describer:)
|
68
57
|
Exporting.new(describer: describer)
|
69
58
|
end
|
70
59
|
|
71
|
-
|
72
|
-
describer_options = @describers.dig(describer_name)
|
73
|
-
return nil unless describer_options.present?
|
74
|
-
|
75
|
-
describer_attributes = describer_options[:attributes]
|
76
|
-
includes = describer_attributes.values.map { |options| options.dig(:includes) }.compact.flatten(1).uniq
|
77
|
-
fields = build_describer_fields(describer_attributes)
|
78
|
-
hash = {
|
79
|
-
name: describer_name,
|
80
|
-
kind: describer_options[:kind],
|
81
|
-
database: describer_options[:database],
|
82
|
-
attributes: describer_attributes,
|
83
|
-
includes: includes,
|
84
|
-
fields: fields
|
85
|
-
}
|
86
|
-
OpenStruct.new(hash)
|
87
|
-
end
|
60
|
+
private
|
88
61
|
|
89
62
|
def build_describer_fields(describer_attributes)
|
90
63
|
describer_attributes.inject({}) do |fields_hash, (name, options)|
|
data/lib/active_kit/version.rb
CHANGED