Topic: Refactoring my controller - not sure if to use instance_eval

scenario

(a user enters a search string and then i need to use Xapian to find the results per model)

I have set @search.included_modules to a string like "0 1 2 3 4 5 6 7" to indicate which models to search in from a posted form with check boxes.. I have around 20 models to search so the code below is a snapshot of the entire chunk but you get the idea

heres the gist of what I'm trying to do



        @t="(user:#{@current_user.id} OR is_public:1) AND "+@search_term

        if @search.included_modules.include?(0.to_s)
          @jobs_xapian_search=ActsAsXapian::Search.new([Job], @t)
          @jobs=@jobs_xapian_search.results.collect {|r| r[:model]}
          @jobs_count=@jobs.count
        end
       
        if @search.included_modules.include?(1.to_s)
          @animals_xapian_search=ActsAsXapian::Search.new([Animal], "(user:#{@current_user.id} OR doctor:#{@current_user.id}) AND "+ @search_term)
          @animals=@animals_xapian_search.results.collect {|r| r[:model]}
          @animals_count=@animals.count
        end
       
       
        if @search.included_modules.include?(2.to_s)
          @books_xapian_search=ActsAsXapian::Search.new([Book], @t)
          @books=@books_xapian_search.results.collect {|r| r[:model]}
          @books_count=@books.count
        end
       
       
        if @search.included_modules.include?(3.to_s)
          @comments_xapian_search=ActsAsXapian::Search.new([Comment], "(user:#{@current_user.id} OR receiver:#{@current_user.id}) AND "+@search_term)
          @comments=@comments_xapian_search.results.collect {|r| r[:model]}
          @comments_count=@comments.count
        end
         
        if @search.included_modules.include?(4.to_s)
          @businesses_xapian_search=ActsAsXapian::Search.new([Business], @t)
          @businesses=@businesses_xapian_search.results.collect {|r| r[:model]}
          @businesses_count =@businesses.count
        end

        if @search.included_modules.include?(5.to_s)
          @contacts_xapian_search=ActsAsXapian::Search.new([Contact],@t)
          @contacts=@contacts_xapian_search.results.collect {|r| r[:model]}
          @contacts_count=@contacts.count
        end
 
      ...... + pages of similar code  :-).....


I've played with looping through an array

e.g.

      %w[jobs books messages businesses contacts].each_with_index do |item,index|
        if @search.included_modules.include?(index.to_s)

         ....some conditions for special cases in the @t search terms exlcuded for simplicity.....

          instance_variable_set("@#{item}_xapian_search", ActsAsXapian::Search.new([item.titleize.singularize], @t))
          instance_variable_set("@#{item}", instance_variable_get("@#{item}_xapian_search").results.collect {|r| r[:model]}   )
          instance_variable_set("@#{item}_count", instance_variable_get("@#{item}").count )


        end
      end


I results in my view for the respective @jobs, @animals etc.  It works but is this the best way to do it?

I'm using rails3, ruby 1.9.1 BTW.

Could somebody please help me out and see if I am doing this ok?!

Thanks ne1.

Bert

Last edited by noooobierails (2010-03-09 16:27:28)