azahara_schema 0.1.8 → 0.1.10

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
  SHA256:
3
- metadata.gz: 1eef15c0c5196c94a0e0620ab5cfc9f651b95137d070155311f435d7eea2a2aa
4
- data.tar.gz: 2e32dfd0f89bc3d5a86cd752df9a92dc45c2e8a475534a1f12dfea430c6b74c2
3
+ metadata.gz: 8f0db3b78bc382f903651efa08c761043b41b275a8083e80abd8312a469f4ebc
4
+ data.tar.gz: c04982362bae33d86ca54871434b4c1bb505736693474ad0d524b0b660e72b51
5
5
  SHA512:
6
- metadata.gz: 655ebda633024e86f39c2cd8bc88ac5ac4b8452c27f2929c96e42a1d6145248cf424c8985bbc61b137be3be8b9ccd6639b28d2596a3d094100903b59a96a306e
7
- data.tar.gz: 2561c92dd7dcf913289b9250662b259c3b3bf89bddb7c6009a174ff69503b0acb445597264845e3ab99fbfe3fad4921ac5cdc8f28da46cd2441cd992b39b13bb
6
+ metadata.gz: 306733e9ee95f23c196722090ef685803fe70362e7ea53926086ff06585ba7572c30203bf59213be4f4983a72a55346fc06b1e97e034e55c800ee363822d99d2
7
+ data.tar.gz: 1671a967d34d162d9afb2ed3f7712be43f11efb275241c597787395e289aa023cf5868a160fa73f125af7486eda3b0f832187a6fa9642638d56f12aed2043f78
@@ -46,7 +46,8 @@ module AzaharaSchema
46
46
  if association.macro == :has_many
47
47
  parent.public_send(association.name).collect{|record| attribute.value( record )}.flatten
48
48
  else
49
- attribute.value( parent.public_send(association.name) )
49
+ entity = parent.public_send(association.name)
50
+ attribute.value( entity ) if entity
50
51
  end
51
52
  end
52
53
 
@@ -54,6 +55,10 @@ module AzaharaSchema
54
55
  { association.name => attribute.association_hash }
55
56
  end
56
57
 
58
+ def arel_statement(operator, values)
59
+ attribute.arel_statement(operator, values)
60
+ end
61
+
57
62
  def add_join(scope)
58
63
  scope.includes(association_hash).references(association_hash)
59
64
  end
@@ -63,14 +63,15 @@ module AzaharaSchema
63
63
  record.public_send(name)
64
64
  end
65
65
 
66
+ # values has to be array!
66
67
  def arel_statement(operator, values)
67
- values = [values] unless values.is_a?(Array)
68
68
  case operator
69
69
  when '='
70
70
  arel_field.in(values)
71
71
  when '~'
72
- arl = arel_field.matches("%#{values[0]}%")
73
- values[1..-1].each{|v| arl = arl.or( arel_field.matches("%#{v}%") ) }
72
+ vals = values.collect{|v| v.split }.flatten
73
+ arl = arel_field.matches("%#{vals[0]}%")
74
+ vals[1..-1].each{|v| arl = arl.or( arel_field.matches("%#{v}%") ) }
74
75
  arl
75
76
  when '>='
76
77
  arel_field.gteq(values.map(&:to_f).min)
@@ -81,11 +82,16 @@ module AzaharaSchema
81
82
  end
82
83
  end
83
84
 
85
+ def add_join(scope)
86
+ scope
87
+ end
88
+
84
89
  def add_preload(scope)
85
90
  scope
86
91
  end
87
92
 
88
93
  def add_statement(scope, operator, values)
94
+ values = [values] unless values.is_a?(Array)
89
95
  scope.where(arel_statement(operator, values))
90
96
  end
91
97
 
