diff --git a/app/models/block.rb b/app/models/block.rb index f11c75d..a18a5b3 100644 --- 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 diff --git a/app/models/profile.rb b/app/models/profile.rb index 3220fd2..7338329 100644 --- 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 diff --git a/app/views/box_organizer/edit.html.erb b/app/views/box_organizer/edit.html.erb index 9fa35eb..c9e2664 100644 --- a/app/views/box_organizer/edit.html.erb +++ b/app/views/box_organizer/edit.html.erb @@ -25,6 +25,11 @@
<%= 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? %> +
+ <%= labelled_check_box(_("Mirror"), "block[mirror]", value = "1", checked = @block.mirror) %>
<% end %> diff --git a/db/migrate/20150429145001_add_mirror_to_block.rb b/db/migrate/20150429145001_add_mirror_to_block.rb new file mode 100644 index 0000000..d48ed75 --- /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 diff --git a/db/schema.rb b/db/schema.rb index 057a6c0..31a8586 100644 --- 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" diff --git a/features/template_block_management.feature b/features/template_block_management.feature new file mode 100644 index 0000000..00132a0 --- /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@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" diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index b61f6a7..5b6a9ec 100644 --- 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) -- libgit2 0.21.2