Commit 92376e38556db312842764a701927bdf18dc1618

Authored by LeandroNunes
1 parent 6794ef2e

ActionItem0: refactoring layout generation

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@62 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
... ... @@ -5,12 +5,17 @@ class ApplicationController < ActionController::Base
5 5 before_filter :detect_stuff_by_domain
6 6 attr_reader :virtual_community
7 7  
  8 + #TODO To diplay the content we need a variable called '@boxes'.
  9 + #This variable is a set of boxes belongs to a owner
  10 + #We have to see a better way to do that
8 11 before_filter :load_boxes
9 12 def load_boxes
10   - @owner = User.find(1)
11   - @boxes = @owner.boxes
  13 + if User.exists?(1)
  14 + owner = User.find(1)
  15 + owner.nil? ? Array.new : @boxes = owner.boxes
  16 + end
12 17 end
13   -
  18 +
14 19 protected
15 20  
16 21 def detect_stuff_by_domain
... ...
app/controllers/edit_template_controller.rb
1 1 class EditTemplateController < ApplicationController
2 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   - def index
10   - @bli = _('testing the app')
11   - end
12   -# before_filter :load_boxes
13   - def load_boxes
14   - @owner = User.find(1)
15   - @boxes = @owner.boxes
16   - end
17   -
18 3 # This method changes a block content to a different box place and
19 4 # updates all boxes at the ends
20 5 def change_box
21   -render :text => "fudeuuuuuu"
22   -return
23 6 b = Block.find(params[:block])
24 7 b.box = Box.find(params[:box_id])
25 8 b.save
26 9 render :update do |page|
27   - @owner.boxes.each do |box|
28   - @box_number = box.number
  10 + @boxes.each do |box|
  11 + @box = box
29 12 page.replace_html "box_#{box.number}", {:partial => 'layouts/box_template'}
30 13 page.sortable "sort_#{box.number}", :url => {:action => 'sort_box', :box_number => box.number}
31 14 end
... ... @@ -33,8 +16,6 @@ return
33 16 end
34 17  
35 18 def sort_box
36   -render :text => "oxeee"
37   -return
38 19 blocks = Array.new
39 20 box_number = params[:box_number]
40 21 pos = 0
... ... @@ -45,7 +26,7 @@ return
45 26 b.save
46 27 blocks.push(b)
47 28 end
48   - @box_number = box_number
  29 + @box = box_number
49 30 render :partial => 'layouts/box_template'
50 31 end
51 32  
... ...
app/helpers/application_helper.rb
1 1 # Methods added to this helper will be available to all templates in the application.
2 2 module ApplicationHelper
3 3  
4   - def display_boxes(boxes, main_content)
5   - boxes.map do |box|
6   - content_tag(:div, show_blocks(box, main_content) ,:id=>"box_#{box.number}")
  4 + # This method expect an array of boxes and the content retuned of a controller action
  5 + def display_boxes(boxes, main_content = "")
  6 + # If no boxes is passed return the main content
  7 + return main_content if boxes.nil?
  8 +
  9 + #Generate all boxes of the current profile and considering the defined on template.
  10 + content = boxes.map do |box|
  11 + content_tag(:div, edit_mode? ? edit_blocks(box, main_content) : show_blocks(box, main_content) , :id=>"box_#{box.number}")
7 12 end
  13 +
  14 + #In case of edit mode add a new div with a class named 'edit_mode' covering all div boxes.
  15 + content = content_tag(:div, content, :class => 'edit_mode') if edit_mode?
  16 +
  17 + content
  18 + end
  19 +
  20 + private
  21 +
  22 + def edit_mode?
  23 + true if controller.controller_name == 'edit_template'
  24 + end
  25 +
  26 + def show_blocks(box, main_content = "")
  27 + blocks = box.blocks_sort_by_position
  28 + content_tag(:ul,
  29 + blocks.map {|b|
  30 + content_tag(:li, b.main? ? main_content : b.to_html, :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" )
  31 + }, :id => "sort_#{box.number}"
  32 + )
8 33 end
9 34  
10   - def show_blocks(box, main_content)
11   - blocks = box.blocks
  35 + def edit_blocks(box, main_content = "")
  36 + blocks = box.blocks_sort_by_position
12 37 content_tag(:ul,
13 38 blocks.map {|b|
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)
  39 + content_tag(:li, b.main? ? main_content : b.to_html , :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" ) + draggable("block_#{b.id}")
15 40 }, :id => "sort_#{box.number}"
16   - ) + drag_drop_item(box.number) + sortable_block(box.number)
  41 + ) + drag_drop_items(box) + sortable_block(box.number)
