Sha256: f705f5294b469578117ca1a28e4e967a8944d4ba280fe917acda951222dbc00f

Contents?: true

Size: 1.25 KB

Versions: 70

Compression:

Stored size: 1.25 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
      queriable_attributes.keys
    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_param)
      params.reject { |it| it == excluded_param.to_s }
    end

    def scoped_query_by(params, excluded_param=nil)
      query_methods.inject(all) do |scope, method|
        valid_params = valid_params_for(method, params, excluded_param)
        scoped_query_module(method).query_by valid_params, scope, self
      end
    end

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

Version data entries

70 entries across 70 versions & 2 rubygems

Version Path
mumuki-domain-7.6.1 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.6.0 app/models/concerns/with_scoped_queries.rb
mumuki-laboratory-7.5.2 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-ad16a5930cec/app/models/concerns/with_scoped_queries.rb
mumuki-laboratory-7.5.1 vendor/bundle/ruby/2.3.0/bundler/gems/mumuki-domain-a059418e5fd8/app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.5.1 app/models/concerns/with_scoped_queries.rb
mumuki-laboratory-7.5.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-acb12583b793/app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.5.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.4.1 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.4.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.3.2 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.3.1 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.2.3 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.3.0 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.2.2 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.2.1 app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.2.0 app/models/concerns/with_scoped_queries.rb
mumuki-laboratory-7.1.0 vendor/bundle/ruby/2.3.0/bundler/gems/mumuki-domain-925adf85cca8/app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.1.0 app/models/concerns/with_scoped_queries.rb
mumuki-laboratory-7.0.12 vendor/bundle/ruby/2.3.0/bundler/gems/mumuki-domain-74da3d4af98c/app/models/concerns/with_scoped_queries.rb
mumuki-domain-7.0.6 app/models/concerns/with_scoped_queries.rb