Commit 8895178ce7bbe3ee35c0a3f212479d185bc67a46

Authored by LeandroNunes
1 parent 7be8e232

ActionItem0: refactoring layout structure

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@59 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
@@ -5,6 +5,12 @@ class ApplicationController < ActionController::Base @@ -5,6 +5,12 @@ 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 + before_filter :load_boxes
  9 + def load_boxes
  10 + @owner = User.find(1)
  11 + @boxes = @owner.boxes
  12 + end
  13 +
8 protected 14 protected
9 15
10 def detect_stuff_by_domain 16 def detect_stuff_by_domain
app/controllers/edit_template_controller.rb 0 → 100644
@@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
  1 +class EditTemplateController < ApplicationController
  2 +
  3 + # TODO: this methods don't belong here
  4 + #TODO See a better way to do this. The layout need a owner to work
  5 +# before_filter :load_owner
  6 +# def load_owner
  7 +# end
  8 +
  9 +# before_filter :load_boxes
  10 + def load_boxes
  11 + @owner = User.find(1)
  12 + @boxes = @owner.boxes
  13 + end
  14 +
  15 + # This method changes a block content to a different box place and
  16 + # updates all boxes at the ends
  17 + def change_box
  18 + b = Block.find(params[:block])
  19 + b.box = Box.find(params[:box_id])
  20 + b.save
  21 + render :update do |page|
  22 + @owner.boxes.each do |box|
  23 + @box_number = box.number
  24 + page.replace_html "box_#{box.number}", {:partial => 'layouts/box_template'}
  25 + page.sortable "sort_#{box.number}", :url => {:action => 'sort_box', :box_number => box.number}
  26 + end
  27 + end
  28 + end
  29 +
  30 + def sort_box
  31 + blocks = Array.new
  32 + box_number = params[:box_number]
  33 + pos = 0
  34 + params["sort_#{box_number}"].each do |block_id|
  35 + pos = pos + 1
  36 + b = Block.find(block_id)
  37 + b.position = pos
  38 + b.save
  39 + blocks.push(b)
  40 + end
  41 + @box_number = box_number
  42 + render :partial => 'layouts/box_template'
  43 + end
  44 +
  45 +end
app/controllers/home_controller.rb
1 class HomeController < ApplicationController 1 class HomeController < ApplicationController
2 2
3 def index 3 def index
4 - render :template => 'home/index' 4 +# render :template => 'home/index'
5 end 5 end
6 6
7 end 7 end
app/helpers/application_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 ApplicationHelper 2 module ApplicationHelper
3 3
  4 + def display_boxes(boxes, main_content)
  5 + boxes.each do |box|
  6 + content_tag(:div, show_blocks(box, main_content) ,:id=>"box_#{box.id}")
  7 + end
  8 + end
4 9
5 - def show_block(owner,box_number)  
6 - blocks = owner.boxes.find(:first, :conditions => ['number = ?', box_number]).blocks  
7 - @out = content_tag(:ul, 10 + def show_blocks(box, main_content)
  11 + blocks = box.blocks
  12 + content_tag(:ul,
8 blocks.map {|b| 13 blocks.map {|b|
9 - content_tag(:li, eval(b.to_html), :class =>"block_item_box_#{b.box_id}" , :id => "block_#{b.id}" ) + draggable('block_'+b.id.to_s)  
10 - }, :id => "leo_#{box_number}"  
11 - ) + drag_drop_item(box_number)  
12 -  
13 -#TODO when we put this parameter the elements stay blocked into the div element indicated.  
14 -#THe consequence is we can't move the element between boxes. Comment it the element can be sorted  
15 -#only when we move a element  
16 -# sortable_block(box_number) 14 + content_tag(:li, b.main? ? main_content : b.to_html, :class =>"block_item_box_#{b.box_id}" , :id => "block_#{b.id}" ) + draggable('block_'+b.id.to_s)
  15 + }, :id => "sort_#{box_number}"
  16 + ) +""
  17 + #drag_drop_item(box_number) + sortable_block(box_number)
  18 + end
  19 +
  20 + def sortable_(box_number)
  21 + drag_drop_item(box_number) + sortable_block(box_number)
17 end 22 end
18 23
19 def sortable_block(box_number) 24 def sortable_block(box_number)
20 - sortable_element "leo_#{box_number}",  
21 - :complete => visual_effect(:highlight, "leo_#{box_number}"), 25 + sortable_element "sort_#{box_number}",
  26 + :complete => visual_effect(:highlight, "sort_#{box_number}"),
22 :url => {:action => 'sort_box', :box_number => box_number } 27 :url => {:action => 'sort_box', :box_number => box_number }
23 end 28 end
24 29
@@ -39,4 +44,5 @@ module ApplicationHelper @@ -39,4 +44,5 @@ module ApplicationHelper
39 }.to_s 44 }.to_s
40 end 45 end
41 46
  47 +
