Commit 46fd3cac0fcdb9f9b9a0378eaf5517f618486967
1 parent
77aa69cd
Exists in
master
and in
29 other branches
Customize template with use_custom_design
Showing
6 changed files
with
136 additions
and
19 deletions
Show diff stats
app/controllers/application_controller.rb
... | ... | @@ -59,15 +59,7 @@ class ApplicationController < ActionController::Base |
59 | 59 | helper :document |
60 | 60 | helper :language |
61 | 61 | |
62 | - def self.no_design_blocks | |
63 | - @no_design_blocks = true | |
64 | - end | |
65 | - def self.uses_design_blocks? | |
66 | - !@no_design_blocks | |
67 | - end | |
68 | - def uses_design_blocks? | |
69 | - !@no_design_blocks && self.class.uses_design_blocks? | |
70 | - end | |
62 | + include DesignHelper | |
71 | 63 | |
72 | 64 | # Be sure to include AuthenticationSystem in Application Controller instead |
73 | 65 | include AuthenticatedSystem | ... | ... |
app/helpers/boxes_helper.rb
... | ... | @@ -38,8 +38,12 @@ module BoxesHelper |
38 | 38 | end |
39 | 39 | end |
40 | 40 | |
41 | + def boxes_limit holder | |
42 | + controller.send(:custom_design)[:boxes_limit] || holder.boxes_limit(controller.send(:custom_design)[:layout_template]) | |
43 | + end | |
44 | + | |
41 | 45 | def display_boxes(holder, main_content) |
42 | - boxes = holder.boxes.with_position.first(holder.boxes_limit) | |
46 | + boxes = holder.boxes.with_position.first(boxes_limit(holder)) | |
43 | 47 | content = boxes.reverse.map { |item| display_box(item, main_content) }.join("\n") |
44 | 48 | content = main_content if (content.blank?) |
45 | 49 | |
... | ... | @@ -65,11 +69,13 @@ module BoxesHelper |
65 | 69 | end |
66 | 70 | |
67 | 71 | def display_box_content(box, main_content) |
68 | - context = { :article => @page, :request_path => request.path, :locale => locale, :params => request.params, :user => user } | |
69 | - box_decorator.select_blocks(box.blocks.includes(:box), context).map { |item| display_block(item, main_content) }.join("\n") + box_decorator.block_target(box) | |
72 | + context = { :article => @page, :request_path => request.path, :locale => locale, :params => request.params, :user => user, :controller => controller } | |
73 | + box_decorator.select_blocks(box, box.blocks.includes(:box), context).map do |item| | |
74 | + display_block item, main_content | |
75 | + end.join("\n") + box_decorator.block_target(box) | |
70 | 76 | end |
71 | 77 | |
72 | - def select_blocks(arr, context) | |
78 | + def select_blocks box, arr, context | |
73 | 79 | arr |
74 | 80 | end |
75 | 81 | |
... | ... | @@ -150,8 +156,22 @@ module BoxesHelper |
150 | 156 | def self.block_edit_buttons(block) |
151 | 157 | '' |
152 | 158 | end |
153 | - def self.select_blocks(arr, context) | |
154 | - arr.select { |block| block.visible?(context) } | |
159 | + def self.select_blocks box, arr, context | |
160 | + arr = arr.select{ |block| block.visible? context } | |
161 | + | |
162 | + custom_design = context[:controller].send(:custom_design) | |
163 | + inserts = [custom_design[:insert]].flatten.compact | |
164 | + inserts.each do |insert_opts| | |
165 | + next unless box.position == insert_opts[:box] | |
166 | + position, block = insert_opts[:position], insert_opts[:block] | |
167 | + block = block.new box: box if block.is_a? Class | |
168 | + | |
169 | + if not insert_opts[:uniq] or not box.blocks.map(&:class).include? block.klass | |
170 | + arr = arr.insert position, block | |
171 | + end | |
172 | + end | |
173 | + | |
174 | + arr | |
155 | 175 | end |
156 | 176 | end |
157 | 177 | ... | ... |
... | ... | @@ -0,0 +1,50 @@ |
1 | +module DesignHelper | |
2 | + | |
3 | + extend ActiveSupport::Concern | |
4 | + | |
5 | + included do | |
6 | + extend ClassMethods | |
7 | + include InstanceMethods | |
8 | + before_filter :load_custom_design if self.respond_to? :before_filter | |
9 | + end | |
10 | + | |
11 | + module ClassMethods | |
12 | + | |
13 | + def no_design_blocks | |
14 | + @no_design_blocks = true | |
15 | + end | |
16 | + | |
17 | + def use_custom_design options = {} | |
18 | + @custom_design = options | |
19 | + end | |
20 | + | |
21 | + def custom_design | |
22 | + @custom_design ||= {} | |
23 | + end | |
24 | + | |
25 | + def uses_design_blocks? | |
26 | + !@no_design_blocks | |
27 | + end | |
28 | + | |
29 | + end | |
30 | + | |
31 | + module InstanceMethods | |
32 | + | |
33 | + protected | |
34 | + | |
35 | + def uses_design_blocks? | |
36 | + !@no_design_blocks && self.class.uses_design_blocks? | |
37 | + end | |
38 | + | |
39 | + def load_custom_design | |
40 | + # see also: LayoutHelper#body_classes | |
41 | + @layout_template = self.class.custom_design[:layout_template] | |
42 | + end | |
43 | + | |
44 | + def custom_design | |
45 | + @custom_design || self.class.custom_design | |
46 | + end | |
47 | + | |
48 | + end | |
49 | + | |
50 | +end | ... | ... |
lib/acts_as_having_boxes.rb
... | ... | @@ -27,8 +27,9 @@ module ActsAsHavingBoxes |
27 | 27 | end |
28 | 28 | |
29 | 29 | # returns 3 unless the class table has a boxes_limit column. In that case |
30 | - # return the value of the column. | |
31 | - def boxes_limit | |
30 | + # return the value of the column. | |
31 | + def boxes_limit layout_template = nil | |
32 | + layout_template ||= self.layout_template | |
32 | 33 | @boxes_limit ||= LayoutTemplate.find(layout_template).number_of_boxes || 3 |
33 | 34 | end |
34 | 35 | ... | ... |
test/unit/boxes_helper_test.rb
1 | 1 | require_relative "../test_helper" |
2 | -require File.dirname(__FILE__) + '/../../app/helpers/boxes_helper' | |
2 | +require 'boxes_helper' | |
3 | 3 | |
4 | 4 | class BoxesHelperTest < ActionView::TestCase |
5 | 5 | |
... | ... | @@ -7,6 +7,8 @@ class BoxesHelperTest < ActionView::TestCase |
7 | 7 | include ActionView::Helpers::TagHelper |
8 | 8 | |
9 | 9 | def setup |
10 | + @controller = mock | |
11 | + @controller.stubs(:custom_design).returns({}) | |
10 | 12 | @controller.stubs(:boxes_editor?).returns(false) |
11 | 13 | @controller.stubs(:uses_design_blocks?).returns(true) |
12 | 14 | end |
... | ... | @@ -115,7 +117,7 @@ class BoxesHelperTest < ActionView::TestCase |
115 | 117 | stubs(:request).returns(request) |
116 | 118 | stubs(:user).returns(nil) |
117 | 119 | expects(:locale).returns('en') |
118 | - box_decorator.expects(:select_blocks).with([], {:article => nil, :request_path => '/', :locale => 'en', :params => {}, :user => nil}).returns([]) | |
120 | + box_decorator.expects(:select_blocks).with(box, [], {:article => nil, :request_path => '/', :locale => 'en', :params => {}, :user => nil, :controller => @controller}).returns([]) | |
119 | 121 | |
120 | 122 | display_box_content(box, '') |
121 | 123 | end |
... | ... | @@ -147,4 +149,36 @@ class BoxesHelperTest < ActionView::TestCase |
147 | 149 | |
148 | 150 | assert_equal true, modifiable?(b) |
149 | 151 | end |
152 | + | |
153 | + should 'consider boxes_limit without custom_design' do | |
154 | + holder = mock | |
155 | + holder.stubs(:boxes_limit).with(nil).returns(2) | |
156 | + assert_equal 2, boxes_limit(holder) | |
157 | + end | |
158 | + | |
159 | + should 'consider boxes_limit with custom_design' do | |
160 | + holder = mock | |
161 | + @controller.expects(:custom_design).returns({boxes_limit: 1}) | |
162 | + | |
163 | + assert_equal 1, boxes_limit(holder) | |
164 | + end | |
165 | + | |
166 | + should 'insert block using custom_design' do | |
167 | + request = mock | |
168 | + request.expects(:path).returns('/') | |
169 | + request.expects(:params).returns({}) | |
170 | + stubs(:request).returns(request) | |
171 | + stubs(:user).returns(nil) | |
172 | + expects(:locale).returns('en') | |
173 | + | |
174 | + box = create(Box, position: 1, owner: fast_create(Profile)) | |
175 | + block = ProfileImageBlock | |
176 | + block.expects(:new).with(box: box) | |
177 | + | |
178 | + @controller.expects(:custom_design).returns({insert: {position: 1, block: block, box: 1}}) | |
179 | + | |
180 | + stubs(:display_block) | |
181 | + display_box_content(box, '') | |
182 | + end | |
183 | + | |
150 | 184 | end | ... | ... |
... | ... | @@ -0,0 +1,20 @@ |
1 | +require_relative "../test_helper" | |
2 | +require 'boxes_helper' | |
3 | + | |
4 | +class DesignHelperTest < ActionView::TestCase | |
5 | + | |
6 | + include DesignHelper | |
7 | + include ActionView::Helpers::TagHelper | |
8 | + | |
9 | + def setup | |
10 | + end | |
11 | + | |
12 | + should 'allow class instance customization of custom design' do | |
13 | + self.class.use_custom_design boxes_limit: 1 | |
14 | + assert_equal({boxes_limit: 1}, self.custom_design) | |
15 | + @custom_design = {boxes_limit: 2} | |
16 | + assert_equal({boxes_limit: 2}, self.custom_design) | |
17 | + | |
18 | + end | |
19 | + | |
20 | +end | ... | ... |