insights_export 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 361e4d874de27627948c5a1f93e4c33cb849a277
4
- data.tar.gz: 0bc4adb9a0092de2972417d4e24b70a44a219fe1
3
+ metadata.gz: 9b3cc11cb225f05541df399578423bb370fad699
4
+ data.tar.gz: bbc9af0241b380c0f91d99a425016e3ccf5e87c9
5
5
  SHA512:
6
- metadata.gz: b0651f79bee2317848cd3645475512e020f0a19ebf013d677689bc3e40f7e2721a08e37c2477b6afc7dfd395015f899d8d1e5e6c9af6719e14d068b4cde506b1
7
- data.tar.gz: b2ccc5c8b845b0a62f54310996a8a95478da5548c81930e34bcd1af181501a9e0db97456915e32142dc4e0f3e8b51816064c2484572152d2d6ed67d9deb62d5b
6
+ metadata.gz: b7ddc9fffe0f31cef0d90059e648eb0f7b21ab153b1056525c1868fc4b989b35f6743756ec990dc088e76cc530c5ce3f4d490e71c122e18cd73240f534809e1e
7
+ data.tar.gz: b5c8921153667c08f4e5754703781c5700d5ef684b67f209a8fb76e2587933ad2493599eabd9626ba42e46155d17315533fb78e6cd29aadb572f237e77b7783f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- insights_export (0.1.0)
4
+ insights_export (0.2.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -9,8 +9,7 @@ To configure the gem, create a file like `config/initializers/insights_export.rb
9
9
  ```rb
10
10
  InsightsExport.configure do |config|
11
11
  # The path to the export file
12
- # Defaults to "#{Rails.root}/config/insights.yml"
13
- config.export_path = "#{Rails.root}/config/insights2.yml"
12
+ config.export_path = "#{Rails.root}/config/insights.yml"
14
13
 
15
14
  # Export only models in this list
16
15
  # Array of strings to filter or blank to export all.
@@ -19,5 +18,8 @@ InsightsExport.configure do |config|
19
18
  # Exclude these models from the export
20
19
  # Array of strings to filter or blank to export all.
21
20
  config.except_models = []
21
+
22
+ # Print a backtrace when the export throws an exception.
23
+ config.debug = false
22
24
  end
23
25
  ```
@@ -12,10 +12,15 @@ module InsightsExport
12
12
  # Array of strings to filter or empty array to export all.
13
13
  attr_accessor :except_models
14
14
 
15
+ # Print a backtrace when the export throws an exception.
16
+ # Default: false
17
+ attr_accessor :debug
18
+
15
19
  def initialize
16
20
  @export_path = "#{Rails.root}/config/insights.yml"
17
21
  @only_models = []
18
22
  @except_models = []
23
+ @debug = false
19
24
  end
20
25
  end
21
26
 
@@ -67,88 +67,107 @@ module InsightsExport
67
67
 
68
68
  model_strings = models.map(&:to_s)
69
69
 
70
- models.map do |model|
70
+ return_object = {}
71
+
72
+ models.each do |model|
71
73
  begin
72
74
  columns_hash = model.columns_hash
73
75
  rescue
74
76
  next
75
77
  end
76
78
 
77
- model_structure = {
78
- enabled: true,
79
- model: model.to_s,
80
- table_name: model.table_name,
81
- primary_key: model.primary_key,
82
- columns: columns_hash.map do |key, column|
83
- obj = if column.type.in? %i(datetime date)
84
- { type: :time }
85
- elsif column.type.in? %i(integer decimal float)
86
- { type: :number }
87
- elsif column.type.in? %i(string text)
88
- { type: :string }
89
- elsif column.type.in? %i(boolean)
90
- { type: :boolean }
91
- elsif column.type.in? %i(json)
92
- { type: :payload }
93
- elsif column.type.in? %i(geography)
94
- { type: :geo }
95
- else
96
- puts "Warning! Unknown column type: :#{column.type} for #{model.to_s}, column #{key}"
97
- { unknown: column.type }
98
- end
99
-
100
- if key == model.primary_key
101
- obj[:index] = :primary_key
102
- end
79
+ begin
80
+ model_structure = {
81
+ enabled: true,
82
+ model: model.to_s,
83
+ table_name: model.table_name,
84
+ primary_key: model.primary_key,
85
+ columns: columns_hash.map do |key, column|
86
+ obj = if column.type.in? %i(datetime date)
87
+ { type: :time }
88
+ elsif column.type.in? %i(integer decimal float)
89
+ { type: :number }
90
+ elsif column.type.in? %i(string text)
91
+ { type: :string }
92
+ elsif column.type.in? %i(boolean)
93
+ { type: :boolean }
94
+ elsif column.type.in? %i(json)
95
+ { type: :payload }
96
+ elsif column.type.in? %i(geography)
97
+ { type: :geo }
98
+ else
99
+ puts "Warning! Unknown column type: :#{column.type} for #{model.to_s}, column #{key}"
100
+ { unknown: column.type }
101
+ end
102
+
103
+ if key == model.primary_key
104
+ obj[:index] = :primary_key
105
+ end
103
106
 
104
- [key.to_sym, obj]
105
- end.to_h,
106
- custom: {},
107
- # aggregate: {
108
- # count: {
109
- # sql: "count($$.#{model.primary_key})"
110
- # }
111
- # },
112
- links: {
113
- incoming: {},
114
- outgoing: {}
107
+ [key.to_sym, obj]
108
+ end.to_h,
109
+ custom: {},
110
+ links: {
111
+ incoming: {},
112
+ outgoing: {}
113
+ }
115
114
  }
116
- }
117
-
118
- model.reflections.each do |association_name, reflection|
119
- reflection_class = reflection.class_name.gsub(/^::/, '')
120
-
121
- next unless model_strings.include?(reflection_class)
122
115
 
123
- if reflection.macro == :belongs_to
124
- # reflection_class # User
125
- # reflection.foreign_key # user_id
126
- # reflection.association_primary_key # id
116
+ model.reflections.each do |association_name, reflection|
117
+ begin
118
+ reflection_class = reflection.class_name.gsub(/^::/, '')
119
+
120
+ next unless model_strings.include?(reflection_class)
121
+
122
+ if reflection.macro == :belongs_to
123
+ raise 'Bla bla' if model.to_s == 'Product'
124
+ # reflection_class # User
125
+ # reflection.foreign_key # user_id
126
+ # reflection.association_primary_key # id
127
+
128
+ model_structure[:columns].delete(reflection.foreign_key.to_sym)
129
+ model_structure[:links][:outgoing][association_name] = {
130
+ model: reflection_class,
131
+ model_key: reflection.association_primary_key,
132
+ my_key: reflection.foreign_key
133
+ }
134
+ elsif reflection.macro.in? %i(has_one has_many)
135
+ # skip has_many :through associations
136
+ if reflection.options.try(:[], :through).present?
137
+ next
138
+ end
127
139
 
128
- model_structure[:columns].delete(reflection.foreign_key.to_sym)
129
- model_structure[:links][:outgoing][association_name] = {
130
- model: reflection_class,
131
- model_key: reflection.association_primary_key,
132
- my_key: reflection.foreign_key
133
- }
134
- elsif reflection.macro.in? %i(has_one has_many)
135
- # skip has_many :through associations
136
- if reflection.options.try(:[], :through).present?
137
- next
140
+ model_structure[:links][:incoming][association_name] = {
141
+ model: reflection_class,
142
+ model_key: reflection.foreign_key,
143
+ my_key: reflection.association_primary_key
144
+ }
145
+ else
146
+ puts "Warning! Unknown reflection :#{reflection.macro} for association '#{association_name}' on model '#{model.to_s}'"
147
+ end
148
+ rescue => error
149
+ puts "!! Error when exporting association '#{association_name}' on model '#{model.to_s}'"
150
+ print_exception(error)
138
151
  end
139
-
140
- model_structure[:links][:incoming][association_name] = {
141
- model: reflection_class,
142
- model_key: reflection.foreign_key,
143
- my_key: reflection.association_primary_key
144
- }
145
- else
146
- puts "Warning! Unknown reflection :#{reflection.macro} for association #{association_name} on model #{model.to_s}"
147
152
  end
153
+
154
+ return_object[model.to_s] = model_structure
155
+ rescue => error
156
+ puts "!! Error when exporting model '#{model.to_s}'"
157
+ print_exception(error)
148
158
  end
159
+ end
160
+
161
+ return_object.sort_by { |k, v| k }.to_h.deep_stringify_keys
162
+ end
149
163
 
150
- [model.to_s, model_structure]
151
- end.select(&:present?).sort_by { |k, v| k }.to_h.deep_stringify_keys
164
+ def self.print_exception(error)
165
+ puts "!! Exception: #{error.message}"
166
+ if InsightsExport.configuration.debug
167
+ puts error.backtrace
168
+ else
169
+ puts "-> Set config.debug = true to see the full backtrace"
170
+ end
152
171
  end
153
172
  end
154
173
  end
@@ -1,3 +1,3 @@
1
1
  module InsightsExport
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insights_export
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marius Andra