Sha256: fdd7415dd66c6a0ec9753c32a6be618d5e43cce223f442732d604b53a96e80c3

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

require 'active_support/concern'

module SimpleSearchFilter
  module Controller
    extend ActiveSupport::Concern

    included do

    end

    def init_search_data(name)
      # input
      pg = params[@filter.page_param_name].to_i || 1
      cmd = params[:cmd] || ''

      # page
      @filter.page = pg

      # input from GET
      @filter.fields.each do |name, f|
        if params.has_key? name
          @filter.set name, params[name]
        end
      end

      # post - save filter and redirect
      #if request.post? && params[:filter]
      if params[:filter]
        if cmd=='clear'
          @filter.clear_data
        else
          @filter.set_data_from_form params[:filter]
        end

        #(redirect_to url and return) if @filter.search_method_post_and_redirect?
      end

      if cmd=='order'
        @filter.set_order params[:orderby], params[:orderdir]
        (redirect_to action: name.to_sym and return) if @filter.search_method_post_and_redirect?
      end



    end

    module ClassMethods
      def search_filter(name, options = {}, &block)
        # raise "You need a block to build!" unless block_given?

        init_method = "init_search_filter_#{name.to_s}"

        define_method(init_method) do
          prefix = options[:prefix] || "filter_#{params[:controller]}_#{name}"

          # create Filter object
          @filter = SimpleSearchFilter::Filter.new(self.session, prefix, options)

          # define filter
          @filter.instance_eval(&block)

          # set data from params
          init_search_data(name)

        end

        # before_action callback
        self.before_action :"#{init_method}", only: [name.to_sym, options[:search_action]]


        #
        define_method("#{options[:search_action]}") do
          redirect_to action: name.to_sym
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_search_filter-0.0.16 lib/simple_search_filter/controller.rb
simple_search_filter-0.0.15 lib/simple_search_filter/controller.rb