Skip to content

Commit

Permalink
fixed #465, destoyed product should not be contain in smart collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
saberma committed Aug 15, 2012
1 parent f16b2da commit 77d7719
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/products_controller.rb
Expand Up @@ -103,7 +103,7 @@ def duplicate
new_product = product.dup
new_product.variants = product.variants.map(&:dup)
new_product.options = product.options.map(&:dup).each{|o| o.position = nil } # Fixed: #159 position不为空导致排序报错
new_product.collection_products = product.collection_products.map(&:dup)
new_product.custom_collection_products = product.custom_collection_products.map(&:dup)
new_product.tags_text = product.tags_text
new_product.update_attributes title: params[:new_title]
render json: {id: new_product.id}
Expand Down
13 changes: 7 additions & 6 deletions app/models/product.rb
Expand Up @@ -2,12 +2,13 @@
class Product < ActiveRecord::Base
include Models::Handle
belongs_to :shop
has_many :photos , dependent: :destroy , order: 'position asc'
has_many :variants , dependent: :destroy , class_name: 'ProductVariant' , order: 'position asc'
has_many :options , dependent: :destroy , class_name: 'ProductOption' , order: 'position asc'
has_many :collection_products, dependent: :destroy , class_name: 'CustomCollectionProduct'
has_many :collections , class_name: 'CustomCollection', through: :collection_products , source: :custom_collection
has_and_belongs_to_many :tags, order: 'id asc'
has_many :photos , dependent: :destroy , order: 'position asc'
has_many :variants , dependent: :destroy , class_name: 'ProductVariant' , order: 'position asc'
has_many :options , dependent: :destroy , class_name: 'ProductOption' , order: 'position asc'
has_many :custom_collection_products, dependent: :destroy , class_name: 'CustomCollectionProduct'
has_many :custom_collections , class_name: 'CustomCollection', through: :custom_collection_products , source: :custom_collection
has_many :smart_collection_products , dependent: :destroy , class_name: 'SmartCollectionProduct'
has_and_belongs_to_many :tags , order: 'id asc'
# 标签
attr_accessor :tags_text,:images
attr_accessible :handle, :title, :published, :body_html, :price, :product_type, :vendor, :tags_text, :images, :photos_attributes, :variants_attributes, :options_attributes, :collection_ids
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -3,7 +3,7 @@ class User < ActiveRecord::Base
extend ActiveHash::Associations::ActiveRecordExtensions

belongs_to :shop
has_many :articles , dependent: :destroy
has_many :articles
has_many :permissions , dependent: :destroy

# Include default devise modules. Others available are:
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/products/_collection.html.haml
@@ -1,3 +1,3 @@
%li.product-edit-collection
=check_box_tag 'product[collection_ids][]', collection.id, product.collections.include?(collection) || (collection.handle == 'frontpage' and collection.products.size.zero?), id: "collection_#{collection.id}"
=check_box_tag 'product[collection_ids][]', collection.id, product.custom_collections.include?(collection) || (collection.handle == 'frontpage' and collection.products.size.zero?), id: "collection_#{collection.id}"
%label(for="collection_#{collection.id}")=collection.title
@@ -0,0 +1,9 @@
class RemoveDestroyedSmartCollectionProduct < ActiveRecord::Migration
def up
SmartCollection
SmartCollectionProduct.all.select{|smart_collection_product| smart_collection_product.product.nil? }.map(&:destroy)
end

def down
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120810144404) do
ActiveRecord::Schema.define(:version => 20120815032021) do

create_table "active_admin_comments", :force => true do |t|
t.integer "resource_id", :null => false
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/smart_collections.rb
Expand Up @@ -6,6 +6,12 @@
body_html "低价商品"
end

factory :smart_collection_default, parent: :smart_collection do
rules_attributes [
{column: 'title', relation: 'equals', condition: ''}
]
end

factory :smart_collection_low_price, parent: :smart_collection do
rules_attributes [
{column: 'variants_price', relation: 'less_than', condition: 100}
Expand Down
27 changes: 27 additions & 0 deletions spec/models/smart_collection_spec.rb
Expand Up @@ -5,6 +5,10 @@

let(:shop) { Factory(:user).shop }

let(:iphone4) { FactoryGirl.create :iphone4, shop: shop }

let(:smart_collection) { FactoryGirl.create :smart_collection_default, shop: shop }

context '#create' do

context '(without handle)' do
Expand All @@ -26,4 +30,27 @@

end

describe '#465' do

context 'product destroyed' do

before do
iphone4
smart_collection
end

describe 'collection_products' do

it "should be destroy" do
expect do
iphone4.destroy
end.to change(SmartCollectionProduct, :count).by(-1)
end

end

end

end

end
4 changes: 2 additions & 2 deletions spec/requests/smart_collections_spec.rb
Expand Up @@ -47,8 +47,8 @@
context '#465' do

before do
psp.destroy
iphone4 # id 不为 1
psp.destroy # 保证 id=1 的记录不存在
iphone4 # id = 2
smart_collection # new(product_id: product) product_id 都会被置为 1;要使用 new(product_id: product.id)
end

Expand Down

0 comments on commit 77d7719

Please sign in to comment.