Commit 8efec986f67921606a878e5e7b20ffd1b2e73091

Authored by LeandroNunes
1 parent 102338bc

ActionItem0: Finishing the action item but there are some project question to review


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@37 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
... ... @@ -4,28 +4,48 @@ class ApplicationController < ActionController::Base
4 4  
5 5 before_filter :detect_stuff_by_domain
6 6  
7   - before_filter :detect_edit_layout
  7 + #TODO See a better way to do this. The layout need a owner to work
  8 + before_filter :load_owner
  9 + def load_owner
  10 + @owner = User.find(1)
  11 + end
8 12  
  13 + #TODO See a better way to do this. We need that something say to us when is the time to edit the layout.
  14 + #I think the better way is set a different render class to the visualization and to edit a layout.
  15 + before_filter :detect_edit_layout
9 16 def detect_edit_layout
10 17 @edit_layout = true unless params[:edit_layout].nil?
11 18 end
12 19  
13   -# after_filter :render_actions
14   -
15   - def render_actions
16   -@bla = 'funfou'
17   -#return @bla
18   - render_action('index', nil, true)
19   -# render :update do |page|
20   -# page.replace_html 'box_1', :partial => 'pending_todos'
21   -# page.replace_html 'completed_todos', :partial => 'completed_todos'
22   -# page.replace_html 'working_todos', :partial => 'working_todos'
23   -# end
  20 + # This method changes a block content to a different box place and
  21 + # updates all boxes at the ends
  22 + def change_box
  23 + b = Block.find(params[:block])
  24 + b.box = Box.find(params[:box_id])
  25 + b.save
  26 + render :update do |page|
  27 + @owner.boxes.each do |box|
  28 + @box_number = box.number
  29 + page.replace_html "box_#{box.number}", {:partial => 'layouts/box_template'}
  30 + page.sortable "leo_#{box.number}", :url => {:action => 'sort_box', :box_number => box.number}
  31 + end
  32 + end
24 33 end
25 34  
26   -# def render(type = nil)
27   -# render_actions
28   -# end
  35 + def sort_box
  36 + blocks = Array.new
  37 + box_number = params[:box_number]
  38 + pos = 0
  39 + params["leo_#{box_number}"].each do |block_id|
  40 + pos = pos + 1
  41 + b = Block.find(block_id)
  42 + b.position = pos
  43 + b.save
  44 + blocks.push(b)
  45 + end
  46 + @box_number = box_number
  47 + render :partial => 'layouts/box_template'
  48 + end
29 49  
30 50 protected
31 51  
... ...
app/controllers/home_controller.rb
1 1 class HomeController < ApplicationController
2 2  
3 3 def index
4   - #TODO this is a test case of owner
5   - @owner = User.find(1)
  4 + render :template => 'home/index'
6 5 end
7 6  
8   -def teste
9   - render :update do |page|
10   - page.replace_html 'box_1', :partial => 'leo'
11   -#'nem acredito'
12   -# page.replace_html 'completed_todos', :partial => 'completed_todos'
13   -# page.replace_html 'working_todos', :partial => 'working_todos'
14   - end
15   -
16   -end
17 7 end
... ...
app/helpers/application_helper.rb
... ... @@ -4,7 +4,39 @@ module ApplicationHelper
4 4  
5 5 def show_block(owner,box_number)
6 6 blocks = owner.boxes.find(:first, :conditions => ['number = ?', box_number]).blocks
7   - @out = blocks.map {|b| b.to_html}.join('')
  7 + @out = content_tag(:ul,
  8 + 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)
  17 + end
  18 +
  19 + def sortable_block(box_number)
  20 + sortable_element "leo_#{box_number}",
  21 + :complete => visual_effect(:highlight, "leo_#{box_number}"),
  22 + :url => {:action => 'sort_box', :box_number => box_number }
  23 + end
  24 +
  25 + def draggable item
  26 + draggable_element(item, :ghosting=>true, :revert=>true)
  27 + end
  28 +
  29 + def drag_drop_item box_id
  30 + boxes = Box.find_not_box(box_id)
  31 + return boxes.map{ |b|
  32 + drop_receiving_element("box_#{box_id}",
  33 + :accept => "block_item_box_#{b.id}",
  34 + :complete => "$('spinner').hide();",
  35 + :before => "$('spinner').show();",
  36 + :hoverclass => 'hover',
  37 + :with => "'block=' + encodeURIComponent(element.id.split('_').last())",
  38 + :url => {:action=>:change_box, :box_id=> box_id})
  39 + }.to_s
8 40 end
9 41  
10 42 end
... ...
app/models/block.rb
... ... @@ -8,7 +8,8 @@ class Block &lt; ActiveRecord::Base
8 8 validates_presence_of :box_id
9 9  
10 10 def to_html
11   - return '<p>bli</p>'
  11 + str = "content_tag(:p, 'bli')"
  12 + return str
12 13 end
13 14  
14 15 end
... ...
app/models/box.rb
... ... @@ -7,4 +7,8 @@ class Box &lt; ActiveRecord::Base
7 7  
8 8 #<tt>number</tt> could not be nil and must be an integer
9 9 validates_numericality_of :number, :only_integer => true, :message => _('%{fn} must be composed only of integers.')
  10 +
  11 + def self.find_not_box(box_id)
  12 + return Box.find(:all, :conditions => ['id != ?', box_id])
  13 + end
