module ActiveScaffold::Actions
  module Search
    def self.included(base)
      base.send :include, ActiveScaffold::Actions::CommonSearch
      base.send :include, InstanceMethods
    end
    
    module InstanceMethods

      protected
      def search_respond_to_html
        render(:action => "search")
      end
      def search_respond_to_js
        render(:partial => "search")
      end
      def do_search
        if search_params.is_a?(String) && search_params.present?
          query = search_params.to_s.strip
          columns = active_scaffold_config.search.columns
          text_search = active_scaffold_config.search.text_search
          query = query.split(active_scaffold_config.search.split_terms) if active_scaffold_config.search.split_terms
          search_conditions = self.class.create_conditions_for_columns(query, columns, text_search)
          @filtered = !search_conditions.blank?
          self.active_scaffold_conditions.concat search_conditions if @filtered

          references, outer_joins = columns.partition{ |column| column.includes.present? && list_columns.include?(column)}
          outer_joins.collect! { |column| column.search_joins}
          references.collect! { |column| column.includes }
          self.active_scaffold_outer_joins.concat outer_joins.flatten.uniq.compact
          self.active_scaffold_references.concat references.flatten.uniq.compact

          active_scaffold_config.list.user.page = nil
        else
          super
        end
      end

      def search_ignore?
        active_scaffold_config.list.always_show_search == :search || global_search_ignore?
      end

      private
      def search_formats
        (default_formats + active_scaffold_config.formats + active_scaffold_config.search.formats).uniq
      end
    end
  end
end
