Commit df6f32a7c72389da55aff01b50645b7df65280e5
1 parent
123c4f76
Exists in
master
and in
22 other branches
ActionItem0: refactoring of templates transformations. Creating a manage_templat…
…e plugin to manage the web git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@78 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
15 changed files
with
31 additions
and
414 deletions
Show diff stats
app/controllers/application.rb
| @@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base | @@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base | ||
| 5 | before_filter :detect_stuff_by_domain | 5 | before_filter :detect_stuff_by_domain |
| 6 | attr_reader :virtual_community | 6 | attr_reader :virtual_community |
| 7 | 7 | ||
| 8 | + uses_manage_template :edit => false | ||
| 9 | + | ||
| 8 | before_filter :load_boxes | 10 | before_filter :load_boxes |
| 9 | #TODO To diplay the content we need a variable called '@boxes'. | 11 | #TODO To diplay the content we need a variable called '@boxes'. |
| 10 | #This variable is a set of boxes belongs to a owner | 12 | #This variable is a set of boxes belongs to a owner |
app/controllers/edit_template_controller.rb
| 1 | class EditTemplateController < ApplicationController | 1 | class EditTemplateController < ApplicationController |
| 2 | 2 | ||
| 3 | - before_filter :manage_template | ||
| 4 | - | ||
| 5 | - attr_accessor :controller_manage_template | ||
| 6 | - | ||
| 7 | - def manage_template? | ||
| 8 | - self.controller_manage_template == true ? true : false | ||
| 9 | - end | ||
| 10 | - | ||
| 11 | - # This method changes a block content to a different box place and | ||
| 12 | - # updates all boxes at the ends | ||
| 13 | - def change_box | ||
| 14 | - b = Block.find(params[:block]) | ||
| 15 | - b.box = Box.find(params[:box_id]) | ||
| 16 | - b.save | ||
| 17 | - render :update do |page| | ||
| 18 | - @boxes.each do |box| | ||
| 19 | - @box = box | ||
| 20 | - page.replace_html "box_#{box.number}", {:partial => 'layouts/box_template'} | ||
| 21 | - end | ||
| 22 | - end | ||
| 23 | - end | ||
| 24 | - | ||
| 25 | - def sort_box | ||
| 26 | - blocks = Array.new | ||
| 27 | - box_number = params[:box_number] | ||
| 28 | - pos = 0 | ||
| 29 | - params["sort_#{box_number}"].each do |block_id| | ||
| 30 | - pos = pos + 1 | ||
| 31 | - b = Block.find(block_id) | ||
| 32 | - b.position = pos | ||
| 33 | - b.save | ||
| 34 | - blocks.push(b) | ||
| 35 | - end | ||
| 36 | - @box = box_number | ||
| 37 | - render :partial => 'layouts/box_template' | ||
| 38 | - end | ||
| 39 | - | ||
| 40 | - def set_sort_mode | ||
| 41 | - box = Box.find(params[:box_id]) | ||
| 42 | - render :update do |page| | ||
| 43 | - @box = box | ||
| 44 | - page.replace_html "box_#{box.number}", {:partial => 'layouts/box_template'} | ||
| 45 | - page.sortable "sort_#{box.number}", :url => {:action => 'sort_box', :box_number => box.number} | ||
| 46 | - end | ||
| 47 | - end | ||
| 48 | - | ||
| 49 | - private | ||
| 50 | - | ||
| 51 | - def manage_template | ||
| 52 | - self.controller_manage_template = true | ||
| 53 | - end | 3 | + uses_manage_template :edit => true |
| 54 | 4 | ||
| 55 | end | 5 | end |
app/helpers/application_helper.rb
| @@ -27,23 +27,6 @@ module ApplicationHelper | @@ -27,23 +27,6 @@ module ApplicationHelper | ||
| 27 | ) | 27 | ) |
| 28 | end | 28 | end |
| 29 | 29 | ||
| 30 | - # This method expect an array of boxes and the content retuned of a controller action | ||
| 31 | - # It will generate the boxes div according the yaml definition | ||
| 32 | - def display_boxes(boxes, main_content = "") | ||
| 33 | - # If no boxes is passed return the main content | ||
| 34 | - return main_content if boxes.nil? | ||
| 35 | - | ||
| 36 | - #Generate all boxes of the current profile and considering the defined on template. | ||
| 37 | - content = boxes.map do |box| | ||
| 38 | - content_tag(:div, edit_mode? ? edit_blocks(box, main_content) : show_blocks(box, main_content) , :id=>"box_#{box.number}") | ||
| 39 | - end | ||
| 40 | - | ||
| 41 | - #In case of edit mode add a new div with a class named 'edit_mode' covering all div boxes. | ||
| 42 | - content = content_tag(:div, content, :class => 'edit_mode') if edit_mode? | ||
| 43 | - | ||
| 44 | - content | ||
| 45 | - end | ||
| 46 | - | ||
| 47 | # Load all the css files of a existing template with the template_name passed as argument. | 30 | # Load all the css files of a existing template with the template_name passed as argument. |
| 48 | # | 31 | # |
| 49 | # The files loaded are in the path: | 32 | # The files loaded are in the path: |
| @@ -73,27 +56,4 @@ module ApplicationHelper | @@ -73,27 +56,4 @@ module ApplicationHelper | ||
| 73 | end | 56 | end |
| 74 | end | 57 | end |
| 75 | 58 | ||
| 76 | - private | ||
| 77 | - | ||
| 78 | - # Check if the current controller is the controller that allows layout editing | ||
| 79 | - def edit_mode? | ||
| 80 | - controller.manage_template? | ||
| 81 | - end | ||
| 82 | - | ||
| 83 | - # Shows the block as the struture bellow | ||
| 84 | - # <ul id="sort#{number of the box}"> | ||
| 85 | - # <li class="block_item_box_#{number of the box}" id="block_#{id of block}"> | ||
| 86 | - # </li> | ||
| 87 | - # </ul> | ||
| 88 | - # | ||
| 89 | - def show_blocks(box, main_content = "") | ||
| 90 | - blocks = box.blocks_sort_by_position | ||
| 91 | - content_tag(:ul, | ||
| 92 | - blocks.map {|b| | ||
| 93 | - content_tag(:li, b.main? ? main_content : b.to_html, :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" ) | ||
| 94 | - }, :id => "sort_#{box.number}" | ||
| 95 | - ) | ||
| 96 | - end | ||
| 97 | - | ||
| 98 | - | ||
| 99 | end | 59 | end |
app/helpers/edit_template_helper.rb
| 1 | # Methods added to this helper will be available to all templates in the application. | 1 | # Methods added to this helper will be available to all templates in the application. |
| 2 | module EditTemplateHelper | 2 | module EditTemplateHelper |
| 3 | 3 | ||
| 4 | - private | ||
| 5 | - | ||
| 6 | - # Shows the blocks as defined in <tt>show_blocks</tt> adding the sortable and draggable elements. | ||
| 7 | - # In this case the layout can be manipulated | ||
| 8 | - def edit_blocks(box, main_content = "") | ||
| 9 | - blocks = box.blocks_sort_by_position | ||
| 10 | - content_tag(:ul, box.name + | ||
| 11 | - link_to_remote(_('sort'), {:update => "sort_#{box.number}", :url => {:action => 'set_sort_mode', :box_id => box.id }}, :class => 'sort_button') + | ||
| 12 | - blocks.map {|b| | ||
| 13 | - content_tag(:li, b.name, :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" ) + draggable("block_#{b.id}") | ||
| 14 | - }.to_s, :id => "sort_#{box.number}" | ||
| 15 | - ) + drag_drop_items(box) | ||
| 16 | - end | ||
| 17 | - | ||
| 18 | - # Allows the biven box to have sortable elements | ||
| 19 | - def sortable_block(box_number) | ||
| 20 | - sortable_element "sort_#{box_number}", | ||
| 21 | - :url => {:action => 'sort_box', :box_number => box_number }, | ||
| 22 | - :complete => visual_effect(:highlight, "sort_#{box_number}") | ||
| 23 | - end | ||
| 24 | - | ||
| 25 | - # Allows an element item to be draggable | ||
| 26 | - def draggable(item) | ||
| 27 | - draggable_element(item, :ghosting => true, :revert => true) | ||
| 28 | - end | ||
| 29 | - | ||
| 30 | - # Allows an draggable element change between diferrents boxes | ||
| 31 | - def drag_drop_items(box) | ||
| 32 | - boxes = Box.find_not_box(box.id) | ||
| 33 | - | ||
| 34 | - boxes.map{ |b| | ||
| 35 | - drop_receiving_element("box_#{box.number}", | ||
| 36 | - :accept => "block_item_box_#{b.number}", | ||
| 37 | - :complete => "$('spinner').hide();", | ||
| 38 | - :before => "$('spinner').show();", | ||
| 39 | - :hoverclass => 'hover', | ||
| 40 | - :with => "'block=' + encodeURIComponent(element.id.split('_').last())", | ||
| 41 | - :url => {:action=>:change_box, :box_id => box.id}) | ||
| 42 | - }.to_s | ||
| 43 | - end | ||
| 44 | - | ||
| 45 | - | ||
| 46 | end | 4 | end |
app/models/block.rb
| @@ -1,26 +0,0 @@ | @@ -1,26 +0,0 @@ | ||
| 1 | -#It's the class that define the block's content will be displayed on box in a determined web | ||
| 2 | -class Block < ActiveRecord::Base | ||
| 3 | - include ActionView::Helpers::TagHelper | ||
| 4 | - belongs_to :box | ||
| 5 | - | ||
| 6 | - #<tt>position</tt> codl not be nil and must be an integer | ||
| 7 | - validates_numericality_of :position, :only_integer => true , :message => _('%{fn} must be composed only of integers') | ||
| 8 | - | ||
| 9 | - # A block must be associated to a box | ||
| 10 | - validates_presence_of :box_id | ||
| 11 | - | ||
| 12 | - # Method that define the html code displayed on the box. | ||
| 13 | - # This method cannot be used directly it will be redefined by the children classes | ||
| 14 | - def to_html | ||
| 15 | - raise _("This is a main class, don't use it") | ||
| 16 | - end | ||
| 17 | - | ||
| 18 | - # This method always return false excepted when redefined by the MainBlock class. It mean the current block it's not the result of a | ||
| 19 | - # controller action. | ||
| 20 | - # | ||
| 21 | - # The child class MainBlock subscribes this method returning true. | ||
| 22 | - def main? | ||
| 23 | - false | ||
| 24 | - end | ||
| 25 | - | ||
| 26 | -end |
app/models/box.rb
| @@ -1,21 +0,0 @@ | @@ -1,21 +0,0 @@ | ||
| 1 | -class Box < ActiveRecord::Base | ||
| 2 | - has_many :blocks | ||
| 3 | - belongs_to :owner, :polymorphic => true | ||
| 4 | - | ||
| 5 | - #we cannot have two boxs with the same number to the same owner | ||
| 6 | - validates_uniqueness_of :number, :scope => [:owner_type, :owner_id] | ||
| 7 | - | ||
| 8 | - #<tt>number</tt> could not be nil and must be an integer | ||
| 9 | - validates_numericality_of :number, :only_integer => true, :message => _('%{fn} must be composed only of integers.') | ||
| 10 | - | ||
| 11 | - # Find all boxes except the box with the id given. | ||
| 12 | - def self.find_not_box(box_id) | ||
| 13 | - return Box.find(:all, :conditions => ['id != ?', box_id]) | ||
| 14 | - end | ||
| 15 | - | ||
| 16 | - # Return all blocks of the current box object sorted by the position block | ||
| 17 | - def blocks_sort_by_position | ||
| 18 | - self.blocks.sort{|x,y| x.position <=> y.position} | ||
| 19 | - end | ||
| 20 | - | ||
| 21 | -end |
app/models/main_block.rb
| @@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
| 1 | +class ActsAsTaggableMigration < ActiveRecord::Migration | ||
| 2 | + def self.up | ||
| 3 | + create_table :tags do |t| | ||
| 4 | + t.column :name, :string | ||
| 5 | + t.column :parent_id, :integer | ||
| 6 | + t.column :pending, :bool | ||
| 7 | + end | ||
| 8 | + | ||
| 9 | + create_table :taggings do |t| | ||
| 10 | + t.column :tag_id, :integer | ||
| 11 | + t.column :taggable_id, :integer | ||
| 12 | + | ||
| 13 | + # You should make sure that the column created is | ||
| 14 | + # long enough to store the required class names. | ||
| 15 | + t.column :taggable_type, :string | ||
| 16 | + | ||
| 17 | + t.column :created_at, :datetime | ||
| 18 | + end | ||
| 19 | + | ||
| 20 | + add_index :taggings, :tag_id | ||
| 21 | + add_index :taggings, [:taggable_id, :taggable_type] | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + def self.down | ||
| 25 | + drop_table :taggings | ||
| 26 | + drop_table :tags | ||
| 27 | + end | ||
| 28 | +end |
db/migrate/004_create_boxes.rb
| @@ -1,14 +0,0 @@ | @@ -1,14 +0,0 @@ | ||
| 1 | -class CreateBoxes < ActiveRecord::Migration | ||
| 2 | - def self.up | ||
| 3 | - create_table :boxes do |t| | ||
| 4 | - t.column :name, :string | ||
| 5 | - t.column :number, :integer | ||
| 6 | - t.column :owner_type, :string | ||
| 7 | - t.column :owner_id, :integer | ||
| 8 | - end | ||
| 9 | - end | ||
| 10 | - | ||
| 11 | - def self.down | ||
| 12 | - drop_table :boxes | ||
| 13 | - end | ||
| 14 | -end |
db/migrate/005_create_blocks.rb
| @@ -1,14 +0,0 @@ | @@ -1,14 +0,0 @@ | ||
| 1 | -class CreateBlocks < ActiveRecord::Migration | ||
| 2 | - def self.up | ||
| 3 | - create_table :blocks do |t| | ||
| 4 | - t.column :name, :string | ||
| 5 | - t.column :box_id, :integer | ||
| 6 | - t.column :position, :integer | ||
| 7 | - t.column :type, :string | ||
| 8 | - end | ||
| 9 | - end | ||
| 10 | - | ||
| 11 | - def self.down | ||
| 12 | - drop_table :blocks | ||
| 13 | - end | ||
| 14 | -end |
db/migrate/006_acts_as_taggable_migration.rb
| @@ -1,28 +0,0 @@ | @@ -1,28 +0,0 @@ | ||
| 1 | -class ActsAsTaggableMigration < ActiveRecord::Migration | ||
| 2 | - def self.up | ||
| 3 | - create_table :tags do |t| | ||
| 4 | - t.column :name, :string | ||
| 5 | - t.column :parent_id, :integer | ||
| 6 | - t.column :pending, :bool | ||
| 7 | - end | ||
| 8 | - | ||
| 9 | - create_table :taggings do |t| | ||
| 10 | - t.column :tag_id, :integer | ||
| 11 | - t.column :taggable_id, :integer | ||
| 12 | - | ||
| 13 | - # You should make sure that the column created is | ||
| 14 | - # long enough to store the required class names. | ||
| 15 | - t.column :taggable_type, :string | ||
| 16 | - | ||
| 17 | - t.column :created_at, :datetime | ||
| 18 | - end | ||
| 19 | - | ||
| 20 | - add_index :taggings, :tag_id | ||
| 21 | - add_index :taggings, [:taggable_id, :taggable_type] | ||
| 22 | - end | ||
| 23 | - | ||
| 24 | - def self.down | ||
| 25 | - drop_table :taggings | ||
| 26 | - drop_table :tags | ||
| 27 | - end | ||
| 28 | -end |
test/fixtures/blocks.yml
| @@ -1,40 +0,0 @@ | @@ -1,40 +0,0 @@ | ||
| 1 | -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | ||
| 2 | -#Main Block | ||
| 3 | -one: | ||
| 4 | - id: 1 | ||
| 5 | - box_id: 2 | ||
| 6 | - position: 1 | ||
| 7 | - type: 'MainBlock' | ||
| 8 | - name: 'Main Content' | ||
| 9 | -# Link Blocks | ||
| 10 | -two: | ||
| 11 | - id: 2 | ||
| 12 | - box_id: 1 | ||
| 13 | - position: 2 | ||
| 14 | - type: 'LinkBlock' | ||
| 15 | - name: 'List of Links 1' | ||
| 16 | -three: | ||
| 17 | - id: 3 | ||
| 18 | - box_id: 1 | ||
| 19 | - position: 3 | ||
| 20 | - type: 'LinkBlock' | ||
| 21 | - name: 'List of Link 2' | ||
| 22 | -#List Blocks | ||
| 23 | -four: | ||
| 24 | - id: 4 | ||
| 25 | - box_id: 1 | ||
| 26 | - position: 1 | ||
| 27 | - type: 'ListBlock' | ||
| 28 | - name: 'List of Names 1' | ||
| 29 | -five: | ||
| 30 | - id: 5 | ||
| 31 | - box_id: 3 | ||
| 32 | - position: 1 | ||
| 33 | - type: 'ListBlock' | ||
| 34 | - name: 'List of Names 2' | ||
| 35 | -six: | ||
| 36 | - id: 6 | ||
| 37 | - box_id: 3 | ||
| 38 | - position: 2 | ||
| 39 | - type: 'ListBlock' | ||
| 40 | - name: 'List of Names 3' |
test/fixtures/boxes.yml
| @@ -1,19 +0,0 @@ | @@ -1,19 +0,0 @@ | ||
| 1 | -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | ||
| 2 | -one: | ||
| 3 | - id: 1 | ||
| 4 | - number: 1 | ||
| 5 | - owner_type: 'Profile' | ||
| 6 | - owner_id: 1 | ||
| 7 | - name: 'Box One' | ||
| 8 | -two: | ||
| 9 | - id: 2 | ||
| 10 | - number: 2 | ||
| 11 | - owner_type: 'Profile' | ||
| 12 | - owner_id: 1 | ||
| 13 | - name: 'Box Two' | ||
| 14 | -three: | ||
| 15 | - id: 3 | ||
| 16 | - number: 3 | ||
| 17 | - owner_type: 'Profile' | ||
| 18 | - owner_id: 1 | ||
| 19 | - name: 'Box Three' |
test/unit/block_test.rb
| @@ -1,58 +0,0 @@ | @@ -1,58 +0,0 @@ | ||
| 1 | -require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | - | ||
| 3 | -class BlockTest < Test::Unit::TestCase | ||
| 4 | - fixtures :blocks | ||
| 5 | - | ||
| 6 | - # Replace this with your real tests. | ||
| 7 | - def test_create | ||
| 8 | - count = Block.count | ||
| 9 | - b = Block.new | ||
| 10 | - assert !b.valid? | ||
| 11 | - assert b.errors.invalid?(:box_id) | ||
| 12 | - assert b.errors.invalid?(:position) | ||
| 13 | - | ||
| 14 | - u = User.new | ||
| 15 | - assert u.save | ||
| 16 | - box = Box.new | ||
| 17 | - box.owner = u | ||
| 18 | - box.number = 1000 | ||
| 19 | - assert box.save | ||
| 20 | - b.box = box | ||
| 21 | - assert !b.valid? | ||
| 22 | - assert b.errors.invalid?(:position) | ||
| 23 | - | ||
| 24 | - b.position=1 | ||
| 25 | - assert b.save | ||
| 26 | - | ||
| 27 | - assert_equal count + 1, Block.count | ||
| 28 | - end | ||
| 29 | - | ||
| 30 | - def test_box_presence | ||
| 31 | - b = Block.new | ||
| 32 | - b.position = 1000 | ||
| 33 | - assert !b.valid? | ||
| 34 | - assert b.errors.invalid?(:box_id) | ||
| 35 | - | ||
| 36 | - u = User.new | ||
| 37 | - assert u.save | ||
| 38 | - box = Box.new | ||
| 39 | - box.owner = u | ||
| 40 | - box.number = 1000 | ||
| 41 | - assert box.save | ||
| 42 | - b.box = box | ||
| 43 | - assert b.valid? | ||
| 44 | - | ||
| 45 | - end | ||
| 46 | - | ||
| 47 | - def test_destroy | ||
| 48 | - b = Block.find(1) | ||
| 49 | - assert b.destroy | ||
| 50 | - end | ||
| 51 | - | ||
| 52 | - def test_valid_fixtures | ||
| 53 | - Block.find(:all).each do |b| | ||
| 54 | - assert b.valid? | ||
| 55 | - end | ||
| 56 | - end | ||
| 57 | - | ||
| 58 | -end |
test/unit/box_test.rb
| @@ -1,52 +0,0 @@ | @@ -1,52 +0,0 @@ | ||
| 1 | -require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | - | ||
| 3 | -class BoxTest < Test::Unit::TestCase | ||
| 4 | - fixtures :boxes, :blocks | ||
| 5 | - | ||
| 6 | - def test_destroy | ||
| 7 | - count = Box.count | ||
| 8 | - assert Box.find(1).destroy | ||
| 9 | - assert_equal count - 1, Box.count | ||
| 10 | - end | ||
| 11 | - | ||
| 12 | - def test_create | ||
| 13 | - count = Box.count | ||
| 14 | - b = Box.new | ||
| 15 | - b.number = 2 | ||
| 16 | - assert b.save | ||
| 17 | - assert count + 1, Box.count | ||
| 18 | - end | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - def test_number_format | ||
| 22 | - b = Box.new | ||
| 23 | - b.number = "none" | ||
| 24 | - assert !b.valid? | ||
| 25 | - assert b.errors.invalid?(:number) | ||
| 26 | - | ||
| 27 | - b = Box.new | ||
| 28 | - b.number = 10.2 | ||
| 29 | - assert !b.save | ||
| 30 | - | ||
| 31 | - b = Box.new | ||
| 32 | - b.number = 10 | ||
| 33 | - assert b.save | ||
| 34 | - | ||
| 35 | - end | ||
| 36 | - | ||
| 37 | - def test_unique_number | ||
| 38 | - assert Box.delete_all | ||
| 39 | - assert Box.create(:number => 1) | ||
| 40 | - | ||
| 41 | - b = Box.new(:number => 1) | ||
| 42 | - assert !b.valid? | ||
| 43 | - assert b.errors.invalid?(:number) | ||
| 44 | - end | ||
| 45 | - | ||
| 46 | - def test_presence_number | ||
| 47 | - b = Box.new(:number => nil) | ||
| 48 | - assert !b.valid? | ||
| 49 | - assert b.errors.invalid?(:number) | ||
| 50 | - end | ||
| 51 | - | ||
| 52 | -end |