10 14 end
... ...
app/models/link_block.rb
1   -class LinkBlock < ActiveRecord::Base
  1 +class LinkBlock < Block
  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
  8 + end
2 9 end
... ...
app/models/list_block.rb
1   -class ListBlock < ActiveRecord::Base
  1 +class ListBlock < Block
  2 +
  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
  8 + end
2 9 end
... ...
app/views/home/_leo.rhtml 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +
  2 +<ul id="leo_1" >
  3 +<!-- TODO this is to test -->
  4 +teste qual foi
  5 + <li class="block_item_box_1" id="block_1" > teste 1</li>
  6 +<%= draggable('block_1') %>
  7 + <li class="block_item_box_1" id="block_2" > teste 2</li>
  8 +</ul>
  9 +
... ...
app/views/layouts/_box_template.rhtml 0 → 100644
... ... @@ -0,0 +1 @@
  1 +<%= show_block(@owner, @box_number) %>
... ...
app/views/layouts/application.rhtml
... ... @@ -11,28 +11,29 @@
11 11 <%= link_to _('show layout'), params.merge({:edit_layout => false}) %>
12 12 <%= link_to _('acao diferente'), :action => 'teste' %>
13 13  
14   -teste
15   -<%= @bla %>
16   -oxe
17   -
18 14 <div id="box_1" >
19 15 <%= show_block(@owner, 1)%>
20 16 </div>
  17 +<%= update_page_tag{|page|
  18 +#TODO this is to test
  19 +#page.alert('leo')
  20 +#@box_number=1
  21 +#page.replace_html "box_1", {:partial => 'leo'};
  22 +#page.replace_html "box_1", {:partial => 'layouts/box_template'};
  23 +#page.sortable "leo_1", :url => {:action => 'sort_box', :box_number => 1};
  24 +#)page.show
  25 +}
  26 +%>
  27 +
  28 +
21 29 <div id="box_2" >
22   - <%= show_block(@owner, 1)%>
23   - <ul>
24   - <li>
25   - <%= yield %>
26   - </li>
27   - </ul>
  30 + <%= show_block(@owner, 2)%>
  31 + <%= yield %>
28 32 </div>
29 33 <div id="box_3" >
30   - <%= show_block(@owner, 1)%>
31   - <ul>
32   - <li> lado direito
33   - </li>
34   - </ul>
  34 + <%= show_block(@owner, 3)%>
35 35 </div>
36 36  
37 37 </body>
  38 +
38 39 </html>
... ...
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 :number, :integer
  4 + t.column :number, :integer
5 5 t.column :owner_type, :string
6   - t.column :owner_id, :integer
  6 + t.column :owner_id, :integer
7 7 end
8 8 end
9 9  
... ...
db/migrate/005_create_blocks.rb
... ... @@ -3,6 +3,7 @@ class CreateBlocks &lt; ActiveRecord::Migration
3 3 create_table :blocks do |t|
4 4 t.column :box_id, :integer
5 5 t.column :position, :integer
  6 + t.column :type, :string
6 7 end
7 8 end
8 9  
... ...
test/fixtures/blocks.yml
1 1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
  2 +# Link Blocks
2 3 one:
3 4 id: 1
4 5 box_id: 1
5 6 position: 1
  7 + type: 'LinkBlock'
6 8 two:
7 9 id: 2
8 10 box_id: 1
9 11 position: 2
  12 + type: 'LinkBlock'
  13 +# Normal Block
10 14 three:
11 15 id: 3
12 16 box_id: 1
13 17 position: 3
  18 +
14 19 four:
15 20 id: 4
16 21 box_id: 2
... ... @@ -19,7 +24,10 @@ five:
19 24 id: 5
20 25 box_id: 3
21 26 position: 1
  27 +
  28 +#List Blocks
22 29 six:
23 30 id: 6
24 31 box_id: 3
25 32 position: 2
  33 + type: 'ListBlock'
... ...
test/fixtures/link_blocks.yml
... ... @@ -1,5 +0,0 @@
1   -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2   -one:
3   - id: 1
4   -two:
5   - id: 2
test/fixtures/list_blocks.yml
... ... @@ -1,5 +0,0 @@
1   -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2   -one:
3   - id: 1
4   -two:
5   - id: 2
test/unit/block_test.rb
... ... @@ -50,7 +50,7 @@ class BlockTest &lt; Test::Unit::TestCase
50 50 end
51 51  
52 52 def test_valid_fixtures
53   - Block.find_all.each do |b|
  53 + Block.find(:all).each do |b|
54 54 assert b.valid?
55 55 end
56 56 end
... ...
test/unit/link_block_test.rb
1 1 require File.dirname(__FILE__) + '/../test_helper'
2 2  
3 3 class LinkBlockTest < Test::Unit::TestCase
4   - fixtures :link_blocks
  4 + fixtures :blocks
5 5  
6 6 # Replace this with your real tests.
7 7 def test_truth
... ...
test/unit/list_block_test.rb
1 1 require File.dirname(__FILE__) + '/../test_helper'
2 2  
3 3 class ListBlockTest < Test::Unit::TestCase
4   - fixtures :list_blocks
  4 + fixtures :blocks
5 5  
6 6 # Replace this with your real tests.
7 7 def test_truth
... ...