diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb index 15fb0a9..82696f3 100644 --- a/app/controllers/my_profile/profile_design_controller.rb +++ b/app/controllers/my_profile/profile_design_controller.rb @@ -18,6 +18,11 @@ class ProfileDesignController < BoxOrganizerController blocks << FavoriteEnterprisesBlock end + # blocks exclusive for enterprises + if profile.enterprise? + blocks << ProductsBlock + end + blocks end diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index 13d6b51..fbf11a4 100644 --- a/app/helpers/boxes_helper.rb +++ b/app/helpers/boxes_helper.rb @@ -121,12 +121,20 @@ module BoxesHelper def block_edit_buttons(block) buttons = [] - buttons << icon_button(:up, _('Move block up'), { :action => 'move_block_up', :id => block.id }, { :method => 'post' }) unless block.first? - buttons << icon_button(:down, _('Move block down'), { :action => 'move_block_down' ,:id => block.id }, { :method => 'post'}) unless block.last? - buttons << icon_button(:delete, _('Remove block'), { :action => 'remove', :id => block.id }, { :method => 'post'}) unless block.main? + if !block.first? + buttons << icon_button(:up, _('Move block up'), { :action => 'move_block_up', :id => block.id }, { :method => 'post' }) + end + + if !block.last? + buttons << icon_button(:down, _('Move block down'), { :action => 'move_block_down' ,:id => block.id }, { :method => 'post'}) + end + + if block.editable? + buttons << lightbox_icon_button(:edit, _('Edit'), { :controller => 'profile_design', :action => 'edit', :id => block.id }) + end - if block.editor - buttons << lightbox_button(:edit, _('Edit'), block.editor) + if !block.main? + buttons << icon_button(:delete, _('Remove block'), { :action => 'remove', :id => block.id }, { :method => 'post'}) end content_tag('div', buttons.join("\n") + tag('br', :style => 'clear: left'), :class => 'button-bar') diff --git a/app/helpers/lightbox_helper.rb b/app/helpers/lightbox_helper.rb index 5a23acd..ea30104 100644 --- a/app/helpers/lightbox_helper.rb +++ b/app/helpers/lightbox_helper.rb @@ -16,6 +16,10 @@ module LightboxHelper button(type, label, url, lightbox_options(options)) end + def lightbox_icon_button(type, label, url, options = {}) + icon_button(type, label, url, lightbox_options(options)) + end + # options must be an HTML options hash as passed to link_to etc. # # returns a new hash with lightbox class added. Keeps existing classes. diff --git a/app/models/article_block.rb b/app/models/article_block.rb index 1761af9..8c41a3a 100644 --- a/app/models/article_block.rb +++ b/app/models/article_block.rb @@ -36,8 +36,8 @@ class ArticleBlock < Block @article = obj end - def editor - { :controller => 'profile_design', :action => 'edit', :id => self.id } + def editable? + true end end diff --git a/app/models/block.rb b/app/models/block.rb index aa7bd83..90447bc 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -45,10 +45,9 @@ class Block < ActiveRecord::Base nil end - # must return a Hash with URL options poiting to the action that edits - # properties of the block - def editor - nil + # Is this block editable? (Default to false) + def editable? + false end # must always return false, except on MainBlock clas. diff --git a/app/models/products_block.rb b/app/models/products_block.rb index 11f1aa6..bc6be5e 100644 --- a/app/models/products_block.rb +++ b/app/models/products_block.rb @@ -8,15 +8,21 @@ class ProductsBlock < Block block_title(_('Products')) + content_tag( 'ul', - owner.products.map {|product| content_tag('li', link_to(product.name, product.url, :style => 'background-image:url(%s)' % ( product.image ? product.image.public_filename(:minor) : '/images/icons-app/product-default-pic-minor.png' )), :class => 'product' )} + products.map {|product| content_tag('li', link_to(product.name, product.url, :style => 'background-image:url(%s)' % ( product.image ? product.image.public_filename(:minor) : '/images/icons-app/product-default-pic-minor.png' )), :class => 'product' )} ) end def footer - link_to(_('View all'), owner.generate_url(:controller => 'catalog', :action => 'index')) + link_to(_('View all products'), owner.generate_url(:controller => 'catalog', :action => 'index')) end settings_items :product_ids, Array + def product_ids=(array) + self.settings[:product_ids] = array + if self.settings[:product_ids] + self.settings[:product_ids] = self.settings[:product_ids].map(&:to_i) + end + end def products(reload = false) if product_ids.blank? @@ -31,4 +37,8 @@ class ProductsBlock < Block end end + def editable? + true + end + end diff --git a/app/views/box_organizer/_products_block.rhtml b/app/views/box_organizer/_products_block.rhtml new file mode 100644 index 0000000..dfaedd3 --- /dev/null +++ b/app/views/box_organizer/_products_block.rhtml @@ -0,0 +1,13 @@ + + +

+ +<%= _('Select the products that must be shown.') %> + +

