query_helper 0.1.6 → 0.1.8

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: 57e18fef49ee5e918c533ad632c3f01a6a6748a751675dc4d917e898072ab391
4
- data.tar.gz: 6125eac1ac5f019ff7e686a19abe78824fe96f3cec110f02abc8f727d847cc18
3
+ metadata.gz: ee722b53cba3c6bbb0657b9102e0f5a46bf15d8f8d4c661b2acd7ff1374e8b9f
4
+ data.tar.gz: c8749dbd254d793f8c864cd7fc85cbf6d38d8738ad8493fc54970c3e169e5915
5
5
  SHA512:
6
- metadata.gz: 4a945428a4dcc38487353461705dd52e9ef5e46acf7f81b5d3a871a3161d588ed36022cf5c313d6451ba837c25b0ea47cc7f90ccee57512034d8cae89da8bdb3
7
- data.tar.gz: 94ffe7976b315a38557ea21ab9faa52e8ad24a61a9f30d2c52520f63c9bd6b1385a7d756a3bc801e9fd8328a136703151ea3868fa4afe995ca51ac5a50ac078d
6
+ metadata.gz: 25116ab59d1f2f3f95a6741f5f17f9864436da4690f89e6ef12d770c16ecf0195eaae20c24b10cdb028e94fbac4bd1ed0317eac8985abba69cf575e02ed975ff
7
+ data.tar.gz: 0e734d5bbbcadfbbb734f9b8986d97eb85e81273901aeae90780523eb08a6aa8b9edbb10b930fb3623ee12d653b8185e4e034e89d458c33ce632ae1dc144609f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- query_helper (0.1.6)
4
+ query_helper (0.1.8)
5
5
  activerecord (~> 5.0)
6
6
  activesupport (~> 5.0)
7
7
 
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, :query, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload
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
- @model = model
34
- @query = query
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
- @model = model if model
69
- @query = query if query
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 build_query
85
- # Correctly set the query and model based on query type
86
- determine_query_type()
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
@@ -1,3 +1,3 @@
1
1
  class QueryHelper
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: query_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan McDaniel