From 8895178ce7bbe3ee35c0a3f212479d185bc67a46 Mon Sep 17 00:00:00 2001 From: LeandroNunes Date: Mon, 9 Jul 2007 22:41:08 +0000 Subject: [PATCH] ActionItem0: refactoring layout structure --- app/controllers/application.rb | 6 ++++++ app/controllers/edit_template_controller.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ app/controllers/home_controller.rb | 2 +- app/helpers/application_helper.rb | 32 +++++++++++++++++++------------- app/helpers/edit_template_helper.rb | 2 ++ app/models/block.rb | 10 ++++++++-- app/models/link_block.rb | 9 ++++----- app/models/list_block.rb | 5 +---- app/models/main_block.rb | 7 +++++++ app/views/layouts/application.rhtml | 18 ++++++++++++++++++ test/fixtures/blocks.yml | 17 +++++++++-------- test/functional/edit_template_controller_test.rb | 18 ++++++++++++++++++ 12 files changed, 138 insertions(+), 33 deletions(-) create mode 100644 app/controllers/edit_template_controller.rb create mode 100644 app/helpers/edit_template_helper.rb create mode 100644 app/models/main_block.rb create mode 100644 app/views/layouts/application.rhtml create mode 100644 test/functional/edit_template_controller_test.rb diff --git a/app/controllers/application.rb b/app/controllers/application.rb index a42f0af..55d7608 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -5,6 +5,12 @@ class ApplicationController < ActionController::Base before_filter :detect_stuff_by_domain attr_reader :virtual_community + before_filter :load_boxes + def load_boxes + @owner = User.find(1) + @boxes = @owner.boxes + end + protected def detect_stuff_by_domain diff --git a/app/controllers/edit_template_controller.rb b/app/controllers/edit_template_controller.rb new file mode 100644 index 0000000..1966b6e --- /dev/null +++ b/app/controllers/edit_template_controller.rb @@ -0,0 +1,45 @@ +class EditTemplateController < ApplicationController + + # TODO: this methods don't belong here + #TODO See a better way to do this. The layout need a owner to work +# before_filter :load_owner +# def load_owner +# end + +# before_filter :load_boxes + def load_boxes + @owner = User.find(1) + @boxes = @owner.boxes + 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 "sort_#{box.number}", :url => {:action => 'sort_box', :box_number => box.number} + end + end + end + + def sort_box + blocks = Array.new + box_number = params[:box_number] + pos = 0 + params["sort_#{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 + +end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 9a028c5..5950e6c 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,7 +1,7 @@ class HomeController < ApplicationController def index - render :template => 'home/index' +# render :template => 'home/index' end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 28b726e..76fcbd9 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,24 +1,29 @@ # Methods added to this helper will be available to all templates in the application. module ApplicationHelper + def display_boxes(boxes, main_content) + boxes.each do |box| + content_tag(:div, show_blocks(box, main_content) ,:id=>"box_#{box.id}") + end + end - def show_block(owner,box_number) - blocks = owner.boxes.find(:first, :conditions => ['number = ?', box_number]).blocks - @out = content_tag(:ul, + def show_blocks(box, main_content) + blocks = box.blocks + 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) + 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) + }, :id => "sort_#{box_number}" + ) +"" + #drag_drop_item(box_number) + sortable_block(box_number) + end + + def sortable_(box_number) + drag_drop_item(box_number) + sortable_block(box_number) end def sortable_block(box_number) - sortable_element "leo_#{box_number}", - :complete => visual_effect(:highlight, "leo_#{box_number}"), + sortable_element "sort_#{box_number}", + :complete => visual_effect(:highlight, "sort_#{box_number}"), :url => {:action => 'sort_box', :box_number => box_number } end @@ -39,4 +44,5 @@ module ApplicationHelper }.to_s end + end diff --git a/app/helpers/edit_template_helper.rb b/app/helpers/edit_template_helper.rb new file mode 100644 index 0000000..36780ef --- /dev/null +++ b/app/helpers/edit_template_helper.rb @@ -0,0 +1,2 @@ +module EditTemplateHelper +end diff --git a/app/models/block.rb b/app/models/block.rb index 4beabde..ce46264 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -1,4 +1,6 @@ class Block < ActiveRecord::Base + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper belongs_to :box #position codl not be nil and must be an integer @@ -8,8 +10,12 @@ class Block < ActiveRecord::Base validates_presence_of :box_id def to_html - str = "content_tag(:p, 'bli')" - return str + #TODO Upgrade this test +# raise _("This is a main class, don't use it") + end + + def main? + false end end diff --git a/app/models/link_block.rb b/app/models/link_block.rb index a224d8f..27461d3 100644 --- a/app/models/link_block.rb +++ b/app/models/link_block.rb @@ -1,9 +1,8 @@ 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 + users = User.find(:all).map do |u| + content_tag("a href='http://www.google.com.br'", u.name) + end + users.join(',') end end diff --git a/app/models/list_block.rb b/app/models/list_block.rb index 457ca1d..7b92109 100644 --- a/app/models/list_block.rb +++ b/app/models/list_block.rb @@ -1,9 +1,6 @@ class ListBlock < Block def to_html - str = "content_tag(:ul, [" + - User.find(:all).map{|u| - "content_tag( :li, '#{u.name}' )" }.join(',') + "])" - return str + content_tag(:ul, User.find(:all).map{|u| content_tag( :li, u.name ) }) end end diff --git a/app/models/main_block.rb b/app/models/main_block.rb new file mode 100644 index 0000000..aebc0a4 --- /dev/null +++ b/app/models/main_block.rb @@ -0,0 +1,7 @@ +class MainBlock < Block + + def main? + true + end + +end diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml new file mode 100644 index 0000000..fcebe05 --- /dev/null +++ b/app/views/layouts/application.rhtml @@ -0,0 +1,18 @@ + + + <%= javascript_include_tag :defaults %> + <%= stylesheet_link_tag 'default' %> + + + + <%= image_tag 'loading.gif', :id=>'spinner', :style=>"display:none; float:right;" %> + + <%= link_to _('edit layout'), params.merge({:edit_layout => true}) %> + <%= link_to _('show layout'), params.merge({:edit_layout => false}) %> + <%= link_to _('acao diferente'), :action => 'teste' %> + + <%= display_boxes(@boxes, yield) %> + + + + diff --git a/test/fixtures/blocks.yml b/test/fixtures/blocks.yml index f0c2ece..6beebaa 100644 --- a/test/fixtures/blocks.yml +++ b/test/fixtures/blocks.yml @@ -1,31 +1,32 @@ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -# Link Blocks +#Main Block one: id: 1 - box_id: 1 + box_id: 2 position: 1 - type: 'LinkBlock' + type: 'MainBlock' +# Link Blocks two: id: 2 box_id: 1 position: 2 type: 'LinkBlock' -# Normal Block three: id: 3 box_id: 1 position: 3 - + type: 'LinkBlock' +#List Blocks four: id: 4 - box_id: 2 + box_id: 1 position: 1 + type: 'ListBlock' five: id: 5 box_id: 3 position: 1 - -#List Blocks + type: 'ListBlock' six: id: 6 box_id: 3 diff --git a/test/functional/edit_template_controller_test.rb b/test/functional/edit_template_controller_test.rb new file mode 100644 index 0000000..066251d --- /dev/null +++ b/test/functional/edit_template_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'edit_template_controller' + +# Re-raise errors caught by the controller. +class EditTemplateController; def rescue_action(e) raise e end; end + +class EditTemplateControllerTest < Test::Unit::TestCase + def setup + @controller = EditTemplateController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end -- libgit2 0.21.2