Commit 969dd1a39e39601c6dc09b844dd86ef0da1b9cf9

Authored by Gabriela Navarro
Committed by Rodrigo Souto
1 parent 4c1e0c9f

Add mirror option for template's blocks. Now if a mirrored block is changed on t…

…he template it is changed on all users that follow that template.

Signed-off-by: Arthur Del Esposte <arthurmde@gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
Signed-off-by: Thiago Ribeiro <thiagitosouza@gmail.com>
app/models/block.rb
... ... @@ -2,7 +2,7 @@ class Block &lt; ActiveRecord::Base
2 2  
3 3 attr_accessible :title, :display, :limit, :box_id, :posts_per_page,
4 4 :visualization_format, :language, :display_user,
5   - :box, :edit_modes, :move_modes
  5 + :box, :edit_modes, :move_modes, :mirror
6 6  
7 7 # to be able to generate HTML
8 8 include ActionView::Helpers::UrlHelper
... ... @@ -15,11 +15,23 @@ class Block &lt; ActiveRecord::Base
15 15  
16 16 acts_as_list :scope => :box
17 17 belongs_to :box
  18 + belongs_to :mirror_block, :class_name => "Block"
  19 + has_many :observers, :class_name => "Block", :foreign_key => "mirror_block_id"
18 20  
19 21 acts_as_having_settings
20 22  
21 23 scope :enabled, :conditions => { :enabled => true }
22 24  
  25 + after_save do |block|
  26 + if block.owner.kind_of?(Profile) && block.owner.is_template? && block.mirror?
  27 + block.observers.each do |observer|
  28 + observer.copy_from(block)
  29 + observer.title = block.title
  30 + observer.save
  31 + end
  32 + end
  33 + end
  34 +
23 35 def embedable?
24 36 false
25 37 end
... ... @@ -269,6 +281,10 @@ class Block &lt; ActiveRecord::Base
269 281 self.position = block.position
270 282 end
271 283  
  284 + def add_observer(block)
  285 + self.observers << block
  286 + end
  287 +
272 288 private
273 289  
274 290 def home_page_path
... ...
app/models/profile.rb
... ... @@ -392,6 +392,9 @@ class Profile &lt; ActiveRecord::Base
392 392 new_block = block.class.new(:title => block[:title])
393 393 new_block.copy_from(block)
394 394 new_box.blocks << new_block
  395 + if block.mirror?
  396 + block.add_observer(new_block)
  397 + end
395 398 end
396 399 end
397 400 end
... ...
app/views/box_organizer/edit.html.erb
... ... @@ -25,6 +25,11 @@
25 25 </div>
26 26 <div class="move-modes">
27 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 + <% end %>
  29 +
  30 + <% if @block.owner.kind_of?(Profile) && @block.owner.is_template? %>
  31 + <div class="mirror_block">
  32 + <%= labelled_check_box(_("Mirror"), "block[mirror]", value = "1", checked = @block.mirror) %>
28 33 </div>
29 34 <% end %>
30 35  
... ...
db/migrate/20150429145001_add_mirror_to_block.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class AddMirrorToBlock < ActiveRecord::Migration
  2 + def up
  3 + change_table :blocks do |t|
  4 + t.boolean :mirror, :default => false
  5 + t.references :mirror_block
  6 + t.references :observers
  7 + end
  8 + end
  9 +
  10 + def down
  11 + remove_column :blocks, :mirror
  12 + remove_column :blocks, :mirror_block_id
  13 + remove_column :blocks, :observers_id
  14 + end
  15 +end
... ...
db/schema.rb
... ... @@ -183,10 +183,13 @@ ActiveRecord::Schema.define(:version =&gt; 20150513213939) do
183 183 t.string "type"
184 184 t.text "settings"
185 185 t.integer "position"
186   - t.boolean "enabled", :default => true
  186 + t.boolean "enabled", :default => true
187 187 t.datetime "created_at"
188 188 t.datetime "updated_at"
189 189 t.datetime "fetched_at"
  190 + t.boolean "mirror", :default => false
  191 + t.integer "mirror_block_id"
  192 + t.integer "observers_id"
