rao-query 0.0.32.pre → 0.0.33.pre

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: d5795dd9ae311311c889c61a81ab543e61decdcfea1ce011e05346bc64c126e4
4
- data.tar.gz: 3fb69a168ff65f63da380e006aeb4391e3ca11aaffc7dfbe18a82e4aa64bfcb3
3
+ metadata.gz: 5bf02e4d6062bd64bb3206afd970320b7dc2d284ac792f538d3207cf5bd648bd
4
+ data.tar.gz: 3835923ea7e41165c576769a9e5b415278eda5ee3a3d804cf85064c452f60cd4
5
5
  SHA512:
6
- metadata.gz: 2fd993ebe26edd4536958915445be9ac3fcbbeeec8a97b32a5b84566db5e8a5cd16041b2f5018db3a51292f8e2496ed0fb44576cc1f3b31a58ce61ad6d73a8e2
7
- data.tar.gz: 6865d09949f5ee67486e204f569d5382165f32938d45897c0a93ee5edda03cb23db63cc0cb5dd865b3f9aab760f8affad01d6be279f11b98de911c8f09e5d43b
6
+ metadata.gz: 1932ba6136ccc01c9652515782a838ae17837ea2f395a097ace5d791c6638060498dfc1e72a6fd73b6b6087007ac353edceb0052be3b94d5dbfc2dd414893b50
7
+ data.tar.gz: 58b279a8c3706ae7f1748342eef84661d1649f8080522184211e66568556ef9d529ca305740d9d7f6e4de38f82bef2534cb6440884cf00f3fb73c8b4304d286b
data/Rakefile CHANGED
@@ -14,12 +14,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
- APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
- load 'rails/tasks/engine.rake'
17
+ Bundler::GemHelper.install_tasks
19
18
 
20
-
21
- load 'rails/tasks/statistics.rake'
22
-
23
-
24
-
25
- require 'bundler/gem_tasks'
19
+ require 'rails/dummy/tasks'
@@ -60,10 +60,8 @@ module Rao
60
60
  end
61
61
 
62
62
  def normalize_key(key)
63
- splitted_key = key.split('_')
64
- predicate = splitted_key.last
65
- attribute = splitted_key[0..-2].join('_')
66
- "#{attribute}(#{predicate})"
63
+ attribute_name, predicate = Rao::Query::Operators.extract_attribute_name_and_predicate_from_name(key)
64
+ "#{attribute_name}(#{predicate})"
67
65
  end
68
66
  end
69
67
  end
@@ -2,7 +2,7 @@ module Rao
2
2
  module Query
3
3
  class FormBuilder < SimpleForm::FormBuilder
4
4
  def boolean(name, options = {})
5
- translated_label = translate_label_for_boolean(name)
5
+ translated_label = translate_label_for_boolean(name, options.delete(:association))
6
6
  options.reverse_merge!(collection: [[I18n.t("rao.query.form_builder.yes"), 1], [I18n.t("rao.query.form_builder.no"), 0]], include_blank: true, label: translated_label)
7
7
  input name, options
8
8
  end
@@ -32,9 +32,7 @@ module Rao
32
32
  private
33
33
 
34
34
  def translate_label(name, association = nil)
35
- splitted_name = name.to_s.split('_')
36
- attribute_name = splitted_name[0..-2].join('_')
37
- predicate = splitted_name.last
35
+ attribute_name, predicate = extract_attribute_name_and_predicate_from_name(name)
38
36
  translated_attribute_name = if association.nil?
39
37
  klass_name = object.original_model_class_name
40
38
  klass_name.constantize.human_attribute_name(attribute_name)
@@ -46,10 +44,8 @@ module Rao
46
44
  I18n.t("rao.query.form_builder.predicates.#{predicate}", attribute_name: translated_attribute_name)
47
45
  end
48
46
 
49
- def translate_label_for_boolean(name)
50
- splitted_name = name.to_s.split('_')
51
- attribute_name = splitted_name[0..-2].join('_')
52
- predicate = splitted_name.last
47
+ def translate_label_for_boolean(name, association = nil)
48
+ attribute_name, predicate = extract_attribute_name_and_predicate_from_name(name)
53
49
  if association.nil?
54
50
  klass_name = object.original_model_class_name
55
51
  else
@@ -58,6 +54,10 @@ module Rao
58
54
  translated_attribute_name = klass_name.constantize.human_attribute_name(attribute_name)
59
55
  I18n.t("rao.query.form_builder.boolean_label", attribute_name: translated_attribute_name)
60
56
  end
57
+
58
+ def extract_attribute_name_and_predicate_from_name(name)
59
+ Rao::Query::Operators.extract_attribute_name_and_predicate_from_name(name)
60
+ end
61
61
  end
62
62
  end
63
63
  end
@@ -1,18 +1,6 @@
1
1
  module Rao
2
2
  module Query
3
3
  class ConditionParser
4
- OPERATOR_MAP = {
5
- gt: :>,
6
- gt_or_eq: :>=,
7
- eq: :'=',
8
- not_eq: :'<>',
9
- lt_or_eq: :<=,
10
- lt: :<,
11
- null: :is_null,
12
- not_null: :is_not_null,
13
- cont: :like
14
- }
15
-
16
4
  def initialize(scope, field, condition)
17
5
  @scope, @field, @condition = scope, field, condition
18
6
  end
@@ -26,18 +14,12 @@ module Rao
26
14
  def build_condition_statement(parent_key, condition, nested = false)
27
15
  if is_a_condition?(parent_key) && !nested
28
16
  table, column, operator = extract_table_column_and_operator(parent_key)
