Spree: Customize Key Product Attributes

Does anyone know if it is possible to add a new attribute to a set of key attributes (name, description, permalink, description of metadata, etc.) of a product? The idea is that I want these attributes to be available when I create the product, and not add it later through Product Properties.

Thanks.

+6
source share
2 answers

The easiest way is to add attributes directly to the product model through migration. Ratings can be added using decorators, the preferred template in Spree to override models.

# in app/models/product_decorator.rb Product.class_eval do validates :some_field, :presence => true end 

Another option is to create a secondary model for your extended fields. Maybe ProductExtension

 # in app/models/product_extension.rb class ProductExtension < ActiveRecord::Base belongs_to :product validates :some_field, :presence => true end # in app/models/product_decorator.rb Product.class_eval do has_one :product_extension accepts_nested_attributes_for :product_extension delegate :some_field, :to => :product_extension end 

Then, in the product creation forms, you can provide these fields from field_to_. I think one caveat to this is that you will need to create a created product model before the extension becomes usable. Perhaps you will get around this with additional logic in the product controllers, create an action.

+9
source

My way to extend the product model for Spree (via delegate_belongs_to):

 #app/models/product_decorator.rb Spree::Product.class_eval do has_one :product_extension accepts_nested_attributes_for :product_extension, :allow_destroy => true delegate_belongs_to :product_extension, :some_field attr_accessible :some_field end #app/models/product_extension.rb class ProductExtension < ActiveRecord::Base belongs_to :product, :class_name => 'Spree::Product' attr_accessible :some_field end 
0
source

Source: https://habr.com/ru/post/887240/


All Articles