Commit 30ffafc5518e52d8757150008e074bde3a8bb23f
Exists in
master
and in
29 other branches
Merge branch 'block_edit_modes' into next
Conflicts: test/unit/boxes_helper_test.rb
Showing
7 changed files
with
104 additions
and
33 deletions
Show diff stats
app/controllers/my_profile/profile_design_controller.rb
... | ... | @@ -4,11 +4,19 @@ class ProfileDesignController < BoxOrganizerController |
4 | 4 | |
5 | 5 | protect 'edit_profile_design', :profile |
6 | 6 | |
7 | - before_filter :protect_fixed_block, :only => [:save, :move_block] | |
7 | + before_filter :protect_uneditable_block, :only => [:save] | |
8 | + before_filter :protect_fixed_block, :only => [:move_block] | |
9 | + | |
10 | + def protect_uneditable_block | |
11 | + block = boxes_holder.blocks.find(params[:id].gsub(/^block-/, '')) | |
12 | + if !current_person.is_admin? && !block.editable? | |
13 | + render_access_denied | |
14 | + end | |
15 | + end | |
8 | 16 | |
9 | 17 | def protect_fixed_block |
10 | 18 | block = boxes_holder.blocks.find(params[:id].gsub(/^block-/, '')) |
11 | - if block.fixed && !current_person.is_admin? | |
19 | + if !current_person.is_admin? && !block.movable? | |
12 | 20 | render_access_denied |
13 | 21 | end |
14 | 22 | end | ... | ... |
app/helpers/boxes_helper.rb
... | ... | @@ -190,7 +190,7 @@ module BoxesHelper |
190 | 190 | else |
191 | 191 | "before-block-#{block.id}" |
192 | 192 | end |
193 | - if block.nil? or modifiable?(block) | |
193 | + if block.nil? || movable?(block) | |
194 | 194 | content_tag('div', ' ', :id => id, :class => 'block-target' ) + drop_receiving_element(id, :url => { :action => 'move_block', :target => id }, :accept => box.acceptable_blocks, :hoverclass => 'block-target-hover') |
195 | 195 | else |
196 | 196 | "" |
... | ... | @@ -199,14 +199,14 @@ module BoxesHelper |
199 | 199 | |
200 | 200 | # makes the given block draggable so it can be moved away. |
201 | 201 | def block_handle(block) |
202 | - modifiable?(block) ? draggable_element("block-#{block.id}", :revert => true) : "" | |
202 | + movable?(block) ? draggable_element("block-#{block.id}", :revert => true) : "" | |
203 | 203 | end |
204 | 204 | |
205 | 205 | def block_edit_buttons(block) |
206 | 206 | buttons = [] |
207 | 207 | nowhere = 'javascript: return false;' |
208 | 208 | |
209 | - if modifiable?(block) | |
209 | + if movable?(block) | |
210 | 210 | if block.first? |
211 | 211 | buttons << icon_button('up-disabled', _("Can't move up anymore."), nowhere) |
212 | 212 | else |
... | ... | @@ -229,15 +229,15 @@ module BoxesHelper |
229 | 229 | buttons << icon_button('left', _('Move to the opposite side'), { :action => 'move_block', :target => 'end-of-box-' + holder.boxes[1].id.to_s, :id => block.id }, :method => 'post' ) |
230 | 230 | end |
231 | 231 | end |
232 | + end | |
232 | 233 | |
233 | - if block.editable? | |
234 | - buttons << modal_icon_button(:edit, _('Edit'), { :action => 'edit', :id => block.id }) | |
235 | - end | |
234 | + if editable?(block) | |
235 | + buttons << modal_icon_button(:edit, _('Edit'), { :action => 'edit', :id => block.id }) | |
236 | + end | |
236 | 237 | |
237 | - if !block.main? | |
238 | - buttons << icon_button(:delete, _('Remove block'), { :action => 'remove', :id => block.id }, { :method => 'post', :confirm => _('Are you sure you want to remove this block?')}) | |
239 | - buttons << icon_button(:clone, _('Clone'), { :action => 'clone_block', :id => block.id }, { :method => 'post' }) | |
240 | - end | |
238 | + if movable?(block) && !block.main? | |
239 | + buttons << icon_button(:delete, _('Remove block'), { :action => 'remove', :id => block.id }, { :method => 'post', :confirm => _('Are you sure you want to remove this block?')}) | |
240 | + buttons << icon_button(:clone, _('Clone'), { :action => 'clone_block', :id => block.id }, { :method => 'post' }) | |
241 | 241 | end |
242 | 242 | |
243 | 243 | if block.respond_to?(:help) |
... | ... | @@ -273,7 +273,11 @@ module BoxesHelper |
273 | 273 | classes |
274 | 274 | end |
275 | 275 | |
276 | - def modifiable?(block) | |
277 | - return !block.fixed || environment.admins.include?(user) | |
276 | + def movable?(block) | |
277 | + return block.movable? || user.is_admin? | |
278 | + end | |
279 | + | |
280 | + def editable?(block) | |
281 | + return block.editable? || user.is_admin? | |
278 | 282 | end |
279 | 283 | end | ... | ... |
app/models/block.rb
1 | 1 | class Block < ActiveRecord::Base |
2 | 2 | |
3 | - attr_accessible :title, :display, :limit, :box_id, :posts_per_page, :visualization_format, :language, :display_user, :box, :fixed | |
3 | + attr_accessible :title, :display, :limit, :box_id, :posts_per_page, | |
4 | + :visualization_format, :language, :display_user, | |
5 | + :box, :edit_modes, :move_modes | |
4 | 6 | |
5 | 7 | # to be able to generate HTML |
6 | 8 | include ActionView::Helpers::UrlHelper |
... | ... | @@ -110,8 +112,13 @@ class Block < ActiveRecord::Base |
110 | 112 | # * <tt>'all'</tt>: the block is always displayed |
111 | 113 | settings_items :language, :type => :string, :default => 'all' |
112 | 114 | |
113 | - # The block can be configured to be fixed. Only can be edited by environment admins | |
114 | - settings_items :fixed, :type => :boolean, :default => false | |
115 | + # The block can be configured to define the edition modes options. Only can be edited by environment admins | |
116 | + # It can assume the following values: | |
117 | + # | |
118 | + # * <tt>'all'</tt>: the block owner has all edit options for this block | |
119 | + # * <tt>'none'</tt>: the block owner can't do anything with the block | |
120 | + settings_items :edit_modes, :type => :string, :default => 'all' | |
121 | + settings_items :move_modes, :type => :string, :default => 'all' | |
115 | 122 | |
116 | 123 | # returns the description of the block, used when the user sees a list of |
117 | 124 | # blocks to choose one to include in the design. |
... | ... | @@ -148,7 +155,11 @@ class Block < ActiveRecord::Base |
148 | 155 | |
149 | 156 | # Is this block editable? (Default to <tt>false</tt>) |
150 | 157 | def editable? |
151 | - true | |
158 | + self.edit_modes == "all" | |
159 | + end | |
160 | + | |
161 | + def movable? | |
162 | + self.move_modes == "all" | |
152 | 163 | end |
153 | 164 | |
154 | 165 | # must always return false, except on MainBlock clas. |
... | ... | @@ -228,6 +239,21 @@ class Block < ActiveRecord::Base |
228 | 239 | } |
229 | 240 | end |
230 | 241 | |
242 | + def edit_block_options | |
243 | + @edit_options ||= { | |
244 | + 'all' => _('Can be modified'), | |
245 | + 'none' => _('Cannot be modified') | |
246 | + } | |
247 | + end | |
248 | + | |
249 | + def move_block_options | |
250 | + @move_options ||= { | |
251 | + 'all' => _('Can be moved'), | |
252 | + 'none' => _('Cannot be moved') | |
253 | + } | |
254 | + end | |
255 | + | |
256 | + | |
231 | 257 | def duplicate |
232 | 258 | duplicated_block = self.dup |
233 | 259 | duplicated_block.display = 'never' | ... | ... |
app/views/box_organizer/edit.html.erb
... | ... | @@ -5,12 +5,6 @@ |
5 | 5 | |
6 | 6 | <%= labelled_form_field(_('Custom title for this block: '), text_field(:block, :title, :maxlength => 20)) %> |
7 | 7 | |
8 | - <% if environment.admins.include?(user) %> | |
9 | - <div class="fixed_block"> | |
10 | - <%= labelled_check_box(_("Fixed"), "block[fixed]", value = "1", checked = @block.fixed) %> | |
11 | - </div> | |
12 | - <% end %> | |
13 | - | |
14 | 8 | <%= render :partial => partial_for_class(@block.class) %> |
15 | 9 | |
16 | 10 | <div class="display"> |
... | ... | @@ -25,6 +19,15 @@ |
25 | 19 | |
26 | 20 | <%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + environment.locales.map {|key, value| [value, key]} )) %> |
27 | 21 | |
22 | + <% if user.is_admin? %> | |
23 | + <div class="edit-modes"> | |
24 | + <%= labelled_form_field _('Edit options:'), select_tag('block[edit_modes]', options_from_collection_for_select(@block.edit_block_options, :first, :last, @block.edit_modes)) %> | |
25 | + </div> | |
26 | + <div class="move-modes"> | |
27 | + <%= labelled_form_field _('Move options:'), select_tag('block[move_modes]', options_from_collection_for_select(@block.move_block_options, :first, :last, @block.move_modes)) %> | |
28 | + </div> | |
29 | + <% end %> | |
30 | + | |
28 | 31 | <% button_bar do %> |
29 | 32 | <%= submit_button(:save, _('Save')) %> |
30 | 33 | <%= modal_close_button(_('Cancel')) %> | ... | ... |
test/functional/profile_design_controller_test.rb
... | ... | @@ -737,9 +737,9 @@ class ProfileDesignControllerTest < ActionController::TestCase |
737 | 737 | end |
738 | 738 | end |
739 | 739 | |
740 | - test 'should forbid POST to save for fixed blocks' do | |
740 | + test 'should forbid POST to save for uneditable blocks' do | |
741 | 741 | block = profile.blocks.last |
742 | - block.fixed = true | |
742 | + block.edit_modes = "none" | |
743 | 743 | block.save! |
744 | 744 | |
745 | 745 | post :save, id: block.id, profile: profile.identifier |
... | ... | @@ -748,7 +748,7 @@ class ProfileDesignControllerTest < ActionController::TestCase |
748 | 748 | |
749 | 749 | test 'should forbid POST to move_block for fixed blocks' do |
750 | 750 | block = profile.blocks.last |
751 | - block.fixed = true | |
751 | + block.move_modes = "none" | |
752 | 752 | block.save! |
753 | 753 | |
754 | 754 | post :move_block, id: block.id, profile: profile.identifier, target: "end-of-box-#{@box3.id}" | ... | ... |
test/unit/block_test.rb
... | ... | @@ -35,6 +35,24 @@ class BlockTest < ActiveSupport::TestCase |
35 | 35 | assert Block.new.editable? |
36 | 36 | end |
37 | 37 | |
38 | + should 'be editable if edit modes is all' do | |
39 | + block = Block.new | |
40 | + block.edit_modes = 'all' | |
41 | + | |
42 | + assert block.editable? | |
43 | + end | |
44 | + | |
45 | + should 'be movable by default' do | |
46 | + assert Block.new.movable? | |
47 | + end | |
48 | + | |
49 | + should 'be movable if move modes is all' do | |
50 | + block = Block.new | |
51 | + block.move_modes = 'all' | |
52 | + | |
53 | + assert block.movable? | |
54 | + end | |
55 | + | |
38 | 56 | should 'have default titles' do |
39 | 57 | b = Block.new |
40 | 58 | b.expects(:default_title).returns('my title') | ... | ... |
test/unit/boxes_helper_test.rb
... | ... | @@ -123,24 +123,24 @@ class BoxesHelperTest < ActionView::TestCase |
123 | 123 | display_box_content(box, '') |
124 | 124 | end |
125 | 125 | |
126 | - should 'not show move options on block when block is fixed' do | |
126 | + should 'not show move options on block when block has no permission to edit' do | |
127 | 127 | p = create_user_with_blocks |
128 | 128 | |
129 | 129 | b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0] |
130 | - b.fixed = true | |
130 | + b.move_modes = "none" | |
131 | 131 | b.save! |
132 | 132 | |
133 | 133 | stubs(:environment).returns(p.environment) |
134 | 134 | stubs(:user).returns(p) |
135 | 135 | |
136 | - assert_equal false, modifiable?(b) | |
136 | + assert_equal false, movable?(b) | |
137 | 137 | end |
138 | 138 | |
139 | - should 'show move options on block when block is fixed and user is admin' do | |
139 | + should 'show move options on block when block has no permission to edit and user is admin' do | |
140 | 140 | p = create_user_with_blocks |
141 | 141 | |
142 | 142 | b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0] |
143 | - b.fixed = true | |
143 | + b.edit_modes = "none" | |
144 | 144 | b.save! |
145 | 145 | |
146 | 146 | p.environment.add_admin(p) |
... | ... | @@ -148,7 +148,7 @@ class BoxesHelperTest < ActionView::TestCase |
148 | 148 | stubs(:environment).returns(p.environment) |
149 | 149 | stubs(:user).returns(p) |
150 | 150 | |
151 | - assert_equal true, modifiable?(b) | |
151 | + assert_equal true, movable?(b) | |
152 | 152 | end |
153 | 153 | |
154 | 154 | should 'consider boxes_limit without custom_design' do |
... | ... | @@ -198,4 +198,16 @@ class BoxesHelperTest < ActionView::TestCase |
198 | 198 | assert_no_tag_in_string block_edit_buttons(block), :tag => 'a', :attributes => {:class => 'button icon-button icon-embed '} |
199 | 199 | end |
200 | 200 | |
201 | + should 'only show edit option on block' do | |
202 | + p = create_user_with_blocks | |
203 | + | |
204 | + b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0] | |
205 | + b.edit_modes = "only_edit" | |
206 | + b.save! | |
207 | + | |
208 | + stubs(:environment).returns(p.environment) | |
209 | + stubs(:user).returns(p) | |
210 | + | |
211 | + assert_equal false, b.editable? | |
212 | + end | |
201 | 213 | end | ... | ... |