Skip to content

Filter support #13

@AlexanderPavlenko

Description

@AlexanderPavlenko

Thanks for this gem! I managed to make the filters form preserve its state after the submit and also render "Search status" sidebar content.

class Test < ActiveResource::Base
  class << self
    RansackContextStub = Struct.new(:class_name) do
      def bind(attr, attr_name)
        attr.attr_name = attr_name
        attr.parent    = class_name
      end

      def klassify(parent)
        parent.constantize
      end

      def type_for(attr)
        klassify(attr.parent).schema.fetch(attr.name)
      end

      def present?
        false
      end

      def lock_association(*) end

      def auth_object; end
    end

    def ransack_context
      @ransack_context ||= RansackContextStub.new(name)
    end

    def _ransackers
      values =
        schema.map do |k, v|
          Struct.new(:name, :type) do
            def attr_from(_bindable)
              name # not sure what to return, but it's probably not used anyway
            end
          end.new(k, v)
        end
      schema.keys.zip(values).to_h
    end

    def ransackable_attributes(*)
      schema.keys
    end

    def ransack(params = {}, _options = {})
      @ransack_params = params.blank? ? {} : params.permit!.to_h
      conditions      = @ransack_params.map { |k, v| Ransack::Nodes::Condition.extract(ransack_context, k, v) }
      result          = OpenStruct.new(conditions: conditions, object: OpenStruct.new(klass: self), result: self)
      @ransack_params.each { |k, v| result[k] = v }
      result.instance_variable_set '@scope_args', []
      result
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions