wallaby-active_record 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93579d0cc4c2a81356d19acc3a24a7cac7160406f4c09c829de95f10d3db1020
4
- data.tar.gz: d591a516f5c2fc04d7b6f46af992498e093d5d3f706d03c639a77a11eeeba339
3
+ metadata.gz: 77775377884b090f9d0c0679a0eda24caccaece57e0429f8307457d91ebce2a5
4
+ data.tar.gz: 23bdf49c685c9b938cfea121ab3bf44fad2f2bffeba964a4bfcfacd3595368f8
5
5
  SHA512:
6
- metadata.gz: 9197ae20142ea0190ed68314ef6eb5c7ec4915febe072f62f0144bb6f42dddbada38adadf05b83eba0ed9744d2c3bc050f2836b25a46c394ab553decaa4f0dee
7
- data.tar.gz: 0a59dcd809acb177822b3665b576b18eefdb0ef55b4fdcfdc6a3cf9344ce73fa1ddc4cb608c47de4d630b6149d0f1a36569eee8c63253830ddad92aae3c3f514
6
+ metadata.gz: 35d9dcf0387cae97199ad0aa47628bbc4719c1f3b48cccd0e04450b87c5b54c5358e8fe5ab3a9fc960e5fa410990438a4b5d00bb7431ab0d8ade6ea7f2aa26af
7
+ data.tar.gz: 07c885247c19f20b71db0f4f85b07e7cb955e68d4cbdec727057dc55938ecac103e4e42f813584869df932638dd7559f2544372aead0903beb86089f75d18c04
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ class ActiveRecord
5
+ # move this to wallaby-core later
6
+ class Logger
7
+ class << self
8
+ %w(debug warn info).each do |method_id|
9
+ define_method method_id do |message, replacements = {}|
10
+ source = caller(replacements.delete(:sourcing).try(:to_i) || 1, 1).first
11
+ Rails.logger.public_send(
12
+ method_id,
13
+ "#{method_id.upcase}: #{format message, replacements}\nfrom #{source}"
14
+ )
15
+ nil
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -12,7 +12,7 @@ module Wallaby
12
12
  # kaminari
13
13
  @collection.respond_to?(:total_count) || \
14
14
  @collection.respond_to?(:total_entries) # will_paginate
15
- Rails.logger.warn "#{@collection.inspect} is not paginatable.\nfrom #{__FILE__}:#{__LINE__}" unless paginatable
15
+ Logger.warn "#{@collection.inspect} is not paginatable." unless paginatable
16
16
 
17
17
  paginatable
18
18
  end
@@ -34,9 +34,21 @@ module Wallaby
34
34
  ':!()' => :not_between
35
35
  }.freeze
36
36
 
37
+ SEQUENCE_JOIN_OPERATORS = { # :nodoc:
38
+ ':' => :or,
39
+ ':=' => :or,
40
+ ':!' => :and,
41
+ ':!=' => :and,
42
+ ':<>' => :and
43
+ }.freeze
44
+
45
+ BETWEEN_OPERATORS = { # :nodoc:
46
+ ':()' => true,
47
+ ':!()' => true
48
+ }.freeze
49
+
37
50
  # For single null
38
51
  rule null: simple(:value)
39
- rule null: sequence(:value)
40
52
 
41
53
  # For single boolean
42
54
  rule(boolean: simple(:value)) { /true/i.match? value }
@@ -51,8 +63,7 @@ module Wallaby
51
63
  rule left: simple(:left), op: simple(:op), right: simple(:right) do
52
64
  oped = op.try :to_str
53
65
  operator = SIMPLE_OPERATORS[oped]
54
- # skip if the operator is unknown
55
- next unless operator
66
+ next Transformer.warn "Unknown operator #{oped} for %<exp>s", instance_values unless operator
56
67
 
57
68
  lefted = left.try :to_str
58
69
  convert =
@@ -68,24 +79,45 @@ module Wallaby
68
79
  rule left: simple(:left), op: simple(:op), right: sequence(:right) do
69
80
  oped = op.try :to_str
70
81
  operator = SEQUENCE_OPERATORS[oped]
71
- next unless operator
82
+ next Transformer.warn "Unknown operator #{oped} for %<exp>s", instance_values unless operator
72
83
 
73
84
  exps = Wrapper.new
74
85
  lefted = left.try :to_str
75
- if right.include? nil
76
- nil_operator = SIMPLE_OPERATORS[oped]
77
- next unless nil_operator
86
+ if BETWEEN_OPERATORS[oped] # BETWEEN related operators
87
+ next Transformer.warn 'Invalid values for %<exp>s', instance_values unless right.first && right.second
78
88
 
