lib/blacklight/search_builder.rb in blacklight-5.12.1 vs lib/blacklight/search_builder.rb in blacklight-5.13.0

- old
+ new

@@ -17,39 +17,90 @@ processor_chain end @scope = scope @blacklight_params = {} + @merged_params = {} + @reverse_merged_params = {} end ## # Set the parameters to pass through the processor chain def with blacklight_params = {} + params_will_change! @blacklight_params = blacklight_params.dup self end ## # Update the :q (query) parameter def where conditions + params_will_change! @blacklight_params[:q] = conditions self end ## # Append additional processor chain directives def append *addl_processor_chain - self.class.new(processor_chain + addl_processor_chain, scope).with(blacklight_params) + params_will_change! + builder = self.class.new(processor_chain + addl_processor_chain, scope) + .with(blacklight_params) + .merge(@merged_params) + .reverse_merge(@reverse_merged_params) + + builder.start(@start) if @start + builder.rows(@rows) if @rows + builder.page(@page) if @page + + builder end + ## + # Merge additional, repository-specific parameters + def merge extra_params, &block + if extra_params + params_will_change! + @merged_params.merge!(extra_params.to_hash, &block) + end + self + end + + ## + # "Reverse merge" additional, repository-specific parameters + def reverse_merge extra_params, &block + if extra_params + params_will_change! + @reverse_merged_params.reverse_merge!(extra_params.to_hash, &block) + end + self + end + + delegate :[], :key?, to: :to_hash + # a solr query method # @param [Hash,HashWithIndifferentAccess] extra_controller_params (nil) extra parameters to add to the search # @return [Blacklight::SolrResponse] the solr response object - def query(extra_params = nil) - extra_params ? processed_parameters.merge(extra_params) : processed_parameters + def to_hash method_extra_params = nil + unless method_extra_params.nil? + Deprecation.warn(Blacklight::SearchBuilder, "Calling SearchBuilder#query with extra parameters is deprecated. Use #merge(Hash) instead") + merge(method_extra_params) + end + + if params_need_update? + @params = processed_parameters. + reverse_merge(@reverse_merged_params). + merge(@merged_params). + tap { self.clear_changes } + else + @params + end end + alias_method :query, :to_hash + alias_method :to_h, :to_hash + # @returns a params hash for searching solr. # The CatalogController #index action uses this. # Solr parameters can come from a number of places. From lowest # precedence to highest: # 1. General defaults in blacklight config (are trumped by) @@ -80,10 +131,11 @@ scope.blacklight_config end def start start = nil if start + params_will_change! @start = start.to_i self else @start ||= (page - 1) * (rows || 10) @@ -94,10 +146,11 @@ end alias_method :padding, :start def page page = nil if page + params_will_change! @page = page.to_i @page = 1 if @page < 1 self else @page ||= begin @@ -112,10 +165,11 @@ end end def rows rows = nil if rows + params_will_change! @rows = rows.to_i @rows = blacklight_config.max_per_page if @rows > blacklight_config.max_per_page self else @rows ||= begin @@ -133,15 +187,10 @@ end end alias_method :per, :rows - protected - def request - Blacklight::Solr::Request.new - end - def sort field = if blacklight_params[:sort].blank? and sort_field = blacklight_config.default_sort_field # no sort param provided, use default sort_field.sort elsif sort_field = blacklight_config.sort_fields[blacklight_params[:sort]] @@ -156,15 +205,36 @@ end def search_field blacklight_config.search_fields[blacklight_params[:search_field]] end - + + protected + def request + Blacklight::Solr::Request.new + end + def should_add_field_to_request? field_name, field field.include_in_request || (field.include_in_request.nil? && blacklight_config.add_field_configuration_to_solr_request) end def scope @scope + end + + def params_will_change! + @dirty = true + end + + def params_changed? + !!@dirty + end + + def params_need_update? + params_changed? || @params.nil? + end + + def clear_changes + @dirty = false end end end