+ +<% for product in @block.owner.products %> + <%= check_box_tag("block[product_ids][]", product.id, (!@block.product_ids.blank? && @block.product_ids.include?(product.id)), :id => "block_product_ids_#{product.id}" ) %> + +
+<% end %> diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index 733c20b..a08be0e 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -32,6 +32,8 @@ profile_admin: key: 'profile_admin' name: 'Profile Administrator' system: true + permissions: + - edit_profile_design profile_member: id: 6 key: 'profile_member' diff --git a/test/functional/profile_design_controller_test.rb b/test/functional/profile_design_controller_test.rb index 308726a..750ddff 100644 --- a/test/functional/profile_design_controller_test.rb +++ b/test/functional/profile_design_controller_test.rb @@ -5,12 +5,13 @@ class ProfileDesignController; def rescue_action(e) raise e end; end class ProfileDesignControllerTest < Test::Unit::TestCase + attr_reader :holder def setup @controller = ProfileDesignController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new - holder = create_user('designtestuser').person + @holder = create_user('designtestuser').person holder.save! @box1 = Box.new @@ -59,11 +60,11 @@ class ProfileDesignControllerTest < Test::Unit::TestCase @request.env['HTTP_REFERER'] = '/editor' @controller.stubs(:boxes_holder).returns(holder) - login_as 'ze' + login_as 'designtestuser' end def test_local_files_reference - assert_local_files_reference :get, :index, :profile => 'ze' + assert_local_files_reference :get, :index, :profile => 'designtestuser' end def test_valid_xhtml @@ -74,7 +75,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase # BEGIN - tests for BoxOrganizerController features ###################################################### def test_should_move_block_to_the_end_of_another_block - get :move_block, :profile => 'ze', :id => "block-#{@b1.id}", :target => "end-of-box-#{@box2.id}" + get :move_block, :profile => 'designtestuser', :id => "block-#{@b1.id}", :target => "end-of-box-#{@box2.id}" assert_response :success @@ -88,7 +89,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase def test_should_move_block_to_the_middle_of_another_block # block 4 is in box 2 - get :move_block, :profile => 'ze', :id => "block-#{@b1.id}", :target => "before-block-#{@b4.id}" + get :move_block, :profile => 'designtestuser', :id => "block-#{@b1.id}", :target => "before-block-#{@b4.id}" assert_response :success @@ -101,7 +102,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase end def test_block_can_be_moved_up - get :move_block, :profile => 'ze', :id => "block-#{@b4.id}", :target => "before-block-#{@b3.id}" + get :move_block, :profile => 'designtestuser', :id => "block-#{@b4.id}", :target => "before-block-#{@b3.id}" assert_response :success @b4.reload @@ -114,7 +115,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase assert_equal [1,2,3], [@b3,@b4,@b5].map {|item| item.position} # b3 -> before b5 - get :move_block, :profile => 'ze', :id => "block-#{@b3.id}", :target => "before-block-#{@b5.id}" + get :move_block, :profile => 'designtestuser', :id => "block-#{@b3.id}", :target => "before-block-#{@b5.id}" [@b3,@b4,@b5].each do |item| item.reload @@ -124,7 +125,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase end def test_should_be_able_to_move_block_directly_down - post :move_block_down, :profile => 'ze', :id => @b1.id + post :move_block_down, :profile => 'designtestuser', :id => @b1.id assert_response :redirect @b1.reload @@ -134,7 +135,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase end def test_should_be_able_to_move_block_directly_up - post :move_block_up, :profile => 'ze', :id => @b2.id + post :move_block_up, :profile => 'designtestuser', :id => @b2.id assert_response :redirect @b1.reload @@ -145,7 +146,7 @@ class ProfileDesignControllerTest < Test::Unit::TestCase def test_should_remove_block assert_difference Block, :count, -1 do - post :remove, :profile => 'ze', :id => @b2.id + post :remove, :profile => 'designtestuser', :id => @b2.id assert_response :redirect assert_redirected_to :action => 'index' end @@ -160,14 +161,14 @@ class ProfileDesignControllerTest < Test::Unit::TestCase ###################################################### should 'display popup for adding a new block' do - get :add_block, :profile => 'ze' + get :add_block, :profile => 'designtestuser' assert_response :success assert_no_tag :tag => 'body' # e.g. no layout end should 'actually add a new block' do assert_difference Block, :count do - post :add_block, :profile => 'ze', :box_id => @box1.id, :type => RecentDocumentsBlock.name + post :add_block, :profile => 'designtestuser', :box_id => @box1.id, :type => RecentDocumentsBlock.name assert_redirected_to :action => 'index' end end @@ -175,19 +176,19 @@ class ProfileDesignControllerTest < Test::Unit::TestCase should 'not allow to create unknown types' do assert_no_difference Block, :count do assert_raise ArgumentError do - post :add_block, :profile => 'ze', :box_id => @box1.id, :type => "PleaseLetMeCrackYourSite" + post :add_block, :profile => 'designtestuser', :box_id => @box1.id, :type => "PleaseLetMeCrackYourSite" end end end should 'provide edit screen for blocks' do - get :edit, :profile => 'ze', :id => @b1.id + get :edit, :profile => 'designtestuser', :id => @b1.id assert_template 'edit' assert_no_tag :tag => 'body' # e.g. no layout end should 'be able to save a block' do - post :save, :profile => 'ze', :id => @b1.id, :block => { :article_id => 999 } + post :save, :profile => 'designtestuser', :id => @b1.id, :block => { :article_id => 999 } assert_redirected_to :action => 'index' @@ -195,6 +196,45 @@ class ProfileDesignControllerTest < Test::Unit::TestCase assert_equal 999, @b1.article_id end + should 'be able to edit ProductsBlock' do + block = ProductsBlock.new + + enterprise = Enterprise.create!(:name => "test", :identifier => 'testenterprise') + p1 = enterprise.products.create!(:name => 'product one') + p2 = enterprise.products.create!(:name => 'product two') + enterprise.boxes.first.blocks << block + enterprise.add_admin(holder) + + @controller.stubs(:boxes_holder).returns(enterprise) + login_as('designtestuser') + + get :edit, :profile => 'testenterprise', :id => block.id + + assert_response :success + assert_tag :tag => 'input', :attributes => { :name => "block[product_ids][]", :value => p1.id.to_s } + assert_tag :tag => 'input', :attributes => { :name => "block[product_ids][]", :value => p2.id.to_s } + end + + should 'be able to save ProductsBlock' do + block = ProductsBlock.new + + enterprise = Enterprise.create!(:name => "test", :identifier => 'testenterprise') + p1 = enterprise.products.create!(:name => 'product one') + p2 = enterprise.products.create!(:name => 'product two') + enterprise.boxes.first.blocks << block + enterprise.add_admin(holder) + + @controller.stubs(:boxes_holder).returns(enterprise) + login_as('designtestuser') + + post :save, :profile => 'testenterprise', :id => block.id, :block => { :product_ids => [p1.id.to_s, p2.id.to_s ] } + + assert_response :redirect + + block.reload + assert_equal [p1.id, p2.id], block.product_ids + + end end diff --git a/test/unit/article_block_test.rb b/test/unit/article_block_test.rb index e494b1f..ce2d3d7 100644 --- a/test/unit/article_block_test.rb +++ b/test/unit/article_block_test.rb @@ -52,4 +52,8 @@ class ArticleBlockTest < Test::Unit::TestCase assert_nil block.article_id end + should 'be editable' do + assert ArticleBlock.new.editable? + end + end diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index 46d977b..5fd67d2 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -32,4 +32,8 @@ class BlockTest < Test::Unit::TestCase assert_nil Block.new.footer end + should 'not be editable by default' do + assert !Block.new.editable? + end + end diff --git a/test/unit/lightbox_helper_test.rb b/test/unit/lightbox_helper_test.rb index c6fea44..3afe8e4 100644 --- a/test/unit/lightbox_helper_test.rb +++ b/test/unit/lightbox_helper_test.rb @@ -44,6 +44,12 @@ class LightboxHelperTest < Test::Unit::TestCase assert_equal '[button]', lightbox_button('type', 'label', { :action => 'popup'}) end + should 'provide lightbox_icon_button' do + expects(:icon_button).with('type', 'label', { :action => 'popup'}, { :class => 'lbOn' }).returns('[button]') + + assert_equal '[button]', lightbox_icon_button('type', 'label', { :action => 'popup'}) + end + should 'tell if rendering inside lightbox' do request = mock expects(:request).returns(request) diff --git a/test/unit/products_block_test.rb b/test/unit/products_block_test.rb index 36927d3..6737657 100644 --- a/test/unit/products_block_test.rb +++ b/test/unit/products_block_test.rb @@ -11,14 +11,13 @@ class ProductsBlockTest < ActiveSupport::TestCase assert_kind_of Block, block end - should "list owner's products" do + should "list owner products" do enterprise = Enterprise.create!(:name => 'testenterprise', :identifier => 'testenterprise') enterprise.products.create!(:name => 'product one') enterprise.products.create!(:name => 'product two') - block.stubs(:owner).returns(enterprise) - + block.expects(:products).returns(enterprise.products) content = block.content @@ -38,7 +37,7 @@ class ProductsBlockTest < ActiveSupport::TestCase footer = block.footer - assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all' + assert_tag_in_string footer, :tag => 'a', :attributes => { :href => /\/catalog\/testenterprise$/ }, :content => 'View all products' end should 'list 4 random products by default' do @@ -94,4 +93,14 @@ class ProductsBlockTest < ActiveSupport::TestCase assert_equal [p1.id, p2.id], ProductsBlock.find(block.id).product_ids end + should 'accept strings in product_ids but store integers' do + block = ProductsBlock.new + block.product_ids = [ '1', '2'] + assert_equal [1, 2], block.product_ids + end + + should 'be editable' do + assert ProductsBlock.new.editable? + end + end -- libgit2 0.21.2