79
- exps.push left: lefted, op: nil_operator, right: right.delete(nil)
89
+ convert = Range.new right.first, right.second
90
+ exps.push left: lefted, op: operator, right: convert
91
+ else
92
+ join = SEQUENCE_JOIN_OPERATORS[oped]
93
+ if right.include? nil
94
+ exps.push left: lefted, op: SIMPLE_OPERATORS[oped], right: right.delete(nil), join: join
95
+ end
96
+ exps.push left: lefted, op: operator, right: right, join: join
80
97
  end
81
- convert = Range.new right.try(:first), right.try(:second) if %w(:() :!()).include?(oped)
82
- exps.push left: lefted, op: operator, right: convert || right
83
98
  exps
84
99
  end
85
100
 
86
- def transform(query_string)
87
- result = apply Parser.new.parse(query_string || EMPTY_STRING)
88
- result.is_a?(Array) ? result : [result]
101
+ class << self
102
+ # @param query_string [String]
103
+ # @return [Array]
104
+ def execute(query_string)
105
+ result = new.apply Parser.new.parse(query_string || EMPTY_STRING)
106
+ result.is_a?(Array) ? result : [result]
107
+ end
108
+
109
+ # @param message [String]
110
+ # @param exp [Hash,nil] transformed expression
111
+ # @return [nil]
112
+ def warn(message, exp = nil)
113
+ Logger.warn message, exp: to_origin(exp), sourcing: 2
114
+ end
115
+
116
+ # @param exp [Hash,nil] transformed expression
117
+ # @return [String] origin expression
118
+ def to_origin(exp)
119
+ "'#{exp['left']}#{exp['op']}#{exp['right']}'"
120
+ end
89
121
  end
90
122
  end
91
123
  end
@@ -4,18 +4,17 @@ module Wallaby
4
4
  class ActiveRecord
5
5
  class ModelServiceProvider
6
6
  class Querier
7
- # Build up query using the results
7
+ # Wrapper for the transform result
8
8
  class Wrapper
9
9
  attr_reader :list
10
- delegate :push, :each, to: :list
10
+ delegate :push, to: :list
11
+ delegate :each, to: :list
12
+ delegate :last, to: :list
13
+ delegate :[], to: :last
11
14
 
12
15
  def initialize(list = [])
13
16
  @list = list
14
17
  end
15
-
16
- def [](key)
17
- list.last[key]
18
- end
19
18
  end
20
19
  end
21
20
  end
@@ -36,7 +36,7 @@ module Wallaby
36
36
  # @param params [ActionController::Parameters]
37
37
  # @return [Array<String, Array, Array>] filter_name, keywords, field_queries
38
38
  def extract(params)
39
- expressions = Transformer.new.transform params[:q]
39
+ expressions = Transformer.execute params[:q]
40
40
  keywords = expressions.select { |v| v.is_a? String }
41
41
  field_queries = expressions.select { |v| v.is_a? Wrapper }
42
42
  filter_name = params[:filter]
@@ -149,7 +149,7 @@ module Wallaby
149
149
  query = nil
150
150
  exps.each do |exp|
151
151
  sub = table[exp[:left]].try(exp[:op], exp[:right])
152
- query = query.try(:or, sub) || sub
152
+ query = query.try(exp[:join], sub) || sub
153
153
  end
154
154
  query
155
155
  end
@@ -11,7 +11,7 @@ module Wallaby
11
11
  def accessible_for(_action, scope)
12
12
  Pundit.policy_scope! user, scope
13
13
  rescue Pundit::NotDefinedError
14
- Rails.logger.warn "Cannot find scope policy for #{scope.inspect}.\nfrom #{__FILE__}:#{__LINE__}"
14
+ Logger.warn "Cannot find scope policy for #{scope.inspect}."
15
15
  scope
16
16
  end
17
17
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Wallaby
4
4
  module ActiveRecordGem
5
- VERSION = '0.2.2' # :nodoc:
5
+ VERSION = '0.2.4' # :nodoc:
6
6
  end
7
7
  end
@@ -6,6 +6,7 @@ require 'wallaby/active_record/version'
6
6
 
7
7
  # All files required for ActiveRecord ORM
8
8
  require 'adapters/wallaby/active_record'
9
+ require 'adapters/wallaby/active_record/logger'
9
10
  require 'adapters/wallaby/active_record/model_finder'
10
11
 
11
12
  # ModelPaginationProvider: begin
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wallaby-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tian Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-19 00:00:00.000000000 Z
11
+ date: 2020-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -106,6 +106,7 @@ files:
106
106
  - lib/adapters/wallaby/active_record.rb
107
107
  - lib/adapters/wallaby/active_record/cancancan_provider.rb
108
108
  - lib/adapters/wallaby/active_record/default_provider.rb
109
+ - lib/adapters/wallaby/active_record/logger.rb
109
110
  - lib/adapters/wallaby/active_record/model_decorator.rb
110
111
  - lib/adapters/wallaby/active_record/model_decorator/fields_builder.rb
111
112
  - lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb