Sha256: c15eb79cf4775f4b4f4dc8f64cd8b48de30585fe7df775a3b022da04a9ce99a3

Contents?: true

Size: 708 Bytes

Versions: 34

Compression:

Stored size: 708 Bytes

Contents

module Workarea
  module FeaturedProducts
    extend ActiveSupport::Concern

    included do
      field :product_ids, type: Array, default: []
      before_validation :clean_product_ids

      index({ product_ids: 1 })
    end

    def featured_products?
      product_ids.present?
    end

    def featured_product?(id)
      id.to_s.in?(product_ids)
    end

    def add_product(id)
      product_ids.prepend(id)
      save
    end

    def remove_product(id_to_remove)
      product_ids.reject! { |id| id == id_to_remove }
      save
    end

    private

    def clean_product_ids
      if product_ids.present?
        product_ids.reject!(&:blank?)
        product_ids.uniq!
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/models/workarea/featured_products.rb
workarea-core-3.4.44 app/models/workarea/featured_products.rb
workarea-core-3.4.43 app/models/workarea/featured_products.rb
workarea-core-3.4.42 app/models/workarea/featured_products.rb
workarea-core-3.4.41 app/models/workarea/featured_products.rb
workarea-core-3.4.40 app/models/workarea/featured_products.rb
workarea-core-3.4.39 app/models/workarea/featured_products.rb
workarea-core-3.4.38 app/models/workarea/featured_products.rb
workarea-core-3.4.37 app/models/workarea/featured_products.rb
workarea-core-3.4.36 app/models/workarea/featured_products.rb
workarea-core-3.4.35 app/models/workarea/featured_products.rb
workarea-core-3.4.34 app/models/workarea/featured_products.rb
workarea-core-3.4.33 app/models/workarea/featured_products.rb
workarea-core-3.4.32 app/models/workarea/featured_products.rb
workarea-core-3.4.31 app/models/workarea/featured_products.rb
workarea-core-3.4.30 app/models/workarea/featured_products.rb
workarea-core-3.4.29 app/models/workarea/featured_products.rb
workarea-core-3.4.28 app/models/workarea/featured_products.rb
workarea-core-3.4.27 app/models/workarea/featured_products.rb
workarea-core-3.4.26 app/models/workarea/featured_products.rb