azahara_schema 0.1.7 → 0.1.8

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
- SHA1:
3
- metadata.gz: fce09b13b68a342872733bbcc7fe3b18907a62af
4
- data.tar.gz: 67f2b89be27832d8881b9f19409991bd63801331
2
+ SHA256:
3
+ metadata.gz: 1eef15c0c5196c94a0e0620ab5cfc9f651b95137d070155311f435d7eea2a2aa
4
+ data.tar.gz: 2e32dfd0f89bc3d5a86cd752df9a92dc45c2e8a475534a1f12dfea430c6b74c2
5
5
  SHA512:
6
- metadata.gz: 4e38cac517db7b62ce49c2c02d94a886dc970d4765743cee6288901b06e007a81aad43feaa9a7b4307a41ae8bd16e8dbeb974515475807d10c1bf21b897b7561
7
- data.tar.gz: 49677a40199bcbf7e699351ca131225348d48fe82a96a503ccbd0045c23f72362f55de3f4d35b792687fd55f17059f4f731accbeba1abd1356ac3ca8603c074c
6
+ metadata.gz: 655ebda633024e86f39c2cd8bc88ac5ac4b8452c27f2929c96e42a1d6145248cf424c8985bbc61b137be3be8b9ccd6639b28d2596a3d094100903b59a96a306e
7
+ data.tar.gz: 2561c92dd7dcf913289b9250662b259c3b3bf89bddb7c6009a174ff69503b0acb445597264845e3ab99fbfe3fad4921ac5cdc8f28da46cd2441cd992b39b13bb
@@ -12,6 +12,10 @@ module AzaharaSchema
12
12
  super(model, 'sum:'+attribute.name, attribute.type)
13
13
  end
14
14
 
15
+ def searchable?
16
+ false
17
+ end
18
+
15
19
  def arel_field
16
20
  attribute.arel_field.sum
17
21
  end
@@ -38,6 +38,10 @@ module AzaharaSchema
38
38
  association.macro == :belongs_to
39
39
  end
40
40
 
41
+ def searchable?
42
+ false
43
+ end
44
+
41
45
  def value(parent)
42
46
  if association.macro == :has_many
43
47
  parent.public_send(association.name).collect{|record| attribute.value( record )}.flatten
@@ -55,32 +55,40 @@ module AzaharaSchema
55
55
  format.aggregable?
56
56
  end
57
57
 
58
- def value(record)
59
- record.public_send(name)
58
+ def searchable?
59
+ format.searchable?
60
60
  end
61
61
 
62
- def add_preload(scope)
63
- scope
62
+ def value(record)
63
+ record.public_send(name)
64
64
  end
65
65
 
66
- def add_statement(scope, operator, values)
66
+ def arel_statement(operator, values)
67
67
  values = [values] unless values.is_a?(Array)
68
68
  case operator
69
69
  when '='
70
- scope.where( arel_field.in(values) )
70
+ arel_field.in(values)
71
71
  when '~'
72
72
  arl = arel_field.matches("%#{values[0]}%")
73
73
  values[1..-1].each{|v| arl = arl.or( arel_field.matches("%#{v}%") ) }
74
- scope.where( arl )
74
+ arl
75
75
  when '>='
76
- scope.where( arel_field.gteq(values.map(&:to_f).min) )
76
+ arel_field.gteq(values.map(&:to_f).min)
77
77
  when '<='
78
- scope.where( arel_field.lteq(values.map(&:to_f).max) )
78
+ arel_field.lteq(values.map(&:to_f).max)
79
79
  else
80
80
  throw 'Unknown operator ' + operator.to_s
81
81
  end
82
82
  end
83
83
 
84
+ def add_preload(scope)
85
+ scope
86
+ end
87
+
88
+ def add_statement(scope, operator, values)
89
+ scope.where(arel_statement(operator, values))
90
+ end
91
+
84
92
  def add_sort(scope, order)
85
93
  scope.order( arel_sort_field.public_send(order) )
86
94
  end
@@ -51,6 +51,10 @@ module AzaharaSchema
51
51
  def aggregable?
52
52
  false
53
53
  end
54
+
55
+ def searchable?
56
+ false
57
+ end
54
58
  end
55
59
 
56
60
  class NumberFormat < Base
@@ -85,6 +89,10 @@ module AzaharaSchema
85
89
  def available_operators
86
90
  super.concat(['~'])
87
91
  end
92
+
93
+ def searchable?
94
+ true
95
+ end
88
96
  end
89
97
 
90
98
  class ListFormat < StringFormat
@@ -30,6 +30,7 @@ module AzaharaSchema
30
30
  end
31
31
 
32
32
  attr_accessor :model, :association, :parent_schema
33
+ attr_accessor :search_query
33
34
 
34
35
  def initialize(model, **attributes)
35
36
  @model = model
@@ -38,6 +39,10 @@ module AzaharaSchema
38
39
  @column_names = attributes[:columns]
39
40
  end
40
41
 
42
+ def searchable_attributes
43
+ @searchable_attributes ||= available_attributes.select{|a| a.searchable? }
44
+ end
45
+
41
46
  def column_names=(values)
42
47
  @column_names = values
43
48
  @columns = nil
@@ -189,6 +194,10 @@ module AzaharaSchema
189
194
  Outputs.new(self)
190
195
  end
191
196
 
197
+ def tokenize_search_query(query=search_query)
198
+ query.split if query
199
+ end
200
+
192
201
  def entities
193
202
  scope = model.respond_to?(:visible) ? model.visible : model.all
194
203
  columns.each do |col|
@@ -201,6 +210,11 @@ module AzaharaSchema
201
210
  att = attribute(name)
202
211
  scope = att.add_sort(scope, order) if att
203
212
  end
213
+ if (tokens = tokenize_search_query)
214
+ arl = searchable_attributes[0].arel_statement('~', tokens) if searchable_attributes.any?
215
+ Array(searchable_attributes[1..-1]).each{|att| arl = arl.or( att.arel_statement('~', tokens) ) }
216
+ scope = scope.where(arl)
217
+ end
204
218
  scope
205
219
  end
206
220
 
@@ -240,6 +254,7 @@ module AzaharaSchema
240
254
  add_sort(sort[:path], sort['desc'] == 'true' ? :desc : :asc )
241
255
  end
242
256
  end
257
+ self.search_query = params[:q] if params[:q]
243
258
  end
244
259
 
245
260
  def to_param
@@ -249,6 +264,7 @@ module AzaharaSchema
249
264
  params[:f][fname] = "#{attrs[:o]}|#{attrs[:v]}"
250
265
  end
251
266
  params[:c] = column_names
267
+ params[:q] = search_query if params[:q]
252
268
  params
253
269
  end
254
270
 
@@ -1,3 +1,3 @@
1
1
  module AzaharaSchema
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azahara_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondřej Ezr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-03 00:00:00.000000000 Z
11
+ date: 2017-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.6.14
100
+ rubygems_version: 2.7.0
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: Gem to support developement of rails application with schema over an entity