noosfero | 2 new commits pushed to repository

Bráulio Bhavamitra gitlab at gitlab.com
Tue Feb 10 16:56:27 BRST 2015


Bráulio Bhavamitra pushed to refs/heads/master at <a href="https://gitlab.com/noosfero/noosfero">Noosfero / noosfero</a>

Commits:
<a href="https://gitlab.com/noosfero/noosfero/commit/46fd3cac0fcdb9f9b9a0378eaf5517f618486967">46fd3cac</a> by Braulio Bhavamitra
Customize template with use_custom_design

- - - - -
<a href="https://gitlab.com/noosfero/noosfero/commit/b403cda3864452b060296609a8b30e4bb51ea879">b403cda3</a> by Bráulio Bhavamitra
Merge branch 'ai896' into 'master'

Customize template on controllers with use_custom_design

See merge request !93

- - - - -


Changes:

=====================================
app/controllers/application_controller.rb
=====================================
--- 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

=====================================
app/helpers/boxes_helper.rb
=====================================
--- 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
 

=====================================
app/helpers/design_helper.rb
=====================================
--- /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

=====================================
lib/acts_as_having_boxes.rb
=====================================
--- 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
 

=====================================
test/unit/boxes_helper_test.rb
=====================================
--- 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

=====================================
test/unit/design_helper_test.rb
=====================================
--- /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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150210/b77e2a5f/attachment-0001.html>


More information about the Noosfero-dev mailing list