From 46fd3cac0fcdb9f9b9a0378eaf5517f618486967 Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Wed, 22 Jan 2014 18:18:27 -0300 Subject: [PATCH] Customize template with use_custom_design --- app/controllers/application_controller.rb | 10 +--------- app/helpers/boxes_helper.rb | 32 ++++++++++++++++++++++++++------ app/helpers/design_helper.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/acts_as_having_boxes.rb | 5 +++-- test/unit/boxes_helper_test.rb | 38 ++++++++++++++++++++++++++++++++++++-- test/unit/design_helper_test.rb | 20 ++++++++++++++++++++ 6 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 app/helpers/design_helper.rb create mode 100644 test/unit/design_helper_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 49c7f04..63d02ad 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -59,15 +59,7 @@ class ApplicationController < ActionController::Base helper :document helper :language - def self.no_design_blocks - @no_design_blocks = true - end - def self.uses_design_blocks? - !@no_design_blocks - end - def uses_design_blocks? - !@no_design_blocks && self.class.uses_design_blocks? - end + include DesignHelper # Be sure to include AuthenticationSystem in Application Controller instead include AuthenticatedSystem diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index a35cf7d..41ad03f 100644 --- a/app/helpers/boxes_helper.rb +++ b/app/helpers/boxes_helper.rb @@ -38,8 +38,12 @@ module BoxesHelper end end + def boxes_limit holder + controller.send(:custom_design)[:boxes_limit] || holder.boxes_limit(controller.send(:custom_design)[:layout_template]) + end + def display_boxes(holder, main_content) - boxes = holder.boxes.with_position.first(holder.boxes_limit) + boxes = holder.boxes.with_position.first(boxes_limit(holder)) content = boxes.reverse.map { |item| display_box(item, main_content) }.join("\n") content = main_content if (content.blank?) @@ -65,11 +69,13 @@ module BoxesHelper end def display_box_content(box, main_content) - context = { :article => @page, :request_path => request.path, :locale => locale, :params => request.params, :user => user } - box_decorator.select_blocks(box.blocks.includes(:box), context).map { |item| display_block(item, main_content) }.join("\n") + box_decorator.block_target(box) + context = { :article => @page, :request_path => request.path, :locale => locale, :params => request.params, :user => user, :controller => controller } + box_decorator.select_blocks(box, box.blocks.includes(:box), context).map do |item| + display_block item, main_content + end.join("\n") + box_decorator.block_target(box) end - def select_blocks(arr, context) + def select_blocks box, arr, context arr end @@ -150,8 +156,22 @@ module BoxesHelper def self.block_edit_buttons(block) '' end - def self.select_blocks(arr, context) - arr.select { |block| block.visible?(context) } + def self.select_blocks box, arr, context + arr = arr.select{ |block| block.visible? context } + + custom_design = context[:controller].send(:custom_design) + inserts = [custom_design[:insert]].flatten.compact + inserts.each do |insert_opts| + next unless box.position == insert_opts[:box] + position, block = insert_opts[:position], insert_opts[:block] + block = block.new box: box if block.is_a? Class + + if not insert_opts[:uniq] or not box.blocks.map(&:class).include? block.klass + arr = arr.insert position, block + end + end + + arr end end diff --git a/app/helpers/design_helper.rb b/app/helpers/design_helper.rb new file mode 100644 index 0000000..9c78710 --- /dev/null +++ b/app/helpers/design_helper.rb @@ -0,0 +1,50 @@ +module DesignHelper + + extend ActiveSupport::Concern + + included do + extend ClassMethods + include InstanceMethods + before_filter :load_custom_design if self.respond_to? :before_filter + end + + module ClassMethods + + def no_design_blocks + @no_design_blocks = true + end + + def use_custom_design options = {} + @custom_design = options + end + + def custom_design + @custom_design ||= {} + end + + def uses_design_blocks? + !@no_design_blocks + end + + end + + module InstanceMethods + + protected + + def uses_design_blocks? + !@no_design_blocks && self.class.uses_design_blocks? + end + + def load_custom_design + # see also: LayoutHelper#body_classes + @layout_template = self.class.custom_design[:layout_template] + end + + def custom_design + @custom_design || self.class.custom_design + end + + end + +end diff --git a/lib/acts_as_having_boxes.rb b/lib/acts_as_having_boxes.rb index a6a3c06..77a82cd 100644 --- a/lib/acts_as_having_boxes.rb +++ b/lib/acts_as_having_boxes.rb @@ -27,8 +27,9 @@ module ActsAsHavingBoxes end # returns 3 unless the class table has a boxes_limit column. In that case - # return the value of the column. - def boxes_limit + # return the value of the column. + def boxes_limit layout_template = nil + layout_template ||= self.layout_template @boxes_limit ||= LayoutTemplate.find(layout_template).number_of_boxes || 3 end diff --git a/test/unit/boxes_helper_test.rb b/test/unit/boxes_helper_test.rb index fb5682e..b7c06fe 100644 --- a/test/unit/boxes_helper_test.rb +++ b/test/unit/boxes_helper_test.rb @@ -1,5 +1,5 @@ require_relative "../test_helper" -require File.dirname(__FILE__) + '/../../app/helpers/boxes_helper' +require 'boxes_helper' class BoxesHelperTest < ActionView::TestCase @@ -7,6 +7,8 @@ class BoxesHelperTest < ActionView::TestCase include ActionView::Helpers::TagHelper def setup + @controller = mock + @controller.stubs(:custom_design).returns({}) @controller.stubs(:boxes_editor?).returns(false) @controller.stubs(:uses_design_blocks?).returns(true) end @@ -115,7 +117,7 @@ class BoxesHelperTest < ActionView::TestCase stubs(:request).returns(request) stubs(:user).returns(nil) expects(:locale).returns('en') - box_decorator.expects(:select_blocks).with([], {:article => nil, :request_path => '/', :locale => 'en', :params => {}, :user => nil}).returns([]) + box_decorator.expects(:select_blocks).with(box, [], {:article => nil, :request_path => '/', :locale => 'en', :params => {}, :user => nil, :controller => @controller}).returns([]) display_box_content(box, '') end @@ -147,4 +149,36 @@ class BoxesHelperTest < ActionView::TestCase assert_equal true, modifiable?(b) end + + should 'consider boxes_limit without custom_design' do + holder = mock + holder.stubs(:boxes_limit).with(nil).returns(2) + assert_equal 2, boxes_limit(holder) + end + + should 'consider boxes_limit with custom_design' do + holder = mock + @controller.expects(:custom_design).returns({boxes_limit: 1}) + + assert_equal 1, boxes_limit(holder) + end + + should 'insert block using custom_design' do + request = mock + request.expects(:path).returns('/') + request.expects(:params).returns({}) + stubs(:request).returns(request) + stubs(:user).returns(nil) + expects(:locale).returns('en') + + box = create(Box, position: 1, owner: fast_create(Profile)) + block = ProfileImageBlock + block.expects(:new).with(box: box) + + @controller.expects(:custom_design).returns({insert: {position: 1, block: block, box: 1}}) + + stubs(:display_block) + display_box_content(box, '') + end + end diff --git a/test/unit/design_helper_test.rb b/test/unit/design_helper_test.rb new file mode 100644 index 0000000..b51ce75 --- /dev/null +++ b/test/unit/design_helper_test.rb @@ -0,0 +1,20 @@ +require_relative "../test_helper" +require 'boxes_helper' + +class DesignHelperTest < ActionView::TestCase + + include DesignHelper + include ActionView::Helpers::TagHelper + + def setup + end + + should 'allow class instance customization of custom design' do + self.class.use_custom_design boxes_limit: 1 + assert_equal({boxes_limit: 1}, self.custom_design) + @custom_design = {boxes_limit: 2} + assert_equal({boxes_limit: 2}, self.custom_design) + + end + +end -- libgit2 0.21.2