Files
openfoodnetwork/app/models/spree/product_property.rb
Maikel Linke 1364b878fe Add ApplicationRecord for customisations
Rails 5 introduced this new class to confine application-specific monkey
patches to our models only, and not leak into other libraries using
ActiveRecord::Base.

https://bigbinary.com/blog/application-record-in-rails-5
2021-04-15 15:59:03 +10:00

28 lines
707 B
Ruby

# frozen_string_literal: true
module Spree
class ProductProperty < ApplicationRecord
belongs_to :product, class_name: "Spree::Product", touch: true
belongs_to :property, class_name: 'Spree::Property'
validates :property, presence: true
validates :value, length: { maximum: 255 }
default_scope -> { order("#{table_name}.position") }
# virtual attributes for use with AJAX completion stuff
def property_name
property&.name
end
def property_name=(name)
return if name.blank?
unless property = Property.find_by(name: name)
property = Property.create(name: name, presentation: name)
end
self.property = property
end
end
end