From 8efec986f67921606a878e5e7b20ffd1b2e73091 Mon Sep 17 00:00:00 2001 From: LeandroNunes Date: Sun, 8 Jul 2007 14:40:01 +0000 Subject: [PATCH] ActionItem0: Finishing the action item but there are some project question to review --- app/controllers/application.rb | 50 +++++++++++++++++++++++++++++++++++--------------- app/controllers/home_controller.rb | 12 +----------- app/helpers/application_helper.rb | 34 +++++++++++++++++++++++++++++++++- app/models/block.rb | 3 ++- app/models/box.rb | 4 ++++ app/models/link_block.rb | 9 ++++++++- app/models/list_block.rb | 9 ++++++++- app/views/home/_leo.rhtml | 9 +++++++++ app/views/layouts/_box_template.rhtml | 1 + app/views/layouts/application.rhtml | 31 ++++++++++++++++--------------- db/migrate/004_create_boxes.rb | 4 ++-- db/migrate/005_create_blocks.rb | 1 + test/fixtures/blocks.yml | 8 ++++++++ test/fixtures/link_blocks.yml | 5 ----- test/fixtures/list_blocks.yml | 5 ----- test/unit/block_test.rb | 2 +- test/unit/link_block_test.rb | 2 +- test/unit/list_block_test.rb | 2 +- 18 files changed, 131 insertions(+), 60 deletions(-) create mode 100644 app/views/home/_leo.rhtml create mode 100644 app/views/layouts/_box_template.rhtml delete mode 100644 test/fixtures/link_blocks.yml delete mode 100644 test/fixtures/list_blocks.yml diff --git a/app/controllers/application.rb b/app/controllers/application.rb index b3f3fe5..2bed635 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -4,28 +4,48 @@ class ApplicationController < ActionController::Base before_filter :detect_stuff_by_domain - before_filter :detect_edit_layout + #TODO See a better way to do this. The layout need a owner to work + before_filter :load_owner + def load_owner + @owner = User.find(1) + end + #TODO See a better way to do this. We need that something say to us when is the time to edit the layout. + #I think the better way is set a different render class to the visualization and to edit a layout. + before_filter :detect_edit_layout def detect_edit_layout @edit_layout = true unless params[:edit_layout].nil? end -# after_filter :render_actions - - def render_actions -@bla = 'funfou' -#return @bla - render_action('index', nil, true) -# render :update do |page| -# page.replace_html 'box_1', :partial => 'pending_todos' -# page.replace_html 'completed_todos', :partial => 'completed_todos' -# page.replace_html 'working_todos', :partial => 'working_todos' -# end + # This method changes a block content to a different box place and + # updates all boxes at the ends + def change_box + b = Block.find(params[:block]) + b.box = Box.find(params[:box_id]) + b.save + render :update do |page| + @owner.boxes.each do |box| + @box_number = box.number + page.replace_html "box_#{box.number}", {:partial => 'layouts/box_template'} + page.sortable "leo_#{box.number}", :url => {:action => 'sort_box', :box_number => box.number} + end + end end -# def render(type = nil) -# render_actions -# end + def sort_box + blocks = Array.new + box_number = params[:box_number] + pos = 0 + params["leo_#{box_number}"].each do |block_id| + pos = pos + 1 + b = Block.find(block_id) + b.position = pos + b.save + blocks.push(b) + end + @box_number = box_number + render :partial => 'layouts/box_template' + end protected diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 6293db1..9a028c5 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,17 +1,7 @@ class HomeController < ApplicationController def index - #TODO this is a test case of owner - @owner = User.find(1) + render :template => 'home/index' end -def teste - render :update do |page| - page.replace_html 'box_1', :partial => 'leo' -#'nem acredito' -# page.replace_html 'completed_todos', :partial => 'completed_todos' -# page.replace_html 'working_todos', :partial => 'working_todos' - end - -end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d47e9ac..28b726e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -4,7 +4,39 @@ module ApplicationHelper def show_block(owner,box_number) blocks = owner.boxes.find(:first, :conditions => ['number = ?', box_number]).blocks - @out = blocks.map {|b| b.to_html}.join('') + @out = content_tag(:ul, + blocks.map {|b| + content_tag(:li, eval(b.to_html), :class =>"block_item_box_#{b.box_id}" , :id => "block_#{b.id}" ) + draggable('block_'+b.id.to_s) + }, :id => "leo_#{box_number}" + ) + drag_drop_item(box_number) + +#TODO when we put this parameter the elements stay blocked into the div element indicated. +#THe consequence is we can't move the element between boxes. Comment it the element can be sorted +#only when we move a element +# sortable_block(box_number) + end + + def sortable_block(box_number) + sortable_element "leo_#{box_number}", + :complete => visual_effect(:highlight, "leo_#{box_number}"), + :url => {:action => 'sort_box', :box_number => box_number } + end + + def draggable item + draggable_element(item, :ghosting=>true, :revert=>true) + end + + def drag_drop_item box_id + boxes = Box.find_not_box(box_id) + return boxes.map{ |b| + drop_receiving_element("box_#{box_id}", + :accept => "block_item_box_#{b.id}", + :complete => "$('spinner').hide();", + :before => "$('spinner').show();", + :hoverclass => 'hover', + :with => "'block=' + encodeURIComponent(element.id.split('_').last())", + :url => {:action=>:change_box, :box_id=> box_id}) + }.to_s end end diff --git a/app/models/block.rb b/app/models/block.rb index e00b993..4beabde 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -8,7 +8,8 @@ class Block < ActiveRecord::Base validates_presence_of :box_id def to_html - return '

