[noosfero/noosfero][master] Add mirror option for template's blocks. Now if a mirrored block is changed on t…

Rodrigo Souto gitlab at gitlab.com
Thu May 28 13:33:22 BRT 2015


Rodrigo Souto pushed to branch master at Noosfero / noosfero


Commits:
969dd1a3 by Gabriela Navarro at 2015-05-28T12:54:58Z
Add mirror option for template's blocks. Now if a mirrored block is changed on the template it is changed on all users that follow that template.

Signed-off-by: Arthur Del Esposte <arthurmde at gmail.com>
Signed-off-by: Gabriela Navarro <navarro1703 at gmail.com>
Signed-off-by: Thiago Ribeiro <thiagitosouza at gmail.com>

- - - - -


7 changed files:

- app/models/block.rb
- app/models/profile.rb
- app/views/box_organizer/edit.html.erb
- + db/migrate/20150429145001_add_mirror_to_block.rb
- db/schema.rb
- + features/template_block_management.feature
- test/unit/profile_test.rb


Changes:

=====================================
app/models/block.rb
=====================================
--- a/app/models/block.rb
+++ b/app/models/block.rb
@@ -2,7 +2,7 @@ class Block < ActiveRecord::Base
 
   attr_accessible :title, :display, :limit, :box_id, :posts_per_page,
                   :visualization_format, :language, :display_user,
-                  :box, :edit_modes, :move_modes
+                  :box, :edit_modes, :move_modes, :mirror
 
   # to be able to generate HTML
   include ActionView::Helpers::UrlHelper
@@ -15,11 +15,23 @@ class Block < ActiveRecord::Base
 
   acts_as_list :scope => :box
   belongs_to :box
+  belongs_to :mirror_block, :class_name => "Block"
+  has_many :observers, :class_name => "Block", :foreign_key => "mirror_block_id"
 
   acts_as_having_settings
 
   scope :enabled, :conditions => { :enabled => true }
 
+  after_save do |block|
+    if block.owner.kind_of?(Profile) && block.owner.is_template? && block.mirror?
+      block.observers.each do |observer|
+        observer.copy_from(block)
+        observer.title = block.title
+        observer.save
+      end
+    end
+  end
+
   def embedable?
     false
   end
@@ -269,6 +281,10 @@ class Block < ActiveRecord::Base
     self.position = block.position
   end
 
+  def add_observer(block)
+    self.observers << block
+  end
+
   private
 
   def home_page_path


=====================================
app/models/profile.rb
=====================================
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -392,6 +392,9 @@ class Profile < ActiveRecord::Base
         new_block = block.class.new(:title => block[:title])
         new_block.copy_from(block)
         new_box.blocks << new_block
+        if block.mirror?
+          block.add_observer(new_block)
+        end
       end
     end
   end


=====================================
app/views/box_organizer/edit.html.erb
=====================================
--- a/app/views/box_organizer/edit.html.erb
+++ b/app/views/box_organizer/edit.html.erb
@@ -25,6 +25,11 @@
       </div>
       <div class="move-modes">
         <%= labelled_form_field _('Move options:'), select_tag('block[move_modes]', options_from_collection_for_select(@block.move_block_options, :first, :last, @block.move_modes)) %>
+    <% end %>
+
+    <% if @block.owner.kind_of?(Profile) && @block.owner.is_template? %>
+      <div class="mirror_block">
+        <%= labelled_check_box(_("Mirror"), "block[mirror]", value = "1", checked = @block.mirror) %>
       </div>
     <% end %>
 


=====================================
db/migrate/20150429145001_add_mirror_to_block.rb
=====================================
--- /dev/null
+++ b/db/migrate/20150429145001_add_mirror_to_block.rb
@@ -0,0 +1,15 @@
+class AddMirrorToBlock < ActiveRecord::Migration
+  def up
+    change_table :blocks do |t|
+      t.boolean :mirror, :default => false
+      t.references :mirror_block
+      t.references :observers
+    end
+  end
+
+  def down
+    remove_column :blocks, :mirror
+    remove_column :blocks, :mirror_block_id
+    remove_column :blocks, :observers_id
+  end
+end


