Sha256: e332210d5909fef6df0a0b29ed9587ab2d140f9482141e75ac0f58375ebd0419

Contents?: true

Size: 1.31 KB

Versions: 66

Compression:

Stored size: 1.31 KB

Contents

module WithScopedQueries
  extend ActiveSupport::Concern

  SCOPING_METHODS = [Filter, Sort, Page]

  included do
    class_attribute :queriable_attributes, instance_writer: false
    self.queriable_attributes = {}

    SCOPING_METHODS.each do |mod|
      define_singleton_method "#{mod.name.demodulize.underscore}able" do |*attributes|
        include mod
        mod.add_queriable_attributes_to(self, attributes)
      end
    end
  end

  class_methods do
    def query_methods(excluded_methods)
      queriable_attributes.keys - excluded_methods.to_a
    end

    def scoped_query_module(method)
      "WithScopedQueries::#{method.to_s.camelcase}".constantize
    end

    def permitted_query_params
      queriable_attributes.values.flatten
    end

    def actual_params(params, excluded_params)
      params.except(*excluded_params)
    end

    def scoped_query_by(params, **options)
      query_methods(options[:excluded_methods]).inject(all) do |scope, method|
        valid_params = valid_params_for(method, params, options[:excluded_params])
        scoped_query_module(method).query_by valid_params, scope, self
      end
    end

    def valid_params_for(method, params, excluded_params)
      actual_params = actual_params(params, excluded_params)
      actual_params.permit queriable_attributes[method]
    end
  end
end

Version data entries

66 entries across 66 versions & 2 rubygems

Version Path
mumuki-domain-9.20.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.19.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.18.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.17.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.16.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.15.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.14.1 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.14.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.13.1 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.13.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.12.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.11.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.10.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.9.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.8.1 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.8.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.7.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.6.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.5.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-9.4.0 app/models/concerns/with_scoped_queries.rb