lib/query_helper.rb in query_helper-0.2.11 vs lib/query_helper.rb in query_helper-0.2.12
- old
+ new
@@ -11,11 +11,11 @@
require "query_helper/sql_sort"
require "query_helper/invalid_query_error"
class QueryHelper
- attr_accessor :model, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload, :search_field, :search_string
+ attr_accessor :model, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload, :search_field, :search_string, :metadata
attr_reader :query
def initialize(
model: nil, # the model to run the query against
query: nil, # a sql string or an active record query
@@ -29,11 +29,12 @@
as_json_options: nil, # a list of as_json options you'd like run before returning the payload
custom_mappings: {}, # custom keyword => sql_expression mappings
api_payload: false, # Return the paginated payload or simply return the result array
preload: [], # preload activerecord associations - used instead of `associations` when you don't want them included in the payload
search_fields: [],
- search_string: nil
+ search_string: nil,
+ metadata: {}
)
@query = query.class < ActiveRecord::Relation ? query.to_sql : query
@model = query.class < ActiveRecord::Relation ? query.base_class : model
@bind_variables = bind_variables
@sql_filter = sql_filter
@@ -47,10 +48,11 @@
@custom_mappings = custom_mappings
@api_payload = api_payload
@preload = preload
@search_fields = search_fields
@search_string = search_string
+ @metadata = metadata
end
def update(
query: nil,
model: nil,
@@ -64,11 +66,12 @@
search_fields: nil,
sql_filter: nil,
sql_sort: nil,
page: nil,
per_page: nil,
- search_string: nil
+ search_string: nil,
+ metadata: nil
)
@query = query.class < ActiveRecord::Relation ? query.to_sql : query if query
@model = query.class < ActiveRecord::Relation ? query.base_class : model if model || query
@bind_variables.merge!(bind_variables)
filters.each{ |f| add_filter(**f) }
@@ -81,10 +84,11 @@
@sql_filter = sql_filter if sql_filter
@sql_sort = sql_sort if sql_sort
@search_string = search_string if search_string
@page = determine_page(page: page, per_page: per_page) if page
@per_page = determine_per_page(page: page, per_page: per_page) if per_page
+ @metadata = metadata if metadata
set_limit_and_offset()
return self
end
def add_filter(operator_code:, criterion:, comparate:)
@@ -201,10 +205,11 @@
end
end
def paginated_results
{ pagination: pagination_results(),
- data: @results }
+ data: @results,
+ metadata: @metadata }
end
def determine_count
# Determine total result count (unpaginated)
if @single_record