forest_liana 1.3.13 → 1.3.14

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: dda2efc4c742ed3cd6fb62658fea5a35e04c16e8
4
- data.tar.gz: 9d7915caa2864c57dd2cf23a4f0221284ed0a2e3
3
+ metadata.gz: 9fec685427c5d1e79f304f7f38820ed656f7c46c
4
+ data.tar.gz: 876e3e4c683f864ddf234e8e97e3906116b3bbd6
5
5
  SHA512:
6
- metadata.gz: deadf1eee8452d8dc83ee95ebccabf65381fc54d8bc882551159bcd1ecf981a597cddaec590b6386b2f8d52591ccfa3bcc2282101e86bde5b1b0f66f8a01d2db
7
- data.tar.gz: f8e5ce65f436eb67e0770dc2d0f101df796c5249430142c6ef47ed619ded0d91db5df64b031715ccf13fb7b8f7a2f389c9871280a898fdd0978df815af5db142
6
+ metadata.gz: 154ad6d095ec785ee483ea2262348dea4fdb5456c950602a834dea5c5c3af974f21b48909e95cb69ec610b9fb71b57fa83193f1bd4913d272547ec5f0d930178
7
+ data.tar.gz: b700add97404c94f21b2de45e3f64c261a86cf545189508f3f74ad3ddacb68aafd78c8c69af85825a508c6689c147ae3c3670011a61f082364afcecea6923026
@@ -0,0 +1,58 @@
1
+ module ForestLiana
2
+ class OperatorDateIntervalParser
3
+ PERIODS = {
4
+ yesterday: { duration: 1, period: 'day' },
5
+ lastWeek: { duration: 1, period: 'week' },
6
+ last2Weeks: { duration: 2, period: 'week' },
7
+ lastMonth: { duration: 1, period: 'month' },
8
+ last3Months: { duration: 3, period: 'month' },
9
+ lastYear: { duration: 1, period: 'year' }
10
+ }
11
+
12
+ PERIODS_LAST_X_DAYS = /^last(\d+)days$/
13
+
14
+ def initialize(value)
15
+ @value = value
16
+ end
17
+
18
+ def is_interval_date_value
19
+ return true if PERIODS[@value.to_sym]
20
+
21
+ match = PERIODS_LAST_X_DAYS.match(@value)
22
+ return true if match && match[1]
23
+
24
+ false
25
+ end
26
+
27
+ def get_interval_date_filter
28
+ return nil unless is_interval_date_value()
29
+
30
+ match = PERIODS_LAST_X_DAYS.match(@value)
31
+ return ">= #{Integer(match[1]).day.ago}" if match && match[1]
32
+
33
+ duration = PERIODS[@value.to_sym][:duration]
34
+ period = PERIODS[@value.to_sym][:period]
35
+
36
+ from = duration.send(period).ago.send("beginning_of_#{period}")
37
+ to = 1.send(period).ago.send("end_of_#{period}")
38
+ "BETWEEN '#{from}' AND '#{to}'"
39
+ end
40
+
41
+ def get_interval_date_filter_for_previous_interval
42
+ return nil unless is_interval_date_value()
43
+
44
+ match = PERIODS_LAST_X_DAYS.match(@value)
45
+ if match && match[1]
46
+ return "BETWEEN #{Integer(match[1] * 2).day.ago}" +
47
+ " AND #{Integer(match[1]).day.ago}"
48
+ end
49
+
50
+ duration = PERIODS[@value.to_sym][:duration]
51
+ period = PERIODS[@value.to_sym][:period]
52
+
53
+ from = (duration * 2).send(period).ago.send("beginning_of_#{period}")
54
+ to = (1 + duration).send(period).ago.send("end_of_#{period}")
55
+ "BETWEEN '#{from}' AND '#{to}'"
56
+ end
57
+ end
58
+ end
@@ -30,38 +30,12 @@ module ForestLiana
30
30
  end
31
31
 
32
32
  def self.add_where(query, field, operator, value, resource)
33
- if field.split(':').size < 2
34
- field_name = "\"#{resource.table_name}\".\"#{field}\""
35
- else
36
- association = field.split(':')[0].pluralize
37
- field_name = "\"#{association}\".\"#{field.split(':')[1]}\""
38
- end
39
-
40
- match = /^last(\d+)days$/.match(value)
41
- if match && match[1]
42
- return query = query.where("#{field_name} >= ?",
43
- Integer(match[1]).day.ago)
44
- end
33
+ field_name = self.get_field_name(field, resource)
45
34
 
46
- case value
47
- when 'yesterday'
48
- query = query.where("#{field_name} BETWEEN " +
49
- "'#{1.day.ago.beginning_of_day}' AND '#{1.day.ago.end_of_day}'")
50
- when 'lastWeek'
51
- query = query.where("#{field_name} BETWEEN " +
52
- "'#{1.week.ago.beginning_of_week}' AND '#{1.week.ago.end_of_week}'")
53
- when 'last2Weeks'
54
- query = query.where("#{field_name} BETWEEN " +
55
- "'#{2.week.ago.beginning_of_week}' AND '#{1.week.ago.end_of_week}'")
56
- when 'lastMonth'
57
- query = query.where("#{field_name} BETWEEN " +
58
- "'#{1.month.ago.beginning_of_month}' AND '#{1.month.ago.end_of_month}'")
59
- when 'last3Months'
60
- query = query.where("#{field_name} BETWEEN " +
61
- "'#{3.month.ago.beginning_of_month}' AND '#{1.month.ago.end_of_month}'")
62
- when 'lastYear'
63
- query = query.where("#{field_name} BETWEEN " +
64
- "'#{1.year.ago.beginning_of_year}' AND '#{1.year.ago.end_of_year}'")
35
+ operator_date_interval_parser = OperatorDateIntervalParser.new(value)
36
+ if operator_date_interval_parser.is_interval_date_value()
37
+ filter = operator_date_interval_parser.get_interval_date_filter()
38
+ query = query.where("#{field_name} #{filter}")
65
39
  else
