queryko 1.2.4 → 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.
@@ -1,122 +0,0 @@
1
- require "active_support/core_ext/class/attribute"
2
- require "queryko/range_attributes"
3
- require "queryko/searchables"
4
- require "queryko/after_attributes"
5
- require "queryko/naming"
6
- require "queryko/able"
7
- require "queryko/filterer"
8
- require "queryko/filter_classes"
9
-
10
- module Queryko
11
- class QueryObject
12
- attr_reader :countable_resource
13
- include Queryko::FilterClasses
14
- include Queryko::Naming
15
- include Queryko::RangeAttributes
16
- include Queryko::Searchables
17
- include Queryko::Able
18
- include Queryko::Filterer
19
- # include AfterAttributes
20
-
21
- def initialize(params = {}, rel = nil)
22
- @relation = @original_relation = rel || self.class.model_class.all
23
- @params = params
24
- end
25
-
26
- def call
27
- perform
28
- self.relation = paginate if config[:paginate]
29
- return relation
30
- end
31
-
32
- def perform
33
- return if @performed
34
-
35
- @performed = true
36
- pre_filter
37
- filter
38
- filter_by_filters
39
- @countable_resource = relation
40
- end
41
-
42
-
43
- def total_count
44
- perform
45
- countable_resource.count
46
- end
47
-
48
- def count
49
- call.to_a.count
50
- end
51
-
52
- private
53
-
54
- attr_reader :params, :relation
55
- attr_writer :relation
56
-
57
- def config
58
- @config ||= {
59
- paginate: true,
60
- since_id: true,
61
- ids: true
62
- }
63
- end
64
-
65
- def pre_filter
66
- self.relation = by_ids if config[:ids] && params[:ids]
67
- self.relation = since_id if config[:since_id] && params[:since_id]
68
- end
69
-
70
- def filter
71
- end
72
-
73
- def paginate
74
- if defined?(WillPaginate)
75
- relation.paginate(page: page, per_page: limit)
76
- elsif defined?(Kaminari)
77
- relation.page(page).per(limit)
78
- else
79
- raise 'Only kaminari and wil_paginate are supported'
80
- end
81
- end
82
-
83
- def page
84
- params[:page] || 1
85
- end
86
-
87
- def limit
88
- @limit ||= get_limit
89
- end
90
-
91
- def get_limit
92
- lim = (params[:limit] || default_limit).to_i
93
- if lower_limit > lim
94
- lower_limit
95
- elsif lim > upper_limit
96
- upper_limit
97
- else
98
- lim
99
- end
100
- end
101
-
102
- def upper_limit
103
- 100
104
- end
105
-
106
- def default_limit
107
- 50
108
- end
109
-
110
- def lower_limit
111
- 10
112
- end
113
-
114
- def by_ids
115
- relation.where(id: params[:ids].split(','))
116
- end
117
-
118
- def since_id
119
- relation.where("#{defined_table_name}.id > ?", params[:since_id])
120
- end
121
- end
122
- end
@@ -1,20 +0,0 @@
1
- module Queryko
2
- module RangeAttributes
3
- def self.included(base)
4
- base.extend(ClassMethods)
5
- end
6
-
7
- module ClassMethods
8
- def add_range_attributes(*args)
9
- suggestion = []
10
- args.each do |arg|
11
- feature arg.to_sym, :min
12
- feature arg.to_sym, :max
13
- suggestion << "feature :#{arg}, :min"
14
- suggestion << "feature :#{arg}, :max"
15
- end
16
- warn "[DEPRECATION] `add_range_attributes` is deprecated. Please use `feature` instead.\nExample:\n#{suggestion.join("\n")}"
17
- end
18
- end
19
- end
20
- end
@@ -1,18 +0,0 @@
1
- module Queryko
2
- module Searchables
3
- def self.included(base)
4
- base.extend(ClassMethods)
5
- end
6
-
7
- module ClassMethods
8
- def add_searchables(*args)
9
- suggestion = []
10
- args.each do |arg|
11
- feature arg.to_sym, :search, as: arg.to_sym
12
- suggestion << "feature :#{arg}, :search, as: :#{arg}"
13
- end
14
- warn "[DEPRECATION] `add_searchables` is deprecated. Please use `feature` instead.\nExample:\n#{suggestion.join("\n")}"
15
- end
16
- end
17
- end
18
- end