Sha256: 00dc3d7d411c274668fd2f7498ad6fdd9175e366635d325b52c4a30ded35d54c

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

using Plutonium::Refinements::ParameterRefinements

module Plutonium
  module Resource
    module Controllers
      module Queryable
        extend ActiveSupport::Concern

        included do
          helper_method :resource_query_params, :current_query_object
        end

        def resource_query_object(resource_class, params)
          query_object_class = "#{resource_class}QueryObject".constantize
          query_object_class.new resource_context, params
        end

        def current_query_object
          @current_query_object ||= Plutonium::Resource::QueryObject.new(resource_context, resource_query_params) do |query_object|
            if current_definition.search_definition
              query_object.define_search proc { |scope, search:|
                current_definition.search_definition.call(scope, search)
              }
            end

            current_definition.defined_scopes.each do |key, value|
              query_object.define_scope key, value[:block]
            end

            current_definition.defined_sorts.each do |key, value|
              query_object.define_sorter key, value[:block]
            end

            query_object
          end
        end

        def resource_query_params
          (params[:q]&.nilify&.to_unsafe_h || {}).with_indifferent_access
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
plutonium-0.15.0 lib/plutonium/resource/controllers/queryable.rb
plutonium-0.15.0.pre.rc3 lib/plutonium/resource/controllers/queryable.rb
plutonium-0.15.0.pre.rc2 lib/plutonium/resource/controllers/queryable.rb
plutonium-0.15.0.pre.rc1 lib/plutonium/resource/controllers/queryable.rb