@@ -0,0 +1,88 @@
1
+ # Author:: Ondřej Ezr (mailto:oezr@msp.justice.cz)
2
+ # Copyright:: Copyright (c) 2017 Ministry of Justice
3
+ # License:: Distributes under license Open Source Licence pro Veřejnou Správu a Neziskový Sektor v.1
4
+
5
+ module AzaharaSchema
6
+
7
+ # The class is attribute for derivateve attribute, it is used for working with combination of attributes as one attribute.
8
+ class DerivedAttribute < Attribute
9
+
10
+ attr_accessor :derivation_method
11
+ attr_accessor :attribute_names
12
+
13
+ def initialize(model, name, derivation_method, *attribute_names, **options)
14
+ self.attribute_names = attribute_names
15
+ self.derivation_method = derivation_method.to_sym
16
+ @options = options
17
+ super(model, name, type)
18
+ end
19
+
20
+ # ------------------| MY METHODS |----------------------
21
+
22
+ # TODO: check if attributes support derivation method
23
+ def attribute_names=(names)
24
+ @attributes = nil
25
+ @attribute_names = names
26
+ end
27
+
28
+ def attributes
29
+ @attributes ||= options[:attributes] || options[:schema].available_attributes_hash.slice(*attribute_names).values
30
+ end
31
+
32
+ def concat_divider
33
+ options[:divider] || ' '
34
+ end
35
+
36
+ def options
37
+ @options || {}
38
+ end
39
+
40
+ # -------------------| OVERRIDES |---------------------
41
+
42
+ def type
43
+ case derivation_method
44
+ when :concat
45
+ 'string'
46
+ else
47
+ raise "DerivedAttribute(#{name}) - derivation_method '#{derivation_method}' is not supported"
48
+ end
49
+ end
50
+
51
+ def arel_field
52
+ case derivation_method
53
+ when :concat
54
+ arel_fields = attributes.collect{|a| a.arel_field}
55
+ (1..arel_fields.length-1).to_a.reverse.each{|i| arel_fields.insert(i, Arel::Nodes::SqlLiteral.new("'#{concat_divider}'")) }
56
+ Arel::Nodes::NamedFunction.new 'CONCAT', arel_fields
57
+ else
58
+ raise "DerivedAttribute(#{name}) - derivation_method '#{derivation_method}' is not supported"
59
+ end
60
+ end
61
+
62
+ def value(record)
63
+ attributes.collect{|a| a.value(record) }.join(concat_divider)
64
+ end
65
+
66
+ def arel_statement(operator, values)
67
+ case operator
68
+ when '~'
69
+ arl = attributes[0].arel_statement(operator, values)
70
+ attributes[1..-1].each{|att| arl = arl.or( att.arel_statement(operator, values) ) }
71
+ arl
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ def add_join(scope)
78
+ attributes.each{ scope = add_join(scope) }
79
+ scope
80
+ end
81
+
82
+ def add_preload(scope)
83
+ attributes.each{ scope = add_preload(scope) }
84
+ scope
85
+ end
86
+
87
+ end
88
+ end
@@ -2,6 +2,7 @@ require 'azahara_schema/field_format'
2
2
  require 'azahara_schema/attribute'
3
3
  require 'azahara_schema/association_attribute'
4
4
  require 'azahara_schema/aggregation_attribute'
5
+ require 'azahara_schema/derived_attribute'
5
6
  require 'azahara_schema/attribute_name'
6
7
  require 'azahara_schema/outputs'
7
8
  require 'azahara_schema/output'
@@ -211,6 +211,7 @@ module AzaharaSchema
211
211
  scope = att.add_sort(scope, order) if att
212
212
  end
213
213
  if (tokens = tokenize_search_query)
214
+ searchable_attributes.each{|a| scope = a.add_join(scope) }
214
215
  arl = searchable_attributes[0].arel_statement('~', tokens) if searchable_attributes.any?
215
216
  Array(searchable_attributes[1..-1]).each{|att| arl = arl.or( att.arel_statement('~', tokens) ) }
216
217
  scope = scope.where(arl)
@@ -227,8 +228,17 @@ module AzaharaSchema
227
228
  attr_hash = entity.as_json(options)
228
229
  # TODO serializable_add_includes(options) do |association, records, opts|
229
230
  columns.each do |col|
230
- attr_hash[col.name] = col.available_values.detect{|l, v| v == attr_hash[col.name] }.try(:[], 0) if col.type == 'love'
231
- attr_hash[col.name] = col.value(entity) if col.is_a?(AzaharaSchema::AggregationAttribute)
231
+ col_sub_hash = attr_hash
232
+ sub_col = col
233
+ while sub_col.is_a?(AzaharaSchema::AssociationAttribute)
234
+ col_sub_hash = (col_sub_hash[sub_col.association.name.to_s] ||= {})
235
+ sub_col = sub_col.attribute
236
+ end
237
+ if col.type == 'love'
238
+ col_sub_hash[sub_col.name] = sub_col.available_values.detect{|l, v| v == col_sub_hash[sub_col.name] }.try(:[], 0)
239
+ else
240
+ col_sub_hash[sub_col.name] = col.value(entity)
241
+ end
232
242
  end
233
243
  attr_hash
234
244
  end
@@ -261,7 +271,7 @@ module AzaharaSchema
261
271
  params = {}
262
272
  params[:f] = {}
263
273
  filters.each do |fname, attrs|
264
- params[:f][fname] = "#{attrs[:o]}|#{attrs[:v]}"
274
+ params[:f][fname] = "#{attrs[:o]}|#{attrs[:v].is_a?(Array) ? attrs[:v].collect{|v| v.to_s}.join('\\') : attrs[:v]}"
265
275
  end
266
276
  params[:c] = column_names
267
277
  params[:q] = search_query if params[:q]
@@ -1,3 +1,3 @@
1
1
  module AzaharaSchema
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.10'
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.8
4
+ version: 0.1.10
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-08 00:00:00.000000000 Z
11
+ date: 2017-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -69,6 +69,7 @@ files:
69
69
  - lib/azahara_schema/association_attribute.rb
70
70
  - lib/azahara_schema/attribute.rb
71
71
  - lib/azahara_schema/attribute_name.rb
72
+ - lib/azahara_schema/derived_attribute.rb
72
73
  - lib/azahara_schema/engine.rb
73
74
  - lib/azahara_schema/field_format.rb
74
75
  - lib/azahara_schema/model_schema.rb