Module: SpreeMobility::CoreExt::Spree::ProductDecorator

Defined in:
lib/spree_mobility/core_ext/spree/product_decorator.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/spree_mobility/core_ext/spree/product_decorator.rb', line 31

def self.prepended(base)
  base.include SpreeMobility::Translatable
  SpreeMobility.translates_for base, :name, :description, :meta_title, :meta_description, :meta_keywords, :slug
  base.friendly_id :slug_candidates, use: [:history, :mobility]

  base.translation_class.class_eval do
    acts_as_paranoid
    after_destroy :punch_slug
    default_scopes = []
    validates :slug, presence: true, uniqueness: { scope: :locale, case_sensitive: false }

    with_options length: { maximum: 255 }, allow_blank: true do
      validates :meta_keywords
      validates :meta_title
    end
    with_options presence: true do
      validates :name
    end
  end

  if RUBY_VERSION.to_f >= 2.5
    base.translation_class.define_method(:punch_slug) { update(slug: "#{Time.current.to_i}_#{slug}"[0..254]) }
  else
    base.translation_class.send(:define_method, :punch_slug) { update(slug: "#{Time.current.to_i}_#{slug}"[0..254]) }
  end
end

Instance Method Details

#duplicate_extra(old_product) ⇒ Object



62
63
64
# File 'lib/spree_mobility/core_ext/spree/product_decorator.rb', line 62

def duplicate_extra(old_product)
  duplicate_translations(old_product)
end

#property(property_name) ⇒ Object



66
67
68
# File 'lib/spree_mobility/core_ext/spree/product_decorator.rb', line 66

def property(property_name)
  product_properties.joins(:property).find_by(spree_properties: { id: ::Spree::Property.find_by(name: property_name) }).try(:value)
end

#punch_slugObject

Don’t punch slug on original product as it prevents bulk deletion. Also we don’t need it, as it is translated.



60
# File 'lib/spree_mobility/core_ext/spree/product_decorator.rb', line 60

def punch_slug; end