tailor_made 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 863305812d05db9660d68bf6792229fb6342287677b2ba35c2c894c159d96e21
4
+ data.tar.gz: bfcb7f8f175ae9dc099df303e2073352aa22529c8ebd9e83da50c97f54be8f63
5
+ SHA512:
6
+ metadata.gz: b5b39873f2ab75d2a6787658d78538400a0b6251d45a48eb3c9fba042a237020b67a60a3e6619f25aa51a14563b4cc5b9db4ed263f1ab42e8f22a414931da3b3
7
+ data.tar.gz: ea191ba8ef5e24b3a6516007128370129d10fe88bb8d55d33d60c9135965dab49df1168377ebeb55fc78923671650c6f1ce6160a9d50d6759e8d01c53391554c
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .DS_Store
14
+ vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tailor_made.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,144 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tailor_made (0.1.0)
5
+ activerecord (>= 4.2)
6
+ railties (>= 4.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.2.2.1)
12
+ actionpack (= 5.2.2.1)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailer (5.2.2.1)
16
+ actionpack (= 5.2.2.1)
17
+ actionview (= 5.2.2.1)
18
+ activejob (= 5.2.2.1)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.2.2.1)
22
+ actionview (= 5.2.2.1)
23
+ activesupport (= 5.2.2.1)
24
+ rack (~> 2.0)
25
+ rack-test (>= 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.2.2.1)
29
+ activesupport (= 5.2.2.1)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.2.2.1)
35
+ activesupport (= 5.2.2.1)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.2.2.1)
38
+ activesupport (= 5.2.2.1)
39
+ activerecord (5.2.2.1)
40
+ activemodel (= 5.2.2.1)
41
+ activesupport (= 5.2.2.1)
42
+ arel (>= 9.0)
43
+ activestorage (5.2.2.1)
44
+ actionpack (= 5.2.2.1)
45
+ activerecord (= 5.2.2.1)
46
+ marcel (~> 0.3.1)
47
+ activesupport (5.2.2.1)
48
+ concurrent-ruby (~> 1.0, >= 1.0.2)
49
+ i18n (>= 0.7, < 2)
50
+ minitest (~> 5.1)
51
+ tzinfo (~> 1.1)
52
+ arel (9.0.0)
53
+ builder (3.2.3)
54
+ concurrent-ruby (1.1.5)
55
+ crass (1.0.4)
56
+ diff-lcs (1.3)
57
+ erubi (1.8.0)
58
+ globalid (0.4.2)
59
+ activesupport (>= 4.2.0)
60
+ i18n (1.6.0)
61
+ concurrent-ruby (~> 1.0)
62
+ loofah (2.2.3)
63
+ crass (~> 1.0.2)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.7.1)
66
+ mini_mime (>= 0.1.1)
67
+ marcel (0.3.3)
68
+ mimemagic (~> 0.3.2)
69
+ method_source (0.9.2)
70
+ mimemagic (0.3.3)
71
+ mini_mime (1.0.1)
72
+ mini_portile2 (2.4.0)
73
+ minitest (5.11.3)
74
+ nio4r (2.3.1)
75
+ nokogiri (1.10.1)
76
+ mini_portile2 (~> 2.4.0)
77
+ rack (2.0.6)
78
+ rack-test (1.1.0)
79
+ rack (>= 1.0, < 3)
80
+ rails (5.2.2.1)
81
+ actioncable (= 5.2.2.1)
82
+ actionmailer (= 5.2.2.1)
83
+ actionpack (= 5.2.2.1)
84
+ actionview (= 5.2.2.1)
85
+ activejob (= 5.2.2.1)
86
+ activemodel (= 5.2.2.1)
87
+ activerecord (= 5.2.2.1)
88
+ activestorage (= 5.2.2.1)
89
+ activesupport (= 5.2.2.1)
90
+ bundler (>= 1.3.0)
91
+ railties (= 5.2.2.1)
92
+ sprockets-rails (>= 2.0.0)
93
+ rails-dom-testing (2.0.3)
94
+ activesupport (>= 4.2.0)
95
+ nokogiri (>= 1.6)
96
+ rails-html-sanitizer (1.0.4)
97
+ loofah (~> 2.2, >= 2.2.2)
98
+ railties (5.2.2.1)
99
+ actionpack (= 5.2.2.1)
100
+ activesupport (= 5.2.2.1)
101
+ method_source
102
+ rake (>= 0.8.7)
103
+ thor (>= 0.19.0, < 2.0)
104
+ rake (10.5.0)
105
+ rspec (3.8.0)
106
+ rspec-core (~> 3.8.0)
107
+ rspec-expectations (~> 3.8.0)
108
+ rspec-mocks (~> 3.8.0)
109
+ rspec-core (3.8.0)
110
+ rspec-support (~> 3.8.0)
111
+ rspec-expectations (3.8.2)
112
+ diff-lcs (>= 1.2.0, < 2.0)
113
+ rspec-support (~> 3.8.0)
114
+ rspec-mocks (3.8.0)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.8.0)
117
+ rspec-support (3.8.0)
118
+ sprockets (3.7.2)
119
+ concurrent-ruby (~> 1.0)
120
+ rack (> 1, < 3)
121
+ sprockets-rails (3.2.1)
122
+ actionpack (>= 4.0)
123
+ activesupport (>= 4.0)
124
+ sprockets (>= 3.0.0)
125
+ thor (0.20.3)
126
+ thread_safe (0.3.6)
127
+ tzinfo (1.2.5)
128
+ thread_safe (~> 0.1)
129
+ websocket-driver (0.7.0)
130
+ websocket-extensions (>= 0.1.0)
131
+ websocket-extensions (0.1.3)
132
+
133
+ PLATFORMS
134
+ ruby
135
+
136
+ DEPENDENCIES
137
+ bundler (~> 1.16)
138
+ rails (>= 5.0)
139
+ rake (~> 10.0)
140
+ rspec (~> 3.0)
141
+ tailor_made!
142
+
143
+ BUNDLED WITH
144
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 Pedro Carmona
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # TailorMade
2
+
3
+ Currently in development.
4
+
5
+ Gem to query and plot grouped data. Makes it easy for people without sql knowledge to explore data.
6
+ Uses active record.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'tailor_made'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install tailor_made
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tailor_made.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tailor_made"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require "tailor_made/version"
2
+ require "tailor_made/group_alias"
3
+ require "tailor_made/relation_alias"
4
+ require "tailor_made/methods"
5
+ require "tailor_made/query"
6
+
7
+ module TailorMade
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,5 @@
1
+ module TailorMade
2
+ module GroupAlias
3
+ attr_accessor :relation, :name
4
+ end
5
+ end
@@ -0,0 +1,58 @@
1
+ module TailorMade
2
+ module Methods
3
+ def self.included(base) # :nodoc:
4
+ const_set('CANONICAL_DIMENSIONS', [])
5
+ const_set('CANONICAL_DOMAIN', {})
6
+ const_set('CANONICAL_ANCHORS', {})
7
+ const_set('TAILOR_MADE_MEASURES', [])
8
+ const_set('TAILOR_MADE_MEASURE_FORMULA', {})
9
+ const_set('DATETIME_COLUMNS', [])
10
+ const_set('DATETIME_DIMENSIONS', {})
11
+ const_set('TAILOR_MADE_MEASURES_DATETIME_PERMITED', {})
12
+ base.extend ClassMethods
13
+ end
14
+
15
+ module ClassMethods
16
+ def dimension(*attributes)
17
+ dimension = attributes[0]
18
+ return if CANONICAL_DIMENSIONS.include?(dimension)
19
+ CANONICAL_DIMENSIONS << dimension
20
+
21
+ attr_accessor dimension
22
+ CANONICAL_DOMAIN[dimension] = attributes[1][:domain] if attributes[1] && attributes[1][:domain]
23
+ CANONICAL_ANCHORS[dimension] = attributes[1][:anchor] if attributes[1] && attributes[1][:anchor]
24
+ end
25
+
26
+ def measure(*attributes)
27
+ measure = attributes[0]
28
+ return if TAILOR_MADE_MEASURES.include?(measure)
29
+ TAILOR_MADE_MEASURES << measure
30
+
31
+ if attributes[1] && attributes[1][:formula]
32
+ TAILOR_MADE_MEASURE_FORMULA[measure] = attributes[1][:formula]
33
+ end
34
+ end
35
+
36
+ def datetime_dimension(*attributes)
37
+ dimension = attributes[0]
38
+ return if DATETIME_COLUMNS.include?(dimension)
39
+ DATETIME_COLUMNS << dimension
40
+ attr_accessor "#{dimension.to_s}_starts_at".to_sym
41
+ attr_accessor "#{dimension.to_s}_ends_at".to_sym
42
+
43
+ if attributes[1] && attributes[1][:permit]
44
+ permit = attributes[1][:permit]
45
+ else
46
+ permit = Groupdate::PERIODS
47
+ end
48
+
49
+ TAILOR_MADE_MEASURES_DATETIME_PERMITED[dimension] = permit
50
+
51
+ DATETIME_DIMENSIONS[dimension] = permit.map do |period|
52
+ [dimension, period].join("_").to_sym
53
+ end
54
+ # groups (day, month, year..)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,328 @@
1
+ module TailorMade
2
+ class Query
3
+ include ActiveModel::Model
4
+ include TailorMade::Methods # creates accesors for dimensions and measures
5
+
6
+ attr_accessor :measures
7
+ attr_accessor :dimensions
8
+ attr_accessor :plot_measure
9
+ attr_accessor :chart
10
+ attr_reader :options_for_select
11
+ attr_reader :scope
12
+
13
+ CHARTS = [
14
+ :pie_chart,
15
+ :line_chart,
16
+ :column_chart,
17
+ :bar_chart,
18
+ :area_chart,
19
+ :scatter_chart,
20
+ :geo_chart,
21
+ :timeline,
22
+ ]
23
+
24
+ def initialize(attributes={})
25
+ super
26
+ @dimensions ||= default_dimensions
27
+ @dimensions = @dimensions.reject(&:blank?).map(&:to_sym)
28
+ @measures ||= default_measures
29
+ @measures = @measures.reject(&:blank?).map(&:to_sym)
30
+ @plot_measure ||= measures.first
31
+
32
+ set_datetime_ranges
33
+ @scope = build_scope(from, dimensions)
34
+ end
35
+
36
+ def from
37
+ fail(NotImplementedError) # required to implement in subclass
38
+ end
39
+
40
+ def options_for_select
41
+ fail(NotImplementedError) # required in beta version
42
+ end
43
+
44
+ def chart
45
+ @chart ||= :pie_chart
46
+ end
47
+
48
+ def plot
49
+ result = scope.order(order).pluck(plot_formulas(dimensions, scope))
50
+ return result if dimensions.size < 2
51
+ result.map { |row| row[1...-1] }.uniq.map { |combination|
52
+ {
53
+ name: combination.join("#"),
54
+ data: combination_data(dimension_groups_sorted(dimensions, scope), combination)
55
+ }
56
+ }
57
+ end
58
+
59
+ def combination_data(groups, combination)
60
+ # combination.each_with_index.map { |param, index| [groups[index + 1].to_sym , param] }
61
+ scope = from
62
+ combination.each_with_index.each { |param, index|
63
+ scope = scope.where("#{groups[index + 1]} = ?", param)
64
+ }
65
+ plot_dimension = groups[0]
66
+ scope = build_scope(scope, [plot_dimension])
67
+ scope.order(
68
+ { dimension_alias(plot_dimension, scope).to_sym => :asc }
69
+ ).pluck(
70
+ plot_formulas([plot_dimension], scope)
71
+ )
72
+ end
73
+
74
+ def all
75
+ scope.order(order).select(table_formulas)
76
+ end
77
+
78
+ def table_columns
79
+ @table_columns ||= begin
80
+ group_values = @scope.group_values.map { |column|
81
+ (column.respond_to? :name) ? group_alias(column) : column
82
+ }
83
+ group_values + measures
84
+ end
85
+ end
86
+
87
+
88
+
89
+ def tabelize(row, column)
90
+ if CANONICAL_ANCHORS[column].nil?
91
+ row.send(column)
92
+ else
93
+ if CANONICAL_ANCHORS[column].respond_to? :call
94
+ CANONICAL_ANCHORS[column] = CANONICAL_ANCHORS[column].call
95
+ end
96
+ result = CANONICAL_ANCHORS[column][row.send(column)]
97
+ result = [row.send(column), result] if result
98
+ result ||= row.send(column)
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def build_scope(scope, dimensions)
105
+ scope = build_canonical_scope(scope)
106
+
107
+ scope = build_datetime_dimensions_scope(scope, dimensions)
108
+
109
+ datetime_dimensions = DATETIME_DIMENSIONS.values().flatten
110
+ unless (dimensions - datetime_dimensions).empty?
111
+ scope = scope.group(dimensions - datetime_dimensions)
112
+ end
113
+ scope
114
+ end
115
+
116
+ def build_canonical_scope(scope)
117
+ CANONICAL_DIMENSIONS.each do |dimension|
118
+ scope = scope.where('#{dimension} LIKE ?', "%#{send(dimension)}%") if send(dimension).present?
119
+ end
120
+ scope
121
+ end
122
+
123
+ # Datetime
124
+
125
+ def build_datetime_dimensions_scope(scope, dimensions)
126
+ DATETIME_COLUMNS.each do |dimension|
127
+ scope = build_datetime_dimension_scope(scope, dimension, dimensions)
128
+ end
129
+ scope
130
+ end
131
+
132
+ def build_datetime_dimension_scope(scope, dimension, dimensions)
133
+ starts_at = instance_variable_get("@#{dimension}_starts_at")
134
+ ends_at = instance_variable_get("@#{dimension}_ends_at")
135
+ permit = TAILOR_MADE_MEASURES_DATETIME_PERMITED[dimension]
136
+
137
+ if !starts_at.blank? && !ends_at.blank?
138
+ scope = scope.where(dimension.to_sym => starts_at..ends_at)
139
+ end
140
+
141
+ datetime_dimension_periods(dimension, dimensions).each do |datetime_dimension|
142
+ period_type = get_datetime_dimension_period(dimension, datetime_dimension)
143
+ scope = scope.group_by_period(period_type, dimension, permit: permit)
144
+ end
145
+ scope
146
+ end
147
+
148
+ def datetime_dimension_periods(datetime_dimension, dimensions)
149
+ dimensions.select { |dimension|
150
+ DATETIME_DIMENSIONS[datetime_dimension].include?(dimension)
151
+ }
152
+ end
153
+
154
+ def get_datetime_dimension_period(dimension, datetime_dimension)
155
+ datetime_dimension.to_s.reverse.chomp((dimension.to_s + "_").reverse).reverse.to_sym
156
+ end
157
+
158
+ def set_datetime_ranges
159
+ DATETIME_COLUMNS.each do |dimension|
160
+ set_datetime_range(dimension)
161
+ end
162
+ end
163
+
164
+ def set_datetime_range(dimension)
165
+ starts_at = "@#{dimension}_starts_at"
166
+ ends_at = "@#{dimension}_ends_at"
167
+
168
+ instance_variable_set(
169
+ starts_at,
170
+ Date.parse(instance_variable_get(starts_at))
171
+ ) unless instance_variable_get(starts_at).blank?
172
+
173
+ instance_variable_set(
174
+ ends_at,
175
+ Date.parse(instance_variable_get(ends_at))
176
+ ) unless instance_variable_get(ends_at).blank?
177
+
178
+ return unless instance_variable_get(starts_at).blank?
179
+ return unless instance_variable_get(ends_at).blank?
180
+ return if datetime_dimension_periods(dimension, dimensions).empty?
181
+ set_datetime_default(dimension)
182
+ end
183
+
184
+ def set_datetime_default(dimension)
185
+ starts_at_options = datetime_dimension_periods(dimension, dimensions).map do |period|
186
+ default_range_start(dimension)
187
+ end
188
+
189
+ ends_at_options = datetime_dimension_periods(dimension, dimensions).map do |period|
190
+ default_range_start(dimension)
191
+ end
192
+
193
+ instance_variable_set("@#{dimension}_starts_at", starts_at_options.min)
194
+ instance_variable_set("@#{dimension}_ends_at", ends_at_options.max)
195
+ end
196
+
197
+ def default_range_start(period)
198
+ case period
199
+ when :second
200
+ Time.now.utc.to_date.beginning_of_day
201
+ when :minute
202
+ Time.now.utc.to_date.beginning_of_day
203
+ when :hour
204
+ Time.now.utc.to_date.beginning_of_day
205
+ when :day
206
+ Time.now.utc.to_date.beginning_of_week
207
+ when :week
208
+ Time.now.utc.to_date.beginning_of_month
209
+ when :month
210
+ Time.now.utc.to_date.beginning_of_year
211
+ when :quarter
212
+ Time.now.utc.to_date.beginning_of_year
213
+ when :year
214
+ Time.now.utc.to_date.beginning_of_year - 5.years
215
+ when :day_of_week
216
+ Time.now.utc.to_date.beginning_of_week
217
+ when :hour_of_day
218
+ Time.now.utc.to_date.beginning_of_day
219
+ when :minute_of_hour
220
+ Time.now.utc.to_date.beginning_of_day
221
+ when :day_of_month
222
+ Time.now.utc.to_date.beginning_of_month
223
+ when :month_of_year
224
+ Time.now.utc.to_date.beginning_of_year
225
+ else
226
+ nil
227
+ end
228
+ end
229
+
230
+ def default_range_end(period)
231
+ case period
232
+ when :second
233
+ Time.now.utc
234
+ when :minute
235
+ Time.now.utc
236
+ when :hour
237
+ Time.now.utc
238
+ when :day
239
+ Time.now.utc.to_date
240
+ when :week
241
+ Time.now.utc.to_date
242
+ when :month
243
+ Time.now.utc.to_date
244
+ when :quarter
245
+ Time.now.utc.to_date
246
+ when :year
247
+ Time.now.utc.to_date
248
+ when :day_of_week
249
+ Time.now.utc.to_date
250
+ when :hour_of_day
251
+ Time.now.utc.to_date
252
+ when :minute_of_hour
253
+ Time.now.utc.to_date
254
+ when :day_of_month
255
+ Time.now.utc.to_date
256
+ when :month_of_year
257
+ Time.now.utc.to_date
258
+ else
259
+ nil
260
+ end
261
+ end
262
+
263
+ # Order and selects
264
+
265
+ def order(scope = @scope)
266
+ col = scope.group_values.map { |column|
267
+ (column.respond_to? :name) ? group_alias(column) : column
268
+ }.first
269
+ { col.to_sym => :asc }
270
+ end
271
+
272
+ def group_alias(group)
273
+ aliaz = "#{group.relation.name}.#{group.name}"
274
+ scope.connection.table_alias_for(aliaz)
275
+ end
276
+
277
+ def dimension_alias(dimension, scope)
278
+ group = find_group(dimension, scope.group_values)
279
+ if group.respond_to? :name
280
+ group_alias(group)
281
+ else
282
+ group
283
+ end
284
+ end
285
+
286
+ def dimensions_formulas(groups)
287
+ groups.map { |group|
288
+ if group.respond_to? :name
289
+ "#{group} AS #{group_alias(group)}"
290
+ else
291
+ group
292
+ end
293
+ }
294
+ end
295
+
296
+ def dimension_groups_sorted(dimensions, scope)
297
+ dimensions.map do |dimension|
298
+ find_group(dimension, scope.group_values)
299
+ end
300
+ end
301
+
302
+ def plot_formulas(dimensions, scope)
303
+ groups = dimension_groups_sorted(dimensions, scope)
304
+ (dimensions_formulas(groups) + measure_formulas([plot_measure.to_sym]))
305
+ end
306
+
307
+ def table_formulas
308
+ groups = dimension_groups_sorted(dimensions, scope)
309
+ (dimensions_formulas(groups) + measure_formulas(measures))
310
+ end
311
+
312
+ def measure_formulas(measures)
313
+ measures.map { |measure|
314
+ [TAILOR_MADE_MEASURE_FORMULA[measure], measure].join(" AS ")
315
+ }
316
+ end
317
+
318
+ def find_group(dimension, groups)
319
+ groups.find {|group|
320
+ dimension == group ||
321
+ (
322
+ group.respond_to?(:name) &&
323
+ dimension == group.name.to_sym
324
+ )
325
+ }
326
+ end
327
+ end
328
+ end
@@ -0,0 +1,5 @@
1
+ module TailorMade
2
+ module RelationAlias
3
+ attr_accessor :name
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module TailorMade
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "tailor_made/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "tailor_made"
9
+ spec.version = TailorMade::VERSION
10
+ spec.authors = ["Pedro Carmona"]
11
+ spec.email = ["pcarmona1990@gmail.com"]
12
+
13
+ spec.summary = %q{Tailor made dashboards}
14
+ spec.description = %q{Create amazing analytical queries and dashboards in minutes.}
15
+ spec.homepage = "https://github.com/pedrocarmona/tailor_made"
16
+ spec.license = "MIT"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "railties", ">= 4.2"
28
+ spec.add_dependency "activerecord", ">= 4.2"
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.16"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "rails", ">= 5.0"
34
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tailor_made
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Carmona
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: Create amazing analytical queries and dashboards in minutes.
98
+ email:
99
+ - pcarmona1990@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/tailor_made.rb
115
+ - lib/tailor_made/group_alias.rb
116
+ - lib/tailor_made/methods.rb
117
+ - lib/tailor_made/query.rb
118
+ - lib/tailor_made/relation_alias.rb
119
+ - lib/tailor_made/version.rb
120
+ - tailor_made.gemspec
121
+ homepage: https://github.com/pedrocarmona/tailor_made
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.7.6
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Tailor made dashboards
145
+ test_files: []