17 42 end
18 43  
19 44 def sortable_block(box_number)
20 45 sortable_element "sort_#{box_number}",
21   - :complete => visual_effect(:highlight, "sort_#{box_number}"),
22   - :url => {:action => 'sort_box', :box_number => box_number }
  46 + :url => {:action => 'sort_box', :box_number => box_number },
  47 + :complete => visual_effect(:highlight, "sort_#{box_number}")
23 48 end
24 49  
25 50 def draggable(item)
26 51 draggable_element(item, :ghosting=>true, :revert=>true)
27 52 end
28 53  
29   - def drag_drop_item box_number
30   - boxes = Box.find_not_box(box_number)
  54 + def drag_drop_items(box)
  55 + boxes = Box.find_not_box(box.id)
31 56  
32 57 boxes.map{ |b|
33   - drop_receiving_element("box_#{box_number}",
  58 + drop_receiving_element("box_#{box.number}",
34 59 :accept => "block_item_box_#{b.number}",
35 60 :complete => "$('spinner').hide();",
36 61 :before => "$('spinner').show();",
37 62 :hoverclass => 'hover',
38 63 :with => "'block=' + encodeURIComponent(element.id.split('_').last())",
39   - :url => {:action=>:change_box, :box_id => box_number})
  64 + :url => {:action=>:change_box, :box_id => box.id})
40 65 }.to_s
41 66 end
42 67  
... ...
app/helpers/edit_template_helper.rb
... ... @@ -1,2 +0,0 @@
1   -module EditTemplateHelper
2   -end
app/models/box.rb
... ... @@ -11,4 +11,9 @@ class Box &lt; ActiveRecord::Base
11 11 def self.find_not_box(box_id)
12 12 return Box.find(:all, :conditions => ['id != ?', box_id])
13 13 end
  14 +
  15 + def blocks_sort_by_position
  16 + self.blocks.sort{|x,y| x.position <=> y.position}
  17 + end
  18 +
14 19 end
... ...
app/views/edit_template/index.rhtml
1   -oi funfou <%= @bli %>
  1 +<h1> something</h1>
... ...
app/views/layouts/_box_template.rhtml
1   -<%= show_block(@owner, @box_number) %>
  1 +<%= edit_blocks(@box) %>
... ...
db/migrate/004_create_boxes.rb
1 1 class CreateBoxes < ActiveRecord::Migration
2 2 def self.up
3 3 create_table :boxes do |t|
  4 + t.column :name, :string
4 5 t.column :number, :integer
5 6 t.column :owner_type, :string
6 7 t.column :owner_id, :integer
... ...
db/migrate/005_create_blocks.rb
1 1 class CreateBlocks < ActiveRecord::Migration
2 2 def self.up
3 3 create_table :blocks do |t|
  4 + t.column :name, :string
4 5 t.column :box_id, :integer
5 6 t.column :position, :integer
6 7 t.column :type, :string
... ...
public/stylesheets/default.css
1   -.hover {
  1 +.edit_mode .hover {
2 2 background-color: pink;
3 3 border: 2px dotted black;
4 4 }
5 5  
6   -.hover ul li {
  6 +.edit_mode .hover ul li {
7 7 border: 2px dotted black;
8 8 }
9 9  
  10 +.edit_mode #box_1 ul, .edit_mode #box_2 ul, .edit_mode #box_3 ul {
  11 + list-style: none;
  12 + cursor: -moz-grab;
  13 + margin: 10px;
  14 + padding:0px;
  15 +
  16 +}
10 17  
11   -#box_1 ul li, #box_2 ul li, #box_3 ul li {
  18 +.edit_mode #box_1 ul li, .edit_mode #box_2 ul li, .edit_mode #box_3 ul li {
12 19 list-style: none;
13 20 cursor: -moz-grab;
14 21 border: 1px solid black;
15   - margin: 20px;
  22 + margin: 15px;
16 23 }
17 24  
18   -#box_1, #box_2, #box_3 {
19   - border: 1px solid green;
  25 +.edit_mode #box_1, .edit_mode #box_2, .edit_mode #box_3 {
  26 + border: 3px solid green;
20 27 }
21 28  
22 29 #box_1 {
... ...