ff-tbl-macros 1.0.2 → 2.0.0
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +5 -3
- data/lib/macros/search/query.rb +20 -7
- data/lib/macros/version.rb +1 -1
- data/spec/lib/search/query_spec.rb +42 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 683c3f02309ce122e5841382c567d6033af6174969c8cbd5690abbc058e0ab9e
|
4
|
+
data.tar.gz: 74939c7eee9dd5410d4bba6a9fce3d5762b306ab1a171866d495a74bbcaabfaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5dc808dc25712ecd9fadbdff7f0f08a897955ebab9ddbf3f499aac54160f56ae2d657253b305c51c89d05ac755af040505af16cb41f0ebcbe85292b2fb282a7
|
7
|
+
data.tar.gz: eaba1884dd96f8038cdd75bb8c374d52bf236fbebc00ea58bbc95266be96d71d42e77109e23db07e224d6262a4112c517cf81c07a9239340af30252172679e64
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ff-tbl-macros (
|
4
|
+
ff-tbl-macros (2.0.0)
|
5
5
|
activesupport (>= 5.2)
|
6
6
|
require_all (~> 2.0)
|
7
7
|
trailblazer (>= 2.0)
|
@@ -35,8 +35,9 @@ GEM
|
|
35
35
|
jaro_winkler (1.5.2)
|
36
36
|
json (2.2.0)
|
37
37
|
minitest (5.11.3)
|
38
|
+
pagy (2.1.5)
|
38
39
|
parallel (1.17.0)
|
39
|
-
parser (2.6.2.
|
40
|
+
parser (2.6.2.1)
|
40
41
|
ast (~> 2.4.0)
|
41
42
|
pipetree (0.1.1)
|
42
43
|
psych (3.1.0)
|
@@ -61,7 +62,7 @@ GEM
|
|
61
62
|
rspec-mocks (>= 2.99, < 4.0)
|
62
63
|
rspec-core (3.8.0)
|
63
64
|
rspec-support (~> 3.8.0)
|
64
|
-
rspec-expectations (3.8.
|
65
|
+
rspec-expectations (3.8.3)
|
65
66
|
diff-lcs (>= 1.2.0, < 2.0)
|
66
67
|
rspec-support (~> 3.8.0)
|
67
68
|
rspec-mocks (3.8.0)
|
@@ -103,6 +104,7 @@ PLATFORMS
|
|
103
104
|
|
104
105
|
DEPENDENCIES
|
105
106
|
ff-tbl-macros!
|
107
|
+
pagy
|
106
108
|
recursive-open-struct
|
107
109
|
reform
|
108
110
|
require_all
|
data/lib/macros/search/query.rb
CHANGED
@@ -1,14 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'pagy'
|
4
|
+
|
3
5
|
module Macros
|
4
6
|
class Search
|
5
7
|
class Query < Macros::Base
|
8
|
+
include Pagy::Backend
|
9
|
+
|
6
10
|
# @return [Macros::Search::Results] step macro instance
|
7
11
|
#
|
8
|
-
# @example searchable is optional
|
12
|
+
# @example searchable is optional, paginate is true by default
|
9
13
|
# Macros::Search::Query(searchable: Admin)
|
10
|
-
def initialize(searchable:)
|
14
|
+
def initialize(searchable:, paginate: true)
|
11
15
|
@searchable = searchable
|
16
|
+
@paginate = paginate
|
12
17
|
end
|
13
18
|
|
14
19
|
# @param ctx [Trailblazer::Skill] tbl context hash
|
@@ -22,12 +27,20 @@ module Macros
|
|
22
27
|
return false unless @searchable
|
23
28
|
|
24
29
|
ctx[:searchable] = @searchable
|
25
|
-
ransack_search_result = @searchable.ransack params[:q]
|
26
|
-
ctx[:query] = ransack_search_result
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
ransack_search = @searchable.ransack params[:q]
|
32
|
+
ctx[:query] = ransack_search
|
33
|
+
|
34
|
+
temp_search_results = ransack_search.result
|
35
|
+
|
36
|
+
if @paginate
|
37
|
+
page = params[:page] || 1
|
38
|
+
pagy, records = pagy(temp_search_results, page: page)
|
39
|
+
ctx[:pages] = pagy
|
40
|
+
temp_search_results = records
|
41
|
+
end
|
42
|
+
|
43
|
+
ctx[:search_results] = order ? temp_search_results.order(order) : temp_search_results
|
31
44
|
end
|
32
45
|
end
|
33
46
|
end
|
data/lib/macros/version.rb
CHANGED
@@ -1,23 +1,53 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
RSpec.describe Macros::Search::Query do
|
4
|
-
subject(:query_step) { described_class.new(
|
4
|
+
subject(:query_step) { described_class.new(macro_params) }
|
5
|
+
let(:macro_params) { { searchable: klass, paginate: false } }
|
6
|
+
let(:params) { { q: query, page: page } }
|
7
|
+
let(:ctx) { { params: params } }
|
8
|
+
let(:query) { { name_cont: 'foo' } }
|
5
9
|
|
6
|
-
let(:page) { 1 }
|
7
10
|
let(:result) { double('result', page: page) }
|
8
|
-
let(:
|
9
|
-
let(:klass) { mock_model('User', ransack:
|
11
|
+
let(:ransack_search) { double('Ransack::Search', result: [user1, user2]) }
|
12
|
+
let(:klass) { mock_model('User', ransack: ransack_search) }
|
10
13
|
|
11
|
-
let(:
|
12
|
-
let(:
|
13
|
-
let(:
|
14
|
+
let(:user1) { double('User') }
|
15
|
+
let(:user2) { double('User') }
|
16
|
+
let(:records) { [user1, user2] }
|
17
|
+
|
18
|
+
describe 'search without paginate' do
|
19
|
+
let(:paginate) { false }
|
20
|
+
let(:page) { nil }
|
21
|
+
|
22
|
+
it 'returns records without pagy' do
|
23
|
+
expect(klass).to receive(:ransack).with(query).and_return(ransack_search)
|
24
|
+
expect(ransack_search).to receive(:result).and_return(records)
|
25
|
+
expect(subject).not_to receive(:pagy)
|
26
|
+
|
27
|
+
query_step.call(ctx, params: params)
|
28
|
+
|
29
|
+
expect(ctx[:search_results]).to eql records
|
30
|
+
expect(ctx[:pages]).to be nil
|
31
|
+
expect(ctx[:query]).to eql ransack_search
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'search with paginate' do
|
36
|
+
let(:macro_params) { { searchable: klass, paginate: true } }
|
37
|
+
let(:paginate) { true }
|
38
|
+
let(:page) { 1 }
|
39
|
+
let(:pagy) { double('pagy') }
|
14
40
|
|
15
|
-
|
16
|
-
|
41
|
+
it 'returns records with pagy' do
|
42
|
+
expect(klass).to receive(:ransack).with(query).and_return(ransack_search)
|
43
|
+
expect(ransack_search).to receive(:result).and_return(records)
|
44
|
+
expect(subject).to receive(:pagy).with(records, page: 1).and_return([pagy, records])
|
17
45
|
|
18
|
-
|
46
|
+
query_step.call(ctx, params: params)
|
19
47
|
|
20
|
-
|
21
|
-
|
48
|
+
expect(ctx[:search_results]).to eql records
|
49
|
+
expect(ctx[:pages]).to eql pagy
|
50
|
+
expect(ctx[:query]).to eql ransack_search
|
51
|
+
end
|
22
52
|
end
|
23
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ff-tbl-macros
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Szwed, Claudio Perez Gamayo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|