Class: Workarea::Catalog::Customizations
- Inherits:
-
Object
- Object
- Workarea::Catalog::Customizations
- 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.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#product_id ⇒ Object
readonly
Returns the value of attribute product_id.
Class Method Summary collapse
-
.customized_fields(*fields) ⇒ void
Called by classes that inherit from Catalog::Customizations to indicate the fields that need to be stored.
-
.find(product_id, attributes) ⇒ Catalog::Customizations
Find Catalog::Customizations for a product.
Instance Method Summary collapse
-
#initialize(product_id, attributes) ⇒ Customizations
constructor
A new instance of Customizations.
-
#present? ⇒ Boolean
Customizations are only present if the customized fields are present.
-
#to_h ⇒ Hash
A Hash representation of the customizations.
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
15 16 17 |
# File 'app/models/workarea/catalog/customizations.rb', line 15 def attributes @attributes end |
#product_id ⇒ Object (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.
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.
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.
62 63 64 |
# File 'app/models/workarea/catalog/customizations.rb', line 62 def present? to_h.present? && super end |
#to_h ⇒ Hash
A Hash representation of the customizations. This will be stored with the Order::Item.
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 |