active_queryable 0.2.0 → 0.3.0

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: 85318f4379a335f5d2398c1b85ab19ddb63cfbfa6955882c4dcd9e7a24a5168f
4
- data.tar.gz: d9dbb94f9c174af1a34b680152b8add196cf1ddf1a38a895fc0f32370855a985
3
+ metadata.gz: 5bf4db93e62b6ef9584ed59ac624d983debc4a7792dc4debc252fe4958c0e3ac
4
+ data.tar.gz: 38bd3b1e7b4e9138be78e1cfed968b9dc499894c5d6aa5d1710bf5d1aa032d26
5
5
  SHA512:
6
- metadata.gz: f64dda266de6dd21f1c6a21fae9bfba014a72a16f7acf6c560f6feea3771b4663db82b11b97c97fc751379963aa2b0b29b26a6a34339d2f8dade38576ee5868b
7
- data.tar.gz: 89bad17c363651ddbc6c75ca6eeda418d436682c9376a1c9670e81bbc959e491b5a3b0acc1345cf71879ea01ad5ccf623fc8624a00e698c34a8131e1f1730c63
6
+ metadata.gz: fe31f1bacbd161809ede6ad71b12793f1515a4b189b59006b5558ef7d4b9cdd5c43bb905bd2e932d8d087f15e014efce399c7ce13631277265522e9fb3c6a589
7
+ data.tar.gz: 4f0b563933703484579689cee4fbabe5445ad021115c73bc863f76f9db3ee61df511fa29bd9041aabb81ffbfa1020215005a916cf6de0cc6e4905573c209d2f6
@@ -1,3 +1,3 @@
1
1
  module ActiveQueryable
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -13,6 +13,7 @@ module ActiveQueryable
13
13
  class_attribute :_queryable_default_page
14
14
  class_attribute :_queryable_default_per
15
15
  class_attribute :_queryable_filter_keys
16
+ class_attribute :_queryable_expandable_filter_keys
16
17
  end
17
18
 
18
19
  module Initializer
@@ -26,12 +27,17 @@ module ActiveQueryable
26
27
  self._queryable_default_order = options[:order] || { id: :asc }
27
28
  self._queryable_default_page = options[:page] || 1
28
29
  self._queryable_default_per = options[:per] || 25
29
- self._queryable_filter_keys = ((options[:filter] || []) + ['not']).map(&:to_sym)
30
+ self._queryable_filter_keys = (((options[:filter] || [])).map(&:to_sym))
30
31
 
31
32
  scope :query_by, ->(params) { queryable_scope(params) }
32
33
  scope :of_not, ->(ids) { where.not(id: ids) }
33
34
  end
34
35
 
36
+ def expand_queryable(options)
37
+ self._queryable_expandable_filter_keys ||= []
38
+ self._queryable_expandable_filter_keys += (((options[:filter] || [])).map(&:to_sym))
39
+ end
40
+
35
41
  def queryable_scope(params)
36
42
  params = params.to_unsafe_h if params.respond_to? :to_unsafe_h
37
43
  params = params.with_indifferent_access if params.respond_to?(:with_indifferent_access)
@@ -80,10 +86,11 @@ module ActiveQueryable
80
86
  def queryable_validate_filter_params(filter_params)
81
87
  return nil if filter_params.nil?
82
88
 
83
- unpermitted = filter_params.except(*_queryable_filter_keys)
89
+ filters = (((self._queryable_filter_keys || []) | (self._queryable_expandable_filter_keys || [])) + ['not']).map(&:to_sym)
90
+ unpermitted = filter_params.except(*filters)
84
91
  Rails.logger.warn("Unpermitted queryable parameters: #{unpermitted.keys.join(', ')}") if unpermitted.present?
85
92
 
86
- filter_params.slice(*_queryable_filter_keys)
93
+ filter_params.slice(*filters)
87
94
  end
88
95
 
89
96
  def queryable_parse_order_params(params)
@@ -3,8 +3,9 @@ require 'spec_helper'
3
3
  describe 'Filters' do
4
4
  context 'default filters' do
5
5
  it 'applies column name-based filters' do
6
- query = Person.query_by(filter: { name: 'john doe' }, per: 'all')
6
+ query = Person.query_by(filter: { name: 'john doe', email: 'e@mail.com' }, per: 'all')
7
7
  expect(query.to_sql).to include('"people"."name" = \'john doe\'')
8
+ expect(query.to_sql).to include('"people"."email" = \'e@mail.com\'')
8
9
  expect(query).to include(Person.first)
9
10
  end
10
11
 
@@ -5,9 +5,16 @@ ActiveRecord::Base.establish_connection(
5
5
  database: ':memory:'
6
6
  )
7
7
 
8
- class Person < ActiveRecord::Base
8
+ class Common < ActiveRecord::Base
9
+ self.abstract_class = true
10
+
9
11
  as_queryable
10
- queryable order: { name: :asc }, filter: ['name', 'article_title']
12
+ queryable order: { name: :asc }, filter: ['name']
13
+ end
14
+
15
+ class Person < Common
16
+ expand_queryable filter: ['email']
17
+ expand_queryable filter: ['article_title']
11
18
 
12
19
  has_many :articles
13
20
 
@@ -16,6 +23,7 @@ class Person < ActiveRecord::Base
16
23
 
17
24
  end
18
25
 
26
+
19
27
  class Article < ActiveRecord::Base
20
28
  as_queryable
21
29
  queryable order: { title: :asc }, filter: ['title']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_queryable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mònade
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7'
22
+ version: '8'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7'
32
+ version: '8'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: kaminari-activerecord
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -99,21 +99,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
- version: 2.3.0
102
+ version: 2.7.0
103
103
  required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  requirements:
105
105
  - - ">="
106
106
  - !ruby/object:Gem::Version
107
107
  version: '0'
108
108
  requirements: []
109
- rubygems_version: 3.0.3
109
+ rubygems_version: 3.2.33
110
110
  signing_key:
111
111
  specification_version: 4
112
112
  summary: Gem to make easier model's filtering, sorting and pagination
113
113
  test_files:
114
- - spec/spec_helper.rb
114
+ - spec/active_queryable/filters_spec.rb
115
115
  - spec/active_queryable/ordering_spec.rb
116
116
  - spec/active_queryable/pagination_spec.rb
117
- - spec/active_queryable/filters_spec.rb
118
- - spec/support/schema.rb
117
+ - spec/spec_helper.rb
119
118
  - spec/support/match_queries.rb
119
+ - spec/support/schema.rb