Class: Workarea::Catalog::Customizations

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/workarea/catalog/customizations.rb

Overview

This is the base class for creating types of product customizations. To make a new type of customizations, simply inherit this class, add the list of customized_fields, and add the appropriate ActiveModel::Validations.

Examples:

Subclass Customizations

class Catalog::Customizations::Monogram < Catalog::Customizations
  customized_fields :first, :second, :third
  validates_presence_of :first, :second, :third
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product_id, attributes) ⇒ Customizations

Returns a new instance of Customizations.



48
49
50
51
52
53
54
55
# File 'app/models/workarea/catalog/customizations.rb', line 48

def initialize(product_id, attributes)
  @product_id = product_id
  @attributes = attributes.with_indifferent_access

  attributes.each do |name, value|
    instance_variable_set("@#{name.to_s.underscore.optionize('_')}", value)
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



15
16
17
# File 'app/models/workarea/catalog/customizations.rb', line 15

def attributes
  @attributes
end

#product_idObject (readonly)

Returns the value of attribute product_id.



15
16
17
# File 'app/models/workarea/catalog/customizations.rb', line 15

def product_id
  @product_id
end

Class Method Details

.customized_fields(*fields) ⇒ void

This method returns an undefined value.

Called by classes that inherit from Catalog::Customizations to indicate the fields that need to be stored. All field names are converted to snake_case format.

Parameters:

  • fields (Array<Symbol>)


24
25
26
27
28
29
30
31
# File 'app/models/workarea/catalog/customizations.rb', line 24

def self.customized_fields(*fields)
  class_eval do
    cattr_accessor :fields
    self.fields = fields

    fields.each { |field| attr_reader field }
  end
end

.find(product_id, attributes) ⇒ Catalog::Customizations

Find Catalog::Customizations for a product. The implementation class is decided based on the customizations that the product supports.

Parameters:

Returns:



40
41
42
43
44
45
46
# File 'app/models/workarea/catalog/customizations.rb', line 40

def self.find(product_id, attributes)
  product = Workarea::Catalog::Product.find(product_id)
  return nil unless product && product.customizations.present?

  klass = "Workarea::Catalog::Customizations::#{product.customizations.classify}".constantize
  klass.new(product_id, attributes)
end

Instance Method Details

#present?Boolean

Customizations are only present if the customized fields are present.

Returns:

  • (Boolean)


62
63
64
# File 'app/models/workarea/catalog/customizations.rb', line 62

def present?
  to_h.present? && super
end

#to_hHash

A Hash representation of the customizations. This will be stored with the Order::Item.

Returns:

  • (Hash)


71
72
73
74
75
76
77
78
79
80
# File 'app/models/workarea/catalog/customizations.rb', line 71

def to_h
  @hash ||=
    begin
      present_fields = self.class.fields.select do |field|
        attributes[field].present?
      end

      attributes.slice(*present_fields)
    end
end