From aaf20b2670a87ff7a2554e25d11fb86b419a90d6 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Sat, 24 Mar 2007 07:40:44 +0000 Subject: [PATCH] Move the page number out of session scope into the Context. git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@521989 13f79535-47bb-0310-9956-ffa450edef68 --- .../ruby/flare/app/controllers/application.rb | 12 ------ .../ruby/flare/app/views/browse/index.rhtml | 2 +- .../vendor/plugins/flare/lib/flare/context.rb | 9 +++- .../flare/lib/flare/controller_extensions.rb | 43 +++++++++++-------- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/client/ruby/flare/app/controllers/application.rb b/client/ruby/flare/app/controllers/application.rb index f00ff230cd7..eafa2eaa39e 100644 --- a/client/ruby/flare/app/controllers/application.rb +++ b/client/ruby/flare/app/controllers/application.rb @@ -16,16 +16,4 @@ class ApplicationController < ActionController::Base # Pick a unique cookie name to distinguish our session data from others' session :session_key => '_flare_session_id' - - before_filter :flare_before - - -private - def flare_before - session[:page] ||= 1 - - session[:flare_context] ||= Flare::Context.new(SOLR_CONFIG) - - @flare = session[:flare_context] - end end diff --git a/client/ruby/flare/app/views/browse/index.rhtml b/client/ruby/flare/app/views/browse/index.rhtml index fb3b487bdfd..8495f72a217 100644 --- a/client/ruby/flare/app/views/browse/index.rhtml +++ b/client/ruby/flare/app/views/browse/index.rhtml @@ -102,6 +102,6 @@ click click click -
<%=link_to_if session[:page] != 1, "<<", :page => session[:page] - 1%> Results <%=[@start + 1,@response.total_hits].min%>-<%=[@response.total_hits,@results_per_page + @start].min%> of <%=@response.total_hits%><%=link_to_if session[:page] < (@response.total_hits.to_f / @results_per_page).ceil, ">>", :page => session[:page] + 1%>
+
<%=link_to_if @flare.page != 1, "<<", :page => @flare.page - 1%> Results <%=[@start + 1,@response.total_hits].min%>-<%=[@response.total_hits,@results_per_page + @start].min%> of <%=@response.total_hits%><%=link_to_if @flare.page < (@response.total_hits.to_f / @results_per_page).ceil, ">>", :page => @flare.page + 1%>
<% end %> \ No newline at end of file diff --git a/client/ruby/flare/vendor/plugins/flare/lib/flare/context.rb b/client/ruby/flare/vendor/plugins/flare/lib/flare/context.rb index 20785fee007..a6a881605e5 100644 --- a/client/ruby/flare/vendor/plugins/flare/lib/flare/context.rb +++ b/client/ruby/flare/vendor/plugins/flare/lib/flare/context.rb @@ -11,11 +11,12 @@ # limitations under the License. class Flare::Context - attr_accessor :queries, :filters, :facet_queries, :applied_facet_queries + attr_accessor :queries, :filters, :facet_queries, :applied_facet_queries, :page attr_reader :facet_fields, :text_fields - def initialize(solr_config) + def initialize(solr_config={}) @solr_config = solr_config + @connection = Solr::Connection.new(@solr_config[:solr_url]) clear @@ -27,12 +28,16 @@ class Flare::Context @facet_fields = @index_info.field_names.find_all {|v| v =~ /_facet$/} - excluded @text_fields = @index_info.field_names.find_all {|v| v =~ /_text$/} + + @page = 1 end def clear + #TODO unify initialize and clear @queries = [] @filters = [] @applied_facet_queries = [] + @page = 1 # this is cleared for development purposes - allowing flare to stay running but different Solr datasets swapping @index_info = @connection.send(Solr::Request::IndexInfo.new) diff --git a/client/ruby/flare/vendor/plugins/flare/lib/flare/controller_extensions.rb b/client/ruby/flare/vendor/plugins/flare/lib/flare/controller_extensions.rb index e8703f15eff..1d1507e1fed 100755 --- a/client/ruby/flare/vendor/plugins/flare/lib/flare/controller_extensions.rb +++ b/client/ruby/flare/vendor/plugins/flare/lib/flare/controller_extensions.rb @@ -20,26 +20,25 @@ module Flare module ClassMethods def flare(options={}) include Flare::ActionControllerExtensions::InstanceMethods + before_filter :flare_before end end module InstanceMethods - - # Assumes @flare is set by a before_filter - def index - session[:page] = params[:page].to_i if params[:page] - session[:page] = 1 if session[:page] <= 0 - @results_per_page = 25 + + if params[:page] + @flare.page = params[:page].to_i + end - @start = (session[:page] - 1) * @results_per_page + @start = (@flare.page - 1) * @results_per_page @response = @flare.search(@start, @results_per_page) end def facet - puts "---- facet: #{params[:field]}" + logger.debug "---- facet: #{params[:field]}" @facets = @flare.retrieve_field_facets(params[:field]) end @@ -53,14 +52,14 @@ module Flare def add_query @flare.queries << {:query => params[:search][:query]} - session[:page] = 1 + @flare.page = 1 redirect_to :action => 'index' end def update_query logger.debug "update_query: #{params.inspect}" @flare.queries[params[:index].to_i][:query] = params[:value] - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes render :update do |page| page.redirect_to '/browse' end @@ -69,32 +68,32 @@ module Flare def invert_query q = @flare.queries[params[:index].to_i] q[:negative] = !q[:negative] - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end def remove_query @flare.queries.delete_at(params[:index].to_i) - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end def invert_filter f = @flare.filters[params[:index].to_i] f[:negative] = !f[:negative] - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end def remove_filter @flare.filters.delete_at(params[:index].to_i) - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end def add_filter @flare.filters << {:field => params[:field], :value => params[:value], :negative => (params[:negative] ? true : false)} - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end @@ -105,7 +104,7 @@ module Flare def remove_saved_constraint @flare.applied_facet_queries.delete_at(params[:index].to_i) - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end @@ -131,16 +130,24 @@ module Flare @flare.facet_queries.delete(params[:name]) @flare.applied_facet_queries.delete_if {|f| params[:name] == f[:name]} puts "---- AFTER", @flare.to_s - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end def invert_saved_constraint f = @flare.applied_facet_queries[params[:index].to_i] f[:negative] = !f[:negative] - session[:page] = 1 + @flare.page = 1 # TODO: let the context adjust this automatically when its state changes redirect_to :action => 'index' end + + private + def flare_before + # TODO: allow source of context to be configurable. + session[:flare_context] ||= Flare::Context.new(SOLR_CONFIG) + + @flare = session[:flare_context] + end end end