query_helper 0.1.6 → 0.1.8
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/Gemfile.lock +1 -1
- data/lib/query_helper.rb +16 -21
- data/lib/query_helper/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee722b53cba3c6bbb0657b9102e0f5a46bf15d8f8d4c661b2acd7ff1374e8b9f
|
4
|
+
data.tar.gz: c8749dbd254d793f8c864cd7fc85cbf6d38d8738ad8493fc54970c3e169e5915
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25116ab59d1f2f3f95a6741f5f17f9864436da4690f89e6ef12d770c16ecf0195eaae20c24b10cdb028e94fbac4bd1ed0317eac8985abba69cf575e02ed975ff
|
7
|
+
data.tar.gz: 0e734d5bbbcadfbbb734f9b8986d97eb85e81273901aeae90780523eb08a6aa8b9edbb10b930fb3623ee12d653b8185e4e034e89d458c33ce632ae1dc144609f
|
data/Gemfile.lock
CHANGED
data/lib/query_helper.rb
CHANGED
@@ -13,7 +13,8 @@ require "query_helper/invalid_query_error"
|
|
13
13
|
|
14
14
|
class QueryHelper
|
15
15
|
|
16
|
-
attr_accessor :model, :
|
16
|
+
attr_accessor :model, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload
|
17
|
+
attr_reader :query
|
17
18
|
|
18
19
|
def initialize(
|
19
20
|
model: nil, # the model to run the query against
|
@@ -30,8 +31,8 @@ class QueryHelper
|
|
30
31
|
api_payload: false, # Return the paginated payload or simply return the result array
|
31
32
|
preload: [] # preload activerecord associations - used instead of `associations` when you don't want them included in the payload
|
32
33
|
)
|
33
|
-
@
|
34
|
-
@
|
34
|
+
@query = query.class < ActiveRecord::Relation ? query.to_sql : query
|
35
|
+
@model = query.class < ActiveRecord::Relation ? query.model : model
|
35
36
|
@bind_variables = bind_variables
|
36
37
|
@sql_filter = sql_filter
|
37
38
|
@sql_sort = sql_sort
|
@@ -65,8 +66,8 @@ class QueryHelper
|
|
65
66
|
custom_mappings: nil,
|
66
67
|
preload: []
|
67
68
|
)
|
68
|
-
@
|
69
|
-
@
|
69
|
+
@query = query.class < ActiveRecord::Relation ? query.to_sql : query if query
|
70
|
+
@model = query.class < ActiveRecord::Relation ? query.model : model if model || query
|
70
71
|
@bind_variables.merge!(bind_variables)
|
71
72
|
filters.each{ |f| add_filter(**f) }
|
72
73
|
@associations = @associations | associations
|
@@ -81,10 +82,17 @@ class QueryHelper
|
|
81
82
|
@sql_filter.filter_values["comparate"] = { operator_code => criterion }
|
82
83
|
end
|
83
84
|
|
84
|
-
def
|
85
|
-
|
86
|
-
|
85
|
+
def query=(value)
|
86
|
+
if value.class < ActiveRecord::Relation
|
87
|
+
@query = value.to_sql
|
88
|
+
@model = value.model
|
89
|
+
else
|
90
|
+
@query = value
|
91
|
+
end
|
92
|
+
return ""
|
93
|
+
end
|
87
94
|
|
95
|
+
def build_query
|
88
96
|
# Create column maps to be used by the filter and sort objects
|
89
97
|
column_maps = create_column_maps()
|
90
98
|
|
@@ -146,19 +154,6 @@ class QueryHelper
|
|
146
154
|
data: @results }
|
147
155
|
end
|
148
156
|
|
149
|
-
def determine_query_type
|
150
|
-
# If a custom sql string is passed in, make sure a valid model is passed in as well
|
151
|
-
if @query.class == String
|
152
|
-
raise InvalidQueryError.new("a valid model must be included to run a custom SQL query") unless @model < ActiveRecord::Base
|
153
|
-
# If an active record query is passed in, find the model and sql from the query
|
154
|
-
elsif @query.class < ActiveRecord::Relation
|
155
|
-
@model ||= @query.model
|
156
|
-
@query = @query.to_sql
|
157
|
-
else
|
158
|
-
raise InvalidQueryError.new("unable to determine query type")
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
157
|
def determine_count
|
163
158
|
# Determine total result count (unpaginated)
|
164
159
|
if @single_record
|
data/lib/query_helper/version.rb
CHANGED