66
40
  where = "#{field_name} #{operator}"
67
41
  where += " '#{value}'" if value
@@ -69,5 +43,14 @@ module ForestLiana
69
43
  end
70
44
  end
71
45
 
46
+ def self.get_field_name(field, resource)
47
+ if field.split(':').size < 2
48
+ "\"#{resource.table_name}\".\"#{field}\""
49
+ else
50
+ association = field.split(':')[0].pluralize
51
+ "\"#{association}\".\"#{field.split(':')[1]}\""
52
+ end
53
+ end
54
+
72
55
  end
73
56
  end
@@ -51,7 +51,7 @@ module ForestLiana
51
51
  def filter_param
52
52
  if @params[:filter]
53
53
  @params[:filter].each do |field, values|
54
- next if has_many_association?(field)
54
+ next if association?(field)
55
55
  values.split(',').each do |value|
56
56
  operator, value = OperatorValueParser.parse(value)
57
57
  @records = OperatorValueParser.add_where(@records, field, operator,
@@ -141,12 +141,18 @@ module ForestLiana
141
141
 
142
142
  operator, value = OperatorValueParser.parse(value)
143
143
 
144
- where = "#{association.table_name}.#{subfield} #{operator}"
145
- where += " '#{value}'" if value
146
-
147
144
  @records = @records
148
145
  .joins(field.to_sym)
149
- .where(where)
146
+
147
+ operator_date_interval_parser = OperatorDateIntervalParser.new(value)
148
+ if operator_date_interval_parser.is_interval_date_value()
149
+ filter = operator_date_interval_parser.get_interval_date_filter()
150
+ @records = @records.where("#{association.table_name}.#{subfield} #{filter}")
151
+ else
152
+ where = "#{association.table_name}.#{subfield} #{operator}"
153
+ where += " '#{value}'" if value
154
+ @records = @records.where(where)
155
+ end
150
156
  end
151
157
 
152
158
  def belongs_to_filter
@@ -9,15 +9,35 @@ module ForestLiana
9
9
 
10
10
  def perform
11
11
  return if @params[:aggregate].blank?
12
- value = @resource
12
+ valueCurrent = @resource
13
+ valuePrevious = @resource
13
14
 
14
15
  @params[:filters].try(:each) do |filter|
15
16
  operator, filter_value = OperatorValueParser.parse(filter[:value])
16
- value = OperatorValueParser.add_where(value, filter[:field], operator,
17
- filter_value, @resource)
17
+ valueCurrent = OperatorValueParser.add_where(valueCurrent,
18
+ filter[:field], operator, filter_value, @resource)
18
19
  end
19
20
 
20
- @record = Model::Stat.new(value: count(value))
21
+ filter_date_interval = false
22
+ @params[:filters].try(:each) do |filter|
23
+ operator, filter_value = OperatorValueParser.parse(filter[:value])
24
+ operator_date_interval_parser = OperatorDateIntervalParser.new(filter_value)
25
+ if operator_date_interval_parser.is_interval_date_value()
26
+ field_name = OperatorValueParser.get_field_name(filter[:field], @resource)
27
+ filter = operator_date_interval_parser
28
+ .get_interval_date_filter_for_previous_interval()
29
+ valuePrevious = valuePrevious.where("#{field_name} #{filter}")
30
+ filter_date_interval = true
31
+ else
32
+ valuePrevious = OperatorValueParser.add_where(valuePrevious,
33
+ filter[:field], operator, filter_value, @resource)
34
+ end
35
+ end
36
+
37
+ @record = Model::Stat.new(value: {
38
+ countCurrent: count(valueCurrent),
39
+ countPrevious: filter_date_interval ? count(valuePrevious) : nil
40
+ })
21
41
  end
22
42
 
23
43
  private
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "1.3.13"
2
+ VERSION = "1.3.14"
3
3
  end
@@ -141,5 +141,20 @@ module ForestLiana
141
141
  assert count = 1
142
142
  assert records.first.id == 5
143
143
  end
144
+
145
+ test 'Filter equal on an updated_at field of an associated collection' do
146
+ getter = ResourcesGetter.new(Tree, {
147
+ page: { size: 10, number: 1 },
148
+ filter: {
149
+ 'owner:updated_at' => 'Sat Jul 02 2016 11:52:00 GMT-0400 (EDT)',
150
+ }
151
+ })
152
+ getter.perform
153
+ records = getter.records
154
+ count = getter.count
155
+
156
+ assert records.count == 0
157
+ assert count = 0
158
+ end
144
159
  end
145
160
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.13
4
+ version: 1.3.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-07 00:00:00.000000000 Z
11
+ date: 2016-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -165,6 +165,7 @@ files:
165
165
  - app/services/forest_liana/intercom_attributes_getter.rb
166
166
  - app/services/forest_liana/intercom_conversations_getter.rb
167
167
  - app/services/forest_liana/line_stat_getter.rb
168
+ - app/services/forest_liana/operator_date_interval_parser.rb
168
169
  - app/services/forest_liana/operator_value_parser.rb
169
170
  - app/services/forest_liana/pie_stat_getter.rb
170
171
  - app/services/forest_liana/resource_creator.rb