29
- return handle_null_condition(column, operator) if is_null_operator?(operator)
30
- # binding.pry
17
+ return handle_null_condition(column, operator, condition) if is_null_operator?(operator)
31
18
  if operator == 'cont'
32
19
  return ["#{table}.#{column} LIKE ?", "%#{normalized_condition(table, column, condition)}%"]
33
20
  else
34
21
  return ["#{table}.#{column} = ?", normalized_condition(table, column, condition)]
35
22
  end
36
- # if column_is_boolean?(column)
37
- # ["#{column} = ?", to_boolean(condition)]
38
- # else
39
- # ["#{column} = ?", condition]
40
- # end
41
23
  else
42
24
  if nested
43
25
  column = extract_column(parent_key)
@@ -52,11 +34,18 @@ module Rao
52
34
  %w(null not_null).include?(operator)
53
35
  end
54
36
 
55
- def handle_null_condition(column, operator)
56
- case operator.to_sym
57
- when :null
37
+ # We build the condition for booleans here.
38
+ #
39
+ # | operator | condition | result |
40
+ # | 'null' | true | 'IS NULL' |
41
+ # | 'null' | false | 'IS NOT NULL' |
42
+ # | 'not_null' | true | 'IS NOT NULL' |
43
+ # | 'not_null' | false | 'IS NULL' |
44
+ #
45
+ def handle_null_condition(column, operator, condition)
46
+ if (operator == "null" && to_boolean(condition) == true) || (operator == "not_null" && to_boolean(condition) == false)
58
47
  "#{column} IS NULL"
59
- when :not_null
48
+ else
60
49
  "#{column} IS NOT NULL"
61
50
  end
62
51
  end
@@ -98,7 +87,7 @@ module Rao
98
87
  end
99
88
 
100
89
  def operator_map
101
- OPERATOR_MAP
90
+ Rao::Query::Operators::MAP
102
91
  end
103
92
 
104
93
  def column_is_boolean?(table_name, column_name)
@@ -126,7 +115,7 @@ module Rao
126
115
  end
127
116
 
128
117
  def to_boolean(string)
129
- result = case
118
+ case
130
119
  when Rails.version < '4.2'
131
120
  ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(string)
132
121
  when Rails.version < '5.0'
@@ -134,7 +123,6 @@ module Rao
134
123
  else
135
124
  ::ActiveRecord::Type::Boolean.new.cast(string)
136
125
  end
137
- result.gsub('"', '')
138
126
  end
139
127
 
140
128
  def normalized_condition(table, column, condition)
@@ -2,8 +2,13 @@ de:
2
2
  rao:
3
3
  query:
4
4
  form_builder:
5
+ boolean_label: "%{attribute_name}?"
5
6
  predicates:
6
7
  cont: "%{attribute_name} enthält"
7
8
  eq: "%{attribute_name} ist"
9
+ 'null': "%{attribute_name} ist leer"
10
+ not_null: "%{attribute_name} ist nicht leer"
8
11
  reset: "Zurücksetzen"
9
- submit: "Suchen"
12
+ submit: "Suchen"
13
+ 'yes': Ja
14
+ 'no': nein
@@ -2,8 +2,13 @@ en:
2
2
  rao:
3
3
  query:
4
4
  form_builder:
5
+ boolean_label: "%{attribute_name}?"
5
6
  predicates:
6
7
  cont: "%{attribute_name} contains"
7
8
  eq: "%{attribute_name} equals"
9
+ 'null': "%{attribute_name} is present"
10
+ not_null: "%{attribute_name} is not present"
8
11
  reset: "Reset"
9
- submit: "Search"
12
+ submit: "Search"
13
+ 'yes': "Yes"
14
+ 'no': "No"
data/lib/rao/query.rb CHANGED
@@ -3,6 +3,7 @@ require "rao-view_helper"
3
3
  require "rao/query/version"
4
4
  require "rao/query/configuration"
5
5
  require "rao/query/engine"
6
+ require "rao/query/operators"
6
7
 
7
8
  module Rao
8
9
  module Query
@@ -0,0 +1,31 @@
1
+ module Rao
2
+ module Query
3
+ module Operators
4
+ # The not operators have to go first. Else the matching will detect _null
5
+ # or _eq before _not_null and _not_eq.
6
+ MAP = {
7
+ not_eq: :'<>',
8
+ not_null: :is_not_null,
9
+
10
+ gt: :>,
11
+ gt_or_eq: :>=,
12
+ eq: :'=',
13
+ lt_or_eq: :<=,
14
+ lt: :<,
15
+ null: :is_null,
16
+ cont: :like
17
+ }
18
+
19
+ def self.extract_attribute_name_and_predicate_from_name(name)
20
+ MAP.keys.each do |predicate|
21
+ if name.to_s.end_with?(predicate.to_s)
22
+ attribute_name = name[0..-(predicate.length + 2)]
23
+ return attribute_name, predicate
24
+ end
25
+ end
26
+
27
+ raise "Could not extract attribute name and predicate from #{name}"
28
+ end
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rao-query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.32.pre
4
+ version: 0.0.33.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Vasquez Angel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-02 00:00:00.000000000 Z
11
+ date: 2019-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -122,6 +122,62 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-coolline
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rails-dummy
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: bootsnap
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: factory_bot_rails
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
125
181
  description:
126
182
  email:
127
183
  - roberto@vasquez-angel.de
@@ -145,6 +201,7 @@ files:
145
201
  - lib/rao/query.rb
146
202
  - lib/rao/query/configuration.rb
147
203
  - lib/rao/query/engine.rb
204
+ - lib/rao/query/operators.rb
148
205
  - lib/rao/query/version.rb
149
206
  homepage: https://github.com/rao
150
207
  licenses: