Sha256: a17ccd59b686b738586c53564ae3acc8dedab2a8ea719626e354b3e977245dab

Contents?: true

Size: 1.56 KB

Versions: 46

Compression:

Stored size: 1.56 KB

Contents

module Spree
  module Taxons
    class Find
      def initialize(scope:, params:)
        @scope = scope
        @ids      = String(params.dig(:filter, :ids)).split(',')
        @parent   = params.dig(:filter, :parent_id)
        @taxonomy = params.dig(:filter, :taxonomy_id)
        @name     = params.dig(:filter, :name)
        @roots    = params.dig(:filter, :roots)
      end

      def execute
        taxons = by_ids(scope)
        taxons = by_parent(taxons)
        taxons = by_taxonomy(taxons)
        taxons = by_roots(taxons)
        taxons = by_name(taxons)

        taxons
      end

      private

      attr_reader :ids, :parent, :taxonomy, :roots, :name, :scope

      def ids?
        ids.present?
      end

      def parent?
        parent.present?
      end

      def taxonomy?
        taxonomy.present?
      end

      def roots?
        roots.present?
      end

      def name?
        name.present?
      end

      def name_matcher
        Spree::Taxon.arel_table[:name].matches("%#{name}%")
      end

      def by_ids(taxons)
        return taxons unless ids?

        taxons.where(id: ids)
      end

      def by_parent(taxons)
        return taxons unless parent?

        taxons.where(parent_id: parent)
      end

      def by_taxonomy(taxons)
        return taxons unless taxonomy?

        taxons.where(taxonomy_id: taxonomy)
      end

      def by_roots(taxons)
        return taxons unless roots?

        taxons.roots
      end

      def by_name(taxons)
        return taxons unless name?

        taxons.where(name_matcher)
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
spree_core-4.0.9 app/finders/spree/taxons/find.rb
spree_core-4.1.15 app/finders/spree/taxons/find.rb
spree_core-4.2.7 app/finders/spree/taxons/find.rb
spree_core-4.2.6 app/finders/spree/taxons/find.rb
spree_core-4.3.0.rc2 app/finders/spree/taxons/find.rb
spree_core-4.3.0.rc1 app/finders/spree/taxons/find.rb
spree_core-4.2.5 app/finders/spree/taxons/find.rb
spree_core-4.1.14 app/finders/spree/taxons/find.rb
spree_core-4.0.8 app/finders/spree/taxons/find.rb
spree_core-4.2.4 app/finders/spree/taxons/find.rb
spree_core-4.2.3.1 app/finders/spree/taxons/find.rb
spree_core-4.1.13.1 app/finders/spree/taxons/find.rb
spree_core-4.0.7.1 app/finders/spree/taxons/find.rb
spree_core-4.2.3 app/finders/spree/taxons/find.rb
spree_core-4.2.2 app/finders/spree/taxons/find.rb
spree_core-4.2.1 app/finders/spree/taxons/find.rb
spree_core-4.0.7 app/finders/spree/taxons/find.rb
spree_core-4.2.0 app/finders/spree/taxons/find.rb
spree_core-4.2.0.rc5 app/finders/spree/taxons/find.rb
spree_core-4.2.0.rc4 app/finders/spree/taxons/find.rb