bli

' + str = "content_tag(:p, 'bli')" + return str end end diff --git a/app/models/box.rb b/app/models/box.rb index 6a3bc2d..b925d30 100644 --- a/app/models/box.rb +++ b/app/models/box.rb @@ -7,4 +7,8 @@ class Box < ActiveRecord::Base #number could not be nil and must be an integer validates_numericality_of :number, :only_integer => true, :message => _('%{fn} must be composed only of integers.') + + def self.find_not_box(box_id) + return Box.find(:all, :conditions => ['id != ?', box_id]) + end end diff --git a/app/models/link_block.rb b/app/models/link_block.rb index 47b5780..12a0a49 100644 --- a/app/models/link_block.rb +++ b/app/models/link_block.rb @@ -1,2 +1,9 @@ -class LinkBlock < ActiveRecord::Base +class LinkBlock < Block + def to_html + str = 'content_tag(:p,[' + + User.find_all.map{ |u| + "[link_to '"+u.name + "', {:controller => 'user', :action => 'test'}]"}.join(',') + + "])" + return str + end end diff --git a/app/models/list_block.rb b/app/models/list_block.rb index 8657acd..09cbbf9 100644 --- a/app/models/list_block.rb +++ b/app/models/list_block.rb @@ -1,2 +1,9 @@ -class ListBlock < ActiveRecord::Base +class ListBlock < Block + + def to_html + str = "content_tag(:ul, [" + + User.find_all.map{|u| + "content_tag( :li, '#{u.name}' )" }.join(',') + "])" + return str + end end diff --git a/app/views/home/_leo.rhtml b/app/views/home/_leo.rhtml new file mode 100644 index 0000000..76a3b67 --- /dev/null +++ b/app/views/home/_leo.rhtml @@ -0,0 +1,9 @@ + + + diff --git a/app/views/layouts/_box_template.rhtml b/app/views/layouts/_box_template.rhtml new file mode 100644 index 0000000..b1e5fd6 --- /dev/null +++ b/app/views/layouts/_box_template.rhtml @@ -0,0 +1 @@ +<%= show_block(@owner, @box_number) %> diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml index 4673d14..31a1c6e 100644 --- a/app/views/layouts/application.rhtml +++ b/app/views/layouts/application.rhtml @@ -11,28 +11,29 @@ <%= link_to _('show layout'), params.merge({:edit_layout => false}) %> <%= link_to _('acao diferente'), :action => 'teste' %> -teste -<%= @bla %> -oxe -
<%= show_block(@owner, 1)%>
+<%= update_page_tag{|page| +#TODO this is to test +#page.alert('leo') +#@box_number=1 +#page.replace_html "box_1", {:partial => 'leo'}; +#page.replace_html "box_1", {:partial => 'layouts/box_template'}; +#page.sortable "leo_1", :url => {:action => 'sort_box', :box_number => 1}; +#)page.show +} +%> + +
- <%= show_block(@owner, 1)%> - + <%= show_block(@owner, 2)%> + <%= yield %>
- <%= show_block(@owner, 1)%> - + <%= show_block(@owner, 3)%>
+ diff --git a/db/migrate/004_create_boxes.rb b/db/migrate/004_create_boxes.rb index 1d33a4d..21581bb 100644 --- a/db/migrate/004_create_boxes.rb +++ b/db/migrate/004_create_boxes.rb @@ -1,9 +1,9 @@ class CreateBoxes < ActiveRecord::Migration def self.up create_table :boxes do |t| - t.column :number, :integer + t.column :number, :integer t.column :owner_type, :string - t.column :owner_id, :integer + t.column :owner_id, :integer end end diff --git a/db/migrate/005_create_blocks.rb b/db/migrate/005_create_blocks.rb index cbd027e..5c72292 100644 --- a/db/migrate/005_create_blocks.rb +++ b/db/migrate/005_create_blocks.rb @@ -3,6 +3,7 @@ class CreateBlocks < ActiveRecord::Migration create_table :blocks do |t| t.column :box_id, :integer t.column :position, :integer + t.column :type, :string end end diff --git a/test/fixtures/blocks.yml b/test/fixtures/blocks.yml index 9859ca9..f0c2ece 100644 --- a/test/fixtures/blocks.yml +++ b/test/fixtures/blocks.yml @@ -1,16 +1,21 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +# Link Blocks one: id: 1 box_id: 1 position: 1 + type: 'LinkBlock' two: id: 2 box_id: 1 position: 2 + type: 'LinkBlock' +# Normal Block three: id: 3 box_id: 1 position: 3 + four: id: 4 box_id: 2 @@ -19,7 +24,10 @@ five: id: 5 box_id: 3 position: 1 + +#List Blocks six: id: 6 box_id: 3 position: 2 + type: 'ListBlock' diff --git a/test/fixtures/link_blocks.yml b/test/fixtures/link_blocks.yml deleted file mode 100644 index b49c4eb..0000000 --- a/test/fixtures/link_blocks.yml +++ /dev/null @@ -1,5 +0,0 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: - id: 1 -two: - id: 2 diff --git a/test/fixtures/list_blocks.yml b/test/fixtures/list_blocks.yml deleted file mode 100644 index b49c4eb..0000000 --- a/test/fixtures/list_blocks.yml +++ /dev/null @@ -1,5 +0,0 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: - id: 1 -two: - id: 2 diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index ffe2355..2b0a7cb 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -50,7 +50,7 @@ class BlockTest < Test::Unit::TestCase end def test_valid_fixtures - Block.find_all.each do |b| + Block.find(:all).each do |b| assert b.valid? end end diff --git a/test/unit/link_block_test.rb b/test/unit/link_block_test.rb index 24f4a16..03411b2 100644 --- a/test/unit/link_block_test.rb +++ b/test/unit/link_block_test.rb @@ -1,7 +1,7 @@ require File.dirname(__FILE__) + '/../test_helper' class LinkBlockTest < Test::Unit::TestCase - fixtures :link_blocks + fixtures :blocks # Replace this with your real tests. def test_truth diff --git a/test/unit/list_block_test.rb b/test/unit/list_block_test.rb index c5d6f9e..c5882ad 100644 --- a/test/unit/list_block_test.rb +++ b/test/unit/list_block_test.rb @@ -1,7 +1,7 @@ require File.dirname(__FILE__) + '/../test_helper' class ListBlockTest < Test::Unit::TestCase - fixtures :list_blocks + fixtures :blocks # Replace this with your real tests. def test_truth -- libgit2 0.21.2