=====================================
db/schema.rb
=====================================
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -183,10 +183,13 @@ ActiveRecord::Schema.define(:version => 20150513213939) do
     t.string   "type"
     t.text     "settings"
     t.integer  "position"
-    t.boolean  "enabled",    :default => true
+    t.boolean  "enabled",         :default => true
     t.datetime "created_at"
     t.datetime "updated_at"
     t.datetime "fetched_at"
+    t.boolean  "mirror",          :default => false
+    t.integer  "mirror_block_id"
+    t.integer  "observers_id"
   end
 
   add_index "blocks", ["box_id"], :name => "index_blocks_on_box_id"


=====================================
features/template_block_management.feature
=====================================
--- /dev/null
+++ b/features/template_block_management.feature
@@ -0,0 +1,64 @@
+Feature: user template
+  As an user
+  I want to create templates with mirror blocks
+  In order to keep these blocks always updated
+
+  Background:
+    Given the following users
+      | login  | name       | is_template |
+      | person | person     | true        |
+    And the following blocks
+      | owner  | type          | mirror |
+      | person | ArticleBlock  | true   |
+      | person | RawHTMLBlock  | false  |
+    And I go to /account/signup
+    And I fill in "Username" with "mario"
+    And I fill in "Password" with "123456"
+    And I fill in "Password confirmation" with "123456"
+    And I fill in "e-Mail" with "mario at mario.com"
+    And I fill in "Full name" with "Mario"
+    And wait for the captcha signup time
+    And I press "Create my account"
+    And I am logged in as admin
+
+  @selenium
+  Scenario: The block Article name is changed
+    Given I am on person's control panel
+    And I follow "Edit sideboxes"
+    And display ".button-bar"
+    And I follow "Edit" within ".article-block"
+    And I fill in "Custom title for this block:" with "Mirror"
+    And I press "Save"
+    And I go to /profile/mario
+    Then I should see "Mirror"
+
+  @selenium
+  Scenario: The block LinkList is changed but the user's block doesnt change
+    Given I am on person's control panel
+    And I follow "Edit sideboxes"
+    And display ".button-bar"
+    And I follow "Edit" within ".raw-html-block"
+    And I fill in "Custom title for this block:" with "Raw HTML Block"
+    And I press "Save"
+    And I go to /profile/mario
+    Then I should not see "Raw HTML Block"
+
+  @selenium
+  Scenario: The block Article cannot move or modify
+    Given I am on person's control panel
+    And I follow "Edit sideboxes"
+    And display ".button-bar"
+    And I follow "Edit" within ".article-block"
+    And I select "Cannot be moved" from "Move options:"
+    And I select "Cannot be modified" from "Edit options:"
+    And I press "Save"
+    And I follow "Logout"
+    And Mario's account is activated
+    And I follow "Login"
+    And I fill in "Username / Email" with "mario"
+    And I fill in "Password" with "123456"
+    And I press "Log in"
+    And I go to /myprofile/mario
+    And I follow "Edit sideboxes"
+    And display ".button-bar"
+    Then I should not see "Edit" within ".article-block"


=====================================
test/unit/profile_test.rb
=====================================
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -1122,6 +1122,23 @@ class ProfileTest < ActiveSupport::TestCase
     assert_equal 'default title', p.boxes[0].blocks.first[:title]
   end
 
+  should 'have blocks observer on template when applying template with mirror' do
+    template = fast_create(Profile)
+    template.boxes.destroy_all
+    template.boxes << Box.new
+    b = Block.new(:title => 'default title', :mirror => true)
+    template.boxes[0].blocks << b
+
+    p = create(Profile)
+    assert !b[:title].blank?
+
+    p.copy_blocks_from(template)
+
+    assert_equal 'default title', p.boxes[0].blocks.first[:title]
+    assert_equal p.boxes[0].blocks.first, template.boxes[0].blocks.first.observers.first
+
+  end
+
   TMP_THEMES_DIR = Rails.root.join('test', 'tmp', 'profile_themes')
   should 'have themes' do
     Theme.stubs(:user_themes_dir).returns(TMP_THEMES_DIR)



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/969dd1a39e39601c6dc09b844dd86ef0da1b9cf9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150528/55df3670/attachment-0001.html>


More information about the Noosfero-dev mailing list