lib/query_helper.rb in query_helper-0.1.1 vs lib/query_helper.rb in query_helper-0.1.2
- old
+ new
@@ -11,11 +11,11 @@
require "query_helper/sql_sort"
require "query_helper/invalid_query_error"
class QueryHelper
- attr_accessor :model, :query, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload
+ attr_accessor :model, :query, :bind_variables, :sql_filter, :sql_sort, :page, :per_page, :single_record, :associations, :as_json_options, :executed_query, :api_payload, :preload
def initialize(
model: nil, # the model to run the query against
query: nil, # a sql string or an active record query
bind_variables: {}, # a list of bind variables to be embedded into the query
@@ -25,11 +25,12 @@
per_page: nil, # define how many results you want per page
single_record: false, # whether or not you expect the record to return a single result, if toggled, only the first result will be returned
associations: [], # a list of activerecord associations you'd like included in the payload
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
+ 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
)
@model = model
@query = query
@bind_variables = bind_variables
@sql_filter = sql_filter
@@ -39,10 +40,11 @@
@single_record = single_record
@associations = associations
@as_json_options = as_json_options
@custom_mappings = custom_mappings
@api_payload = api_payload
+ @preload = preload
if @page && @per_page
# Determine limit and offset
limit = @per_page
offset = (@page - 1) * @per_page
@@ -58,20 +60,22 @@
bind_variables: {},
filters: [],
associations: [],
as_json_options: nil,
single_record: nil,
- custom_mappings: nil
+ custom_mappings: nil,
+ preload: []
)
@model = model if model
@query = query if query
@bind_variables.merge!(bind_variables)
filters.each{ |f| add_filter(**f) }
@associations = @associations | associations
@single_record = single_record if single_record
@as_json_options = as_json_options if as_json_options
@custom_mappings = custom_mappings if custom_mappings
+ @preload = preload if preload
end
def add_filter(operator_code:, criterion:, comparate:)
@sql_filter.filter_values["comparate"] = { operator_code => criterion }
end
@@ -104,22 +108,21 @@
@executed_query = manipulator.build()
@results = @model.find_by_sql([@executed_query, @bind_variables]) # Execute Sql Query
@results = @results.first if @single_record # Return a single result if requested
determine_count()
+ preload_associations()
load_associations()
clean_results()
end
def results()
execute_query()
return paginated_results() if @api_payload
return @results
end
-
-
private
def paginated_results
{ pagination: pagination_results(),
data: @results }
@@ -149,10 +152,17 @@
associations: @associations,
as_json_options: @as_json_options
)
end
+ def preload_associations
+ Associations.preload_associations(
+ payload: @results,
+ preload: @preload
+ )
+ end
+
def clean_results
@results.map!{ |r| r.except("_query_full_count") } if @page && @per_page
end
def pagination_results
@@ -183,7 +193,6 @@
query: @query,
custom_mappings: @custom_mappings,
model: @model
)
end
-
end