Skip to content

Commit

Permalink
Merge branch 'master' of github.com:saberma/shopqi
Browse files Browse the repository at this point in the history
  • Loading branch information
liwh committed Aug 26, 2011
2 parents 93a4130 + 9537c01 commit 3e5563c
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 84 deletions.
12 changes: 11 additions & 1 deletion app/coffeescripts/views/link_lists/edit.coffee
Expand Up @@ -19,9 +19,19 @@ App.Views.LinkList.Edit = Backbone.View.extend
self = this
$('li', @el).each -> #循环li,把输入项设置回model
model = self.model.links.get $("input[name='id']", this).val()
link_type = $('.selector', this).val()
handle = $("select.subject", this).val()
url = switch link_type
when 'blog', 'collection', 'page', 'product' then "/#{link_type}s/#{handle}"
when 'frontpage' then "/"
when 'search' then "/search"
when 'http' then $("input[name='url']", this).val()
model.set
title: $("input[name='title']", this).val()
subject: $("input[name='subject']", this).val()
link_type: link_type
subject_handle: handle
subject_params: $("input[name='subject_params']", this).val()
url: url
this.model._changed = true #修正:只修改link item时也要触发change事件,更新列表
this.model.save {
title: this.$("input[name='link_list[title]']").val()
Expand Down
18 changes: 18 additions & 0 deletions app/coffeescripts/views/link_lists/links/edit.coffee
Expand Up @@ -4,10 +4,13 @@ App.Views.LinkList.Links.Edit = Backbone.View.extend

events:
"click .delete": "destroy"
"change .selector": "select" # 选择链接类型

initialize: ->
@render()
@options.parent.append @el
@$('.selector').val(@model.get('link_type')).change()
@$('.subject').val @model.get('subject_handle')

render: ->
template = Handlebars.compile $('#edit-link-item').html()
Expand All @@ -27,3 +30,18 @@ App.Views.LinkList.Links.Edit = Backbone.View.extend
self.remove()
msg '删除成功!'
return false

select: ->
toggle_subject_handle = toggle_subject_params = toggle_subject_http = false
switch @$('.selector').val()
when 'blog', 'page', 'product'
@$('select.subject').html($("#selector-#{@$('.selector').val()}-item").html())
toggle_subject_handle = true
when 'collection'
@$('select.subject').html($('#selector-collection-item').html())
toggle_subject_handle = toggle_subject_params = true
when 'http'
toggle_subject_http = true
@$('select.subject').toggle(toggle_subject_handle)
@$('input.subject_params').toggle(toggle_subject_params)
@$('input.subject_http').toggle(toggle_subject_http)
11 changes: 4 additions & 7 deletions app/coffeescripts/views/link_lists/links/new.coffee
Expand Up @@ -60,12 +60,9 @@ App.Views.LinkList.Links.New = Backbone.View.extend

# private
get_url: -> # 设置http值
url = @$("input[name='url']").val()
return url if url
switch @$('.selector').val()
when 'blog' then "/blogs/#{@$('select.subject').val()}"
link_type = @$('.selector').val()
switch link_type
when 'blog', 'collection', 'page', 'product' then "/#{link_type}s/#{@$('select.subject').val()}"
when 'frontpage' then "/"
when 'collection' then "/collections/#{@$('select.subject').val()}"
when 'page' then "/pages/#{@$('select.subject').val()}"
when 'product' then "/products/#{@$('select.subject').val()}"
when 'search' then "/search"
when 'http' then @$("input[name='url']").val()
2 changes: 1 addition & 1 deletion app/controllers/products_controller.rb
Expand Up @@ -99,7 +99,7 @@ def set
def duplicate
new_product = product.clone
new_product.variants = product.variants.map(&:clone)
new_product.options = product.options.map(&:clone)
new_product.options = product.options.map(&:clone).each{|o| o.position = nil } # Fixed: #159 position不为空导致排序报错
new_product.collection_products = product.collection_products.map(&:clone)
new_product.tags_text = product.tags_text
new_product.update_attribute :title, params[:new_title]
Expand Down
1 change: 1 addition & 0 deletions app/models/link_list.rb
Expand Up @@ -7,6 +7,7 @@ class LinkList < ActiveRecord::Base

before_save do
self.title = self.title.blank? ? '未命名' : self.title
self.handle = Pinyin.t(self.title, '-') if self.handle.blank?
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/views/link_lists/index.html.haml
Expand Up @@ -117,10 +117,10 @@
%a.delete.fr(href="#")=image_tag 'admin/icons/trash.gif'
%input(name="id" type="hidden" value="{{{id}}}")
%input.medium(name="title" size="30" type="text" value="{{{title}}}")
%select.selector.sl(name="link_type")
%select.selector.sl(name="link_type" value="{{{link_type}}}")
=options_for_select link_types
%select.subject(name="subject_handle" style="display: none")
%input.medium.subject_params.linkform-hint.hide(data-hint="根据标签过滤(以逗号分隔)" name="subject_params" size="30" type="text")
%select.subject(name="subject_handle" style="display: none" value="{{{subject_handle}}}")
%input.medium.subject_params.linkform-hint.hide(data-hint="根据标签过滤(以逗号分隔)" name="subject_params" size="30" type="text" value="{{{subject_params}}}")
%input.long.subject_http.linkform-hint.hide(data-hint="http://shopify.com" name="url" size="30" type="text" value="{{{url}}}")
%script(id="selector-blog-item" type="text/x-jquery-tmpl")
=options_for_select blog_types
Expand Down
20 changes: 17 additions & 3 deletions spec/controllers/products_controller_spec.rb
Expand Up @@ -8,21 +8,35 @@

let(:shop) { user.shop }

let(:iphone4) {Factory.build(:iphone4)}
let(:iphone4) {Factory.build(:iphone4, shop: shop)}

before :each do
before :each do
request.host = "#{shop.primary_domain.host}"
sign_in(user)
end

context '#create' do
it "should create new product " do

it "should create new product" do
attrs = iphone4.attributes
attrs.delete('shop_id')
post :create ,:product => attrs
response.should be_redirect
flash[:notice].should == "新增商品成功!"
end

end

context '#duplicate' do

it "should be copy" do
iphone4.save
expect do
post :duplicate , id: iphone4.id, new_title: 'iphone5'
response.should be_success
end.should change(Product, :count).by(1)
end

end

end
2 changes: 1 addition & 1 deletion spec/factories/links.rb
Expand Up @@ -5,5 +5,5 @@
f.link_type "http"
f.subject_handle nil
f.subject_params nil
f.subject "http://shopqi.com"
f.url "http://shopqi.com"
end
2 changes: 1 addition & 1 deletion spec/liquids/tag_filter_spec.rb
Expand Up @@ -23,7 +23,7 @@
end

it 'should get link_to' do
link = LinkDrop.new link_list.links.build title: '查询', link_type: 'search'
link = LinkDrop.new link_list.links.build title: '查询', link_type: 'search', url: '/search'
variant = "{{ link.title | escape | link_to: link.url }}"
Liquid::Template.parse(variant).render('link' => link).should eql "<a href='/search'>查询</a>"
end
Expand Down
72 changes: 5 additions & 67 deletions spec/models/link_list_spec.rb
Expand Up @@ -7,77 +7,15 @@

let(:link_list) { Factory :link_list, shop: shop }

let(:iphone4) { Factory :iphone4, shop: shop, product_type: '智能手机', vendor: 'Apple', tags_text: 'phone,phone,apple' }

let(:collection) { Factory :custom_collection, shop: shop }

let(:about_us) { Factory 'about-us', shop: shop }

let(:welcome) { Factory :welcome, shop: shop }

describe Link do

context '(blog)' do

it 'should get url' do
link = link_list.links.build title: '博客', link_type: 'blog', subject_handle: welcome.id.to_s
link.url.should eql "/blogs/#{welcome.handle}"
end

end

context '(frontpage)' do

it 'should get url' do
link = link_list.links.build title: '首页', link_type: 'frontpage'
link.url.should eql '/'
end

end

context '(collection)' do

it 'should get url' do
link = link_list.links.build title: '商品列表', link_type: 'collection', subject_handle: collection.id.to_s
link.url.should eql "/collections/#{collection.handle}"
end

end

context '(page)' do

it 'should get url' do
link = link_list.links.build title: '关于我们', link_type: 'page', subject_handle: about_us.id.to_s
link.url.should eql "/pages/#{about_us.handle}"
end

end

context '(product)' do

it 'should get url' do
link = link_list.links.build title: 'iphone', link_type: 'product', subject_handle: iphone4.id.to_s
link.url.should eql "/products/#{iphone4.handle}"
end

end

context '(search)' do

it 'should get url' do
link = link_list.links.build title: '查询', link_type: 'search'
link.url.should eql "/search"
end

end

context '(http)' do

it 'should get url' do
link = link_list.links.build title: '其他', link_type: 'http', subject: 'http://shopqi.com'
link.url.should eql "http://shopqi.com"
end

it 'should be save' do
welcome
expect do
link_list.links.create title: '博客', link_type: 'blog', subject_handle: welcome.handle, url: "/blogs/#{welcome.handle}"
end.should change(Link, :count).by(1)
end

end
Expand Down

0 comments on commit 3e5563c

Please sign in to comment.