lib/query_helper.rb in query_helper-0.0.0 vs lib/query_helper.rb in query_helper-0.1.0
- old
+ new
@@ -22,11 +22,11 @@
sql_filter: SqlFilter.new(), # a SqlFilter object
sql_sort: SqlSort.new(), # a SqlSort object
page: nil, # define the page you want returned
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: nil, # a list of activerecord associations you'd like included in the payload
+ 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
)
@model = model
@@ -50,14 +50,28 @@
# Merge limit/offset variables into bind_variables
@bind_variables.merge!({limit: limit, offset: offset})
end
end
- def update_query(query: nil, model:nil, bind_variables: {})
+ def update(
+ query: nil,
+ model: nil,
+ bind_variables: {},
+ filters: [],
+ associations: [],
+ as_json_options: nil,
+ single_record: nil,
+ custom_mappings: nil
+ )
@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
end
def add_filter(operator_code:, criterion:, comparate:)
@sql_filter.filter_values["comparate"] = { operator_code => criterion }
end
@@ -66,10 +80,11 @@
# Correctly set the query and model based on query type
determine_query_type()
# Create column maps to be used by the filter and sort objects
column_maps = create_column_maps()
+
@sql_filter.column_maps = column_maps
@sql_sort.column_maps = column_maps
# create the filters from the column maps
@sql_filter.create_filters()
@@ -81,10 +96,11 @@
manipulator = SqlManipulator.new(
sql: @query,
where_clauses: @sql_filter.where_clauses,
having_clauses: @sql_filter.having_clauses,
order_by_clauses: @sql_sort.parse_sort_string,
- include_limit_clause: @page && @per_page ? true : false
+ include_limit_clause: @page && @per_page ? true : false,
+ additional_select_clauses: @sql_sort.select_strings
)
@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