Sha256: 70823b11c45777219fb460370e33a691fc6f51e0695258ef19011d74ddc5bf05

Contents?: true

Size: 1.21 KB

Versions: 9

Compression:

Stored size: 1.21 KB

Contents

module Spree
  class Property < ActiveRecord::Base
    has_and_belongs_to_many :prototypes, :join_table => 'spree_properties_prototypes'

    has_many :product_properties, :dependent => :destroy
    has_many :products, :through => :product_properties

    after_destroy :recalculate_product_group_products

    validates :name, :presentation, :presence => true

    scope :sorted, lambda { order(:name) }

    def self.find_all_by_prototype(prototype)
      id = prototype
      if prototype.class == Prototype
        id = prototype.id
      end
      joins("LEFT JOIN properties_prototypes ON property_id = #{self.table_name}.id").where(:prototype_id => id)
    end

    private

    # A fix for #774
    # What was happening was that when a property was deleted, any product group
    # that used the property to calculate included properties was not recalculated
    #
    # Recalculates product group products after the property has been deleted
    def recalculate_product_group_products
      ProductScope.where(:name => "with_property", :arguments => [self.name].to_yaml).each do |scope|
        # Triggers ProductGroup#update_memberships callback to recalculate products
        scope.product_group.save!
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spree_core-1.0.4 app/models/spree/property.rb
spree_core-1.0.3 app/models/spree/property.rb
spree_core-1.0.2 app/models/spree/property.rb
spree_core-1.0.1 app/models/spree/property.rb
spree_core-1.0.0 app/models/spree/property.rb
spree_core-1.0.0.rc4 app/models/spree/property.rb
spree_core-1.0.0.rc3 app/models/spree/property.rb
spree_core-1.0.0.rc2 app/models/spree/property.rb
spree_core-1.0.0.rc1 app/models/spree/property.rb