Sha256: eb10f98347cd3f96e19e4834b9f446dcf87ba560f32c9abf93595202fb13714b

Contents?: true

Size: 651 Bytes

Versions: 1

Compression:

Stored size: 651 Bytes

Contents

module Shoppe
  class Product
    
    # Relationships
    has_many :variants, :class_name => 'Shoppe::Product', :foreign_key => 'parent_id', :dependent => :destroy
    belongs_to :parent, :class_name => 'Shoppe::Product', :foreign_key => 'parent_id'
    
    # Validations
    validate do
      errors.add :base, "can only belong to a root product" if self.parent && self.parent.parent
    end
    
    # Scopes
    scope :root, -> { where(:parent_id => nil) }
    
    def has_variants?
      !variants.empty?
    end
    
    def default_variant
      return nil if self.parent
      @default_variant ||= self.variants.first
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoppe-0.0.15 app/models/shoppe/product/variants.rb