wallaby-active_record 0.1.1 → 0.2.1

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: 92d10c82a2195970de2aa77c1ad8cd776c03fe68c264678e962bab91a01e7e0d
4
- data.tar.gz: 629a2839e2e558ca281f2a48777fbfa20a0a7a77a7497ca8b2a185a5d731d8ff
3
+ metadata.gz: 7d10a3ddeced877f4c004c1d2a5141b6463865a22d673ae91a9ca87a22a3523c
4
+ data.tar.gz: c194bdfef64070a23a5bc0ebdf7d0bdbc327047eb330a459e81b616fd5ab6f84
5
5
  SHA512:
6
- metadata.gz: 71820d2020d94d02e2a02063c70faa630a58ae3e2f6ca0422f043a9f3042cf4eeedf336fe8d8f4fcb37f3c0471715427a34329dfacacd27444deb45aef57d04f
7
- data.tar.gz: 392802282ef7e87b47c7018544f1324fea3d6750304d88e0d28b1ccf7a27d869cbd68dfc4ed58d03610f9a48b80b10f0a55f37bfdbb3af82ea7234997d71930c
6
+ metadata.gz: '0695f8352480c7597eae7e5049e8d009ae3f30afa4400c233bd4f036a42abab71780cfe7b62f5c362f21b0a31384dee564655de5fbd76628fd282714eb6f2b8d'
7
+ data.tar.gz: 3c34d83727141ee696e9f839f78ac209fabec0edd1437265f013f23f0ff6531dcba8aa762038cc4487c11a3b599ead2520f0f0b3e46e80b3e0184febf9cd267a
data/README.md CHANGED
@@ -1,31 +1,37 @@
1
- # Wallaby::ActiveRecord
1
+ # [Wallaby::ActiveRecord](https://github.com/wallaby-rails/wallaby-active_record)
2
2
 