42 end 48 end
app/helpers/edit_template_helper.rb 0 → 100644
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
  1 +module EditTemplateHelper
  2 +end
app/models/block.rb
1 class Block < ActiveRecord::Base 1 class Block < ActiveRecord::Base
  2 + include ActionView::Helpers::UrlHelper
  3 + include ActionView::Helpers::TagHelper
2 belongs_to :box 4 belongs_to :box
3 5
4 #<tt>position</tt> codl not be nil and must be an integer 6 #<tt>position</tt> codl not be nil and must be an integer
@@ -8,8 +10,12 @@ class Block &lt; ActiveRecord::Base @@ -8,8 +10,12 @@ class Block &lt; ActiveRecord::Base
8 validates_presence_of :box_id 10 validates_presence_of :box_id
9 11
10 def to_html 12 def to_html
11 - str = "content_tag(:p, 'bli')"  
12 - return str 13 + #TODO Upgrade this test
  14 +# raise _("This is a main class, don't use it")
  15 + end
  16 +
  17 + def main?
  18 + false
13 end 19 end
14 20
15 end 21 end
app/models/link_block.rb
1 class LinkBlock < Block 1 class LinkBlock < Block
2 def to_html 2 def to_html
3 - str = 'content_tag(:p,[' +  
4 - User.find(:all).map{ |u|  
5 - "[link_to '"+u.name + "', {:controller => 'user', :action => 'test'}]"}.join(',') +  
6 - "])"  
7 - return str 3 + users = User.find(:all).map do |u|
  4 + content_tag("a href='http://www.google.com.br'", u.name)
  5 + end
  6 + users.join(',')
8 end 7 end
9 end 8 end
app/models/list_block.rb
1 class ListBlock < Block 1 class ListBlock < Block
2 2
3 def to_html 3 def to_html
4 - str = "content_tag(:ul, [" +  
5 - User.find(:all).map{|u|  
6 - "content_tag( :li, '#{u.name}' )" }.join(',') + "])"  
7 - return str 4 + content_tag(:ul, User.find(:all).map{|u| content_tag( :li, u.name ) })
8 end 5 end
9 end 6 end
app/models/main_block.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +class MainBlock < Block
  2 +
  3 + def main?
  4 + true
  5 + end
  6 +
  7 +end
app/views/layouts/application.rhtml 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +<html>
  2 + <head>
  3 + <%= javascript_include_tag :defaults %>
  4 + <%= stylesheet_link_tag 'default' %>
  5 +
  6 + </head>
  7 + <body>
  8 + <%= image_tag 'loading.gif', :id=>'spinner', :style=>"display:none; float:right;" %>
  9 +
  10 + <%= link_to _('edit layout'), params.merge({:edit_layout => true}) %>
  11 + <%= link_to _('show layout'), params.merge({:edit_layout => false}) %>
  12 + <%= link_to _('acao diferente'), :action => 'teste' %>
  13 +
  14 + <%= display_boxes(@boxes, yield) %>
  15 +
  16 + </body>
  17 +
  18 +</html>
test/fixtures/blocks.yml
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 -# Link Blocks 2 +#Main Block
3 one: 3 one:
4 id: 1 4 id: 1
5 - box_id: 1 5 + box_id: 2
6 position: 1 6 position: 1
7 - type: 'LinkBlock' 7 + type: 'MainBlock'
  8 +# Link Blocks
8 two: 9 two:
9 id: 2 10 id: 2
10 box_id: 1 11 box_id: 1
11 position: 2 12 position: 2
12 type: 'LinkBlock' 13 type: 'LinkBlock'
13 -# Normal Block  
14 three: 14 three:
15 id: 3 15 id: 3
16 box_id: 1 16 box_id: 1
17 position: 3 17 position: 3
18 - 18 + type: 'LinkBlock'
  19 +#List Blocks
19 four: 20 four:
20 id: 4 21 id: 4
21 - box_id: 2 22 + box_id: 1
22 position: 1 23 position: 1
  24 + type: 'ListBlock'
23 five: 25 five:
24 id: 5 26 id: 5
25 box_id: 3 27 box_id: 3
26 position: 1 28 position: 1
27 -  
28 -#List Blocks 29 + type: 'ListBlock'
29 six: 30 six:
30 id: 6 31 id: 6
31 box_id: 3 32 box_id: 3
test/functional/edit_template_controller_test.rb 0 → 100644
@@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'edit_template_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class EditTemplateController; def rescue_action(e) raise e end; end
  6 +
  7 +class EditTemplateControllerTest < Test::Unit::TestCase
  8 + def setup
  9 + @controller = EditTemplateController.new
  10 + @request = ActionController::TestRequest.new
  11 + @response = ActionController::TestResponse.new
  12 + end
  13 +
  14 + # Replace this with your real tests.
  15 + def test_truth
  16 + assert true
  17 + end
  18 +end