Sha256: 54c26dbfec52281a46580ae4dc94133e9817b067b48cab7b138c5a4d59b372e3

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Admin::Resources::FiltersHelper

  def build_filters(resource = @resource, params = params)
    typus_filters = resource.typus_filters

    return if typus_filters.empty?

    locals = {}

    locals[:filters] = typus_filters.map do |key, value|
                         { :filter => set_filter(key, value), :items => send("#{value}_filter", key) }
                       end

    locals[:hidden_filters] = params.dup

    # Remove default params.
    rejections = %w(controller action locale utf8 sort_order order_by)
    locals[:hidden_filters].delete_if { |k, v| rejections.include?(k) }

    # Remove also custom params.
    rejections = locals[:filters].map { |f| f[:filter] }
    locals[:hidden_filters].delete_if { |k, v| rejections.include?(k) }

    render "helpers/admin/resources/filters", locals
  end

  def set_filter(key, value)
    return key unless value == :belongs_to

    att_assoc = @resource.reflect_on_association(key.to_sym)
    class_name = att_assoc.options[:class_name] || key.capitalize.camelize
    resource = class_name.typus_constantize
    att_assoc.foreign_key
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
typus-3.1.0.rc8 app/helpers/admin/resources/filters_helper.rb
typus-3.1.0.rc7 app/helpers/admin/resources/filters_helper.rb