3
- This gem contains the ActiveRecord ORM adapter for Wallaby.
3
+ [![Gem Version](https://badge.fury.io/rb/wallaby-active_record.svg)](https://badge.fury.io/rb/wallaby-active_record)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![Travis CI](https://travis-ci.com/wallaby-rails/wallaby-active_record.svg?branch=master)](https://travis-ci.com/wallaby-rails/wallaby-active_record)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/9ba0a610043a2e1a9e74/maintainability)](https://codeclimate.com/github/wallaby-rails/wallaby-active_record/maintainability)
7
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/9ba0a610043a2e1a9e74/test_coverage)](https://codeclimate.com/github/wallaby-rails/wallaby-active_record/test_coverage)
8
+ [![Inch CI](https://inch-ci.org/github/wallaby-rails/wallaby-active_record.svg?branch=master)](https://inch-ci.org/github/wallaby-rails/wallaby-active_record)
4
9
 
5
- ## Installation
10
+ Wallaby::ActiveRecord is the ActiveRecord adapter for [Wallaby::Core](https://github.com/wallaby-rails/wallaby-core).
6
11
 
7
- Add this line to your application's Gemfile:
12
+ ## Install
13
+
14
+ Add `Wallaby::ActiveRecord` to `Gemfile`.
8
15
 
9
16
  ```ruby
10
17
  gem 'wallaby-active_record'
11
18
  ```
12
19
 
13
- And then execute:
20
+ And re-bundle.
14
21
 
15
22
  ```shell
16
- $ bundle
23
+ bundle install
17
24
  ```
18
25
 
19
- Or install it yourself as:
26
+ ## Documentation
20
27
 
21
- ```shell
22
- $ gem install wallaby-active_record
23
- ```
28
+ - [API Reference](https://www.rubydoc.info/gems/wallaby-active_record)
29
+ - [Change Logs](https://github.com/wallaby-rails/wallaby-active_record/blob/master/CHANGELOG.md)
24
30
 
25
- ## Contributing
31
+ ## Want to contribute?
26
32
 
27
- Bug reports and pull requests are welcome on GitHub at https://github.com/wallaby-rails/wallaby-active_record.
33
+ Raise an [issue](https://github.com/wallaby-rails/wallaby-active_record/issues/new), discuss and resolve!
28
34
 
29
35
  ## License
30
36
 
31
- This project is [MIT Licensed](LICENSE)
37
+ This project uses [MIT License](https://github.com/wallaby-rails/wallaby-active_record/blob/master/LICENSE).
@@ -115,7 +115,7 @@ module Wallaby
115
115
  # @param resource [Object]
116
116
  # @return [String] the title of given resource
117
117
  def guess_title(resource)
118
- ModuleUtils.try_to resource, title_field_finder.find
118
+ resource.try title_field_finder.find
119
119
  end
120
120
 
121
121
  protected
@@ -41,7 +41,7 @@ module Wallaby
41
41
  # @param klass [Class]
42
42
  # @return [Boolean] whether the class is ActiveRecord base class
43
43
  def top_parent?(klass)
44
- klass == ModelFinder.base || ModuleUtils.try_to(klass, :abstract_class?)
44
+ klass == ModelFinder.base || klass.try(:abstract_class?)
45
45
  end
46
46
  end
47
47
  end
@@ -2,30 +2,40 @@
2
2
 
3
3
  module Wallaby
4
4
  class ActiveRecord
5
- # Model pagination provider
5
+ # Model pagination provider for {Wallaby::ActiveRecord}
6
6
  class ModelPaginationProvider < ::Wallaby::ModelPaginationProvider
7
- # Check if collection has pagination feature
8
- # @return [Boolean]
7
+ # Check if collection can be paginated
8
+ # @return [true] if paginatable
9
+ # @return [false] if not paginatable
9
10
  def paginatable?
10
- # `total_count` is a method that kaminari uses
11
- (@collection && @collection.respond_to?(:total_count)).tap do |paginatable|
12
- next if paginatable
13
-
14
- Rails.logger.warn I18n.t('errors.activerecord.paginatable', collection: @collection.inspect)
11
+ paginatable =
12
+ # kaminari
13
+ @collection.respond_to?(:total_count) || \
14
+ @collection.respond_to?(:total_entries) # will_paginate
15
+ unless paginatable
16
+ Rails.logger.warn I18n.t(
17
+ 'errors.activerecord.paginatable', collection: @collection.inspect
18
+ )
15
19
  end
20
+
21
+ paginatable
16
22
  end
17
23
 
18
- # @return [Integer] total count for the query
24
+ # @return [Integer] total count for the collection
19
25
  def total
20
- @collection.total_count
26
+ # kaminari
27
+ @collection.try(:total_count) || \
28
+ @collection.try(:total_entries) # will_paginate
21
29
  end
22
30
 
23
- # @return [Integer] page size from parameters or configuration
31
+ # @return [Integer] page size from parameters or
32
+ # {https://rubydoc.info/gems/wallaby-core/Wallaby/Configuration/Pagination#page_size-instance_method page_size}
33
+ # Wallaby configuration
24
34
  def page_size
25
- @params[:per].try(:to_i) || Wallaby.configuration.pagination.page_size
35
+ (@params[:per] || Wallaby.configuration.pagination.page_size).to_i
26
36
  end
27
37
 
28
- # @return [Integer] page number from parameters
38
+ # @return [Integer] page number from parameters starting from 1
29
39
  def page_number
30
40
  [@params[:page].to_i, 1].max
31
41
  end
@@ -2,8 +2,7 @@
2
2
 
3
3
  module Wallaby
4
4
  class ActiveRecord
5
- # Model service provider
6
- # @see Wallaby::ModelServiceProvider
5
+ # Model service provider for {Wallaby::ActiveRecord}
7
6
  class ModelServiceProvider < ::Wallaby::ModelServiceProvider
8
7
  # @param params [ActionController::Parameters]
9
8
  # @param action [String, Symbol]
@@ -31,9 +30,11 @@ module Wallaby
31
30
  # @return [ActiveRecord::Relation] paginated query
32
31
  # @see Wallaby::ModelServiceProvider#paginate
33
32
  def paginate(query, params)
34
- per = params[:per] || Wallaby.configuration.pagination.page_size
33
+ # NOTE: do not take out the `.to_i` as will_paginate requires an integer `per_page`
34
+ per = (params[:per] || Wallaby.configuration.pagination.page_size).to_i
35
35
  query = query.page params[:page] if query.respond_to? :page
36
- query = query.per per if query.respond_to? :per
36
+ query = query.per per if query.respond_to? :per # kaminari
37
+ query = query.per_page per if query.respond_to? :per_page # will_paginate
37
38
  query
38
39
  end
39
40
 
@@ -106,7 +106,7 @@ module Wallaby
106
106
  text_fields.each do |field_name|
107
107
  sub_query = nil
108
108
  keywords.each do |keyword|
109
- exp = table[field_name].matches("%#{keyword}%")
109
+ exp = table[field_name].matches(Escaper.execute(keyword))
110
110
  sub_query = sub_query.try(:and, exp) || exp
111
111
  end
112
112
  query = query.try(:or, sub_query) || sub_query
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wallaby
4
+ class ActiveRecord
5
+ class ModelServiceProvider
6
+ class Querier
7
+ # Build up query using the results
8
+ class Escaper
9
+ include ::ActiveRecord::Sanitization
10
+ LIKE_SIGN = /[%_]/.freeze # :nodoc:
11
+ PCT = '%' # :nodoc:
12
+
13
+ class << self
14
+ def execute(keyword)
15
+ first = keyword.first
16
+ last = keyword.last
17
+ start_with, start_index = LIKE_SIGN.match?(first) ? [true, 1] : [false, 0]
18
+ end_with, end_index = LIKE_SIGN.match?(last) ? [true, -2] : [false, -1]
19
+ escaped = sanitize_sql_like keyword[start_index..end_index]
20
+ starting = sign(start_with, first, end_with)
21
+ ending = sign(end_with, last, start_with)
22
+
23
+ "#{starting}#{escaped}#{ending}"
24
+ end
25
+
26
+ def sign(
27
+ first_condition, first_char, second_condition, default_sign = PCT
28
+ )
29
+ return first_char if first_condition
30
+ return if second_condition
31
+
32
+ default_sign
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -6,7 +6,7 @@ module Wallaby
6
6
  class Querier
7
7
  # Build up query using the results
8
8
  class Transformer < Parslet::Transform
9
- SIMPLE_OPERATORS = {
9
+ SIMPLE_OPERATORS = { # :nodoc:
10
10
  ':' => :eq,
11
11
  ':=' => :eq,
12
12
  ':!' => :not_eq,
@@ -24,7 +24,7 @@ module Wallaby
24
24
  ':<=' => :lteq
25
25
  }.freeze
26
26
 
27
- SEQUENCE_OPERATORS = {
27
+ SEQUENCE_OPERATORS = { # :nodoc:
28
28
  ':' => :in,
29
29
  ':=' => :in,
30
30
  ':!' => :not_in,
@@ -34,15 +34,18 @@ module Wallaby
34
34
  ':!()' => :not_between
35
35
  }.freeze
36
36
 
37
- # For single keyword
38
- rule keyword: simple(:value) do
39
- value.try :to_str
40
- end
37
+ # For single null
38
+ rule null: simple(:value)
39
+ rule null: sequence(:value)
41
40
 
42
- # For multiple keywords
43
- rule keyword: sequence(:value) do
44
- value.presence.try :map, :to_str
45
- end
41
+ # For single boolean
42
+ rule(boolean: simple(:value)) { /true/i.match? value }
43
+
44
+ # For single string
45
+ rule(string: simple(:value)) { value.try :to_str }
46
+
47
+ # For multiple strings
48
+ rule(string: sequence(:value)) { EMPTY_STRING }
46
49
 
47
50
  # For operators
48
51
  rule left: simple(:left), op: simple(:op), right: simple(:right) do
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'kaminari'
4
3
  require 'wallaby/core'
5
4
 
6
5
  require 'wallaby/active_record/version'
@@ -27,6 +26,7 @@ require 'adapters/wallaby/active_record/model_service_provider'
27
26
  require 'adapters/wallaby/active_record/model_service_provider/normalizer'
28
27
  require 'adapters/wallaby/active_record/model_service_provider/permitter'
29
28
  require 'adapters/wallaby/active_record/model_service_provider/querier'
29
+ require 'adapters/wallaby/active_record/model_service_provider/querier/escaper'
30
30
  require 'adapters/wallaby/active_record/model_service_provider/querier/transformer'
31
31
  require 'adapters/wallaby/active_record/model_service_provider/validator'
32
32
  # ModelServiceProvider: end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Wallaby
4
4
  module ActiveRecordGem
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.1' # :nodoc:
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wallaby-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tian Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-10 00:00:00.000000000 Z
11
+ date: 2020-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: kaminari
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 4.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 4.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: wallaby-core
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.2.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: cancancan
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -118,6 +118,7 @@ files:
118
118
  - lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb
119
119
  - lib/adapters/wallaby/active_record/model_service_provider/permitter.rb
120
120
  - lib/adapters/wallaby/active_record/model_service_provider/querier.rb
121
+ - lib/adapters/wallaby/active_record/model_service_provider/querier/escaper.rb
121
122
  - lib/adapters/wallaby/active_record/model_service_provider/querier/transformer.rb
122
123
  - lib/adapters/wallaby/active_record/model_service_provider/validator.rb
123
124
  - lib/adapters/wallaby/active_record/pundit_provider.rb
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
146
  - !ruby/object:Gem::Version
146
147
  version: '0'
147
148
  requirements: []
148
- rubygems_version: 3.0.6
149
+ rubygems_version: 3.0.3
149
150
  signing_key:
150
151
  specification_version: 4
151
152
  summary: Wallaby's ActiveRecord ORM adapter