190 193 end
191 194  
192 195 add_index "blocks", ["box_id"], :name => "index_blocks_on_box_id"
... ...
features/template_block_management.feature 0 → 100644
... ... @@ -0,0 +1,64 @@
  1 +Feature: user template
  2 + As an user
  3 + I want to create templates with mirror blocks
  4 + In order to keep these blocks always updated
  5 +
  6 + Background:
  7 + Given the following users
  8 + | login | name | is_template |
  9 + | person | person | true |
  10 + And the following blocks
  11 + | owner | type | mirror |
  12 + | person | ArticleBlock | true |
  13 + | person | RawHTMLBlock | false |
  14 + And I go to /account/signup
  15 + And I fill in "Username" with "mario"
  16 + And I fill in "Password" with "123456"
  17 + And I fill in "Password confirmation" with "123456"
  18 + And I fill in "e-Mail" with "mario@mario.com"
  19 + And I fill in "Full name" with "Mario"
  20 + And wait for the captcha signup time
  21 + And I press "Create my account"
  22 + And I am logged in as admin
  23 +
  24 + @selenium
  25 + Scenario: The block Article name is changed
  26 + Given I am on person's control panel
  27 + And I follow "Edit sideboxes"
  28 + And display ".button-bar"
  29 + And I follow "Edit" within ".article-block"
  30 + And I fill in "Custom title for this block:" with "Mirror"
  31 + And I press "Save"
  32 + And I go to /profile/mario
  33 + Then I should see "Mirror"
  34 +
  35 + @selenium
  36 + Scenario: The block LinkList is changed but the user's block doesnt change
  37 + Given I am on person's control panel
  38 + And I follow "Edit sideboxes"
  39 + And display ".button-bar"
  40 + And I follow "Edit" within ".raw-html-block"
  41 + And I fill in "Custom title for this block:" with "Raw HTML Block"
  42 + And I press "Save"
  43 + And I go to /profile/mario
  44 + Then I should not see "Raw HTML Block"
  45 +
  46 + @selenium
  47 + Scenario: The block Article cannot move or modify
  48 + Given I am on person's control panel
  49 + And I follow "Edit sideboxes"
  50 + And display ".button-bar"
  51 + And I follow "Edit" within ".article-block"
  52 + And I select "Cannot be moved" from "Move options:"
  53 + And I select "Cannot be modified" from "Edit options:"
  54 + And I press "Save"
  55 + And I follow "Logout"
  56 + And Mario's account is activated
  57 + And I follow "Login"
  58 + And I fill in "Username / Email" with "mario"
  59 + And I fill in "Password" with "123456"
  60 + And I press "Log in"
  61 + And I go to /myprofile/mario
  62 + And I follow "Edit sideboxes"
  63 + And display ".button-bar"
  64 + Then I should not see "Edit" within ".article-block"
... ...
test/unit/profile_test.rb
... ... @@ -1122,6 +1122,23 @@ class ProfileTest &lt; ActiveSupport::TestCase
1122 1122 assert_equal 'default title', p.boxes[0].blocks.first[:title]
1123 1123 end
1124 1124  
  1125 + should 'have blocks observer on template when applying template with mirror' do
  1126 + template = fast_create(Profile)
  1127 + template.boxes.destroy_all
  1128 + template.boxes << Box.new
  1129 + b = Block.new(:title => 'default title', :mirror => true)
  1130 + template.boxes[0].blocks << b
  1131 +
  1132 + p = create(Profile)
  1133 + assert !b[:title].blank?
  1134 +
  1135 + p.copy_blocks_from(template)
  1136 +
  1137 + assert_equal 'default title', p.boxes[0].blocks.first[:title]
  1138 + assert_equal p.boxes[0].blocks.first, template.boxes[0].blocks.first.observers.first
  1139 +
  1140 + end
  1141 +
1125 1142 TMP_THEMES_DIR = Rails.root.join('test', 'tmp', 'profile_themes')
1126 1143 should 'have themes' do
1127 1144 Theme.stubs(:user_themes_dir).returns(TMP_THEMES_DIR)
... ...