From df6f32a7c72389da55aff01b50645b7df65280e5 Mon Sep 17 00:00:00 2001 From: LeandroNunes Date: Mon, 16 Jul 2007 01:46:17 +0000 Subject: [PATCH] ActionItem0: refactoring of templates transformations. Creating a manage_template plugin to manage the web --- app/controllers/application.rb | 2 ++ app/controllers/edit_template_controller.rb | 52 +--------------------------------------------------- app/helpers/application_helper.rb | 40 ---------------------------------------- app/helpers/edit_template_helper.rb | 42 ------------------------------------------ app/models/block.rb | 26 -------------------------- app/models/box.rb | 21 --------------------- app/models/main_block.rb | 9 --------- db/migrate/004_acts_as_taggable_migration.rb | 28 ++++++++++++++++++++++++++++ db/migrate/004_create_boxes.rb | 14 -------------- db/migrate/005_create_blocks.rb | 14 -------------- db/migrate/006_acts_as_taggable_migration.rb | 28 ---------------------------- test/fixtures/blocks.yml | 40 ---------------------------------------- test/fixtures/boxes.yml | 19 ------------------- test/unit/block_test.rb | 58 ---------------------------------------------------------- test/unit/box_test.rb | 52 ---------------------------------------------------- 15 files changed, 31 insertions(+), 414 deletions(-) delete mode 100644 app/models/block.rb delete mode 100644 app/models/box.rb delete mode 100644 app/models/main_block.rb create mode 100644 db/migrate/004_acts_as_taggable_migration.rb delete mode 100644 db/migrate/004_create_boxes.rb delete mode 100644 db/migrate/005_create_blocks.rb delete mode 100644 db/migrate/006_acts_as_taggable_migration.rb delete mode 100644 test/fixtures/blocks.yml delete mode 100644 test/fixtures/boxes.yml delete mode 100644 test/unit/block_test.rb delete mode 100644 test/unit/box_test.rb diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 744e798..050aeee 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -5,6 +5,8 @@ class ApplicationController < ActionController::Base before_filter :detect_stuff_by_domain attr_reader :virtual_community + uses_manage_template :edit => false + before_filter :load_boxes #TODO To diplay the content we need a variable called '@boxes'. #This variable is a set of boxes belongs to a owner diff --git a/app/controllers/edit_template_controller.rb b/app/controllers/edit_template_controller.rb index f1eb0ec..e6381a7 100644 --- a/app/controllers/edit_template_controller.rb +++ b/app/controllers/edit_template_controller.rb @@ -1,55 +1,5 @@ class EditTemplateController < ApplicationController - before_filter :manage_template - - attr_accessor :controller_manage_template - - def manage_template? - self.controller_manage_template == true ? true : false - 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| - @boxes.each do |box| - @box = box - page.replace_html "box_#{box.number}", {:partial => 'layouts/box_template'} - 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 = box_number - render :partial => 'layouts/box_template' - end - - def set_sort_mode - box = Box.find(params[:box_id]) - render :update do |page| - @box = box - 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 - - private - - def manage_template - self.controller_manage_template = true - end + uses_manage_template :edit => true end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 80e887e..1d643ae 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -27,23 +27,6 @@ module ApplicationHelper ) end - # This method expect an array of boxes and the content retuned of a controller action - # It will generate the boxes div according the yaml definition - def display_boxes(boxes, main_content = "") - # If no boxes is passed return the main content - return main_content if boxes.nil? - - #Generate all boxes of the current profile and considering the defined on template. - content = boxes.map do |box| - content_tag(:div, edit_mode? ? edit_blocks(box, main_content) : show_blocks(box, main_content) , :id=>"box_#{box.number}") - end - - #In case of edit mode add a new div with a class named 'edit_mode' covering all div boxes. - content = content_tag(:div, content, :class => 'edit_mode') if edit_mode? - - content - end - # Load all the css files of a existing template with the template_name passed as argument. # # The files loaded are in the path: @@ -73,27 +56,4 @@ module ApplicationHelper end end - private - - # Check if the current controller is the controller that allows layout editing - def edit_mode? - controller.manage_template? - end - - # Shows the block as the struture bellow - # - # - def show_blocks(box, main_content = "") - blocks = box.blocks_sort_by_position - content_tag(:ul, - blocks.map {|b| - content_tag(:li, b.main? ? main_content : b.to_html, :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" ) - }, :id => "sort_#{box.number}" - ) - end - - end diff --git a/app/helpers/edit_template_helper.rb b/app/helpers/edit_template_helper.rb index 3f1edf1..0157adb 100644 --- a/app/helpers/edit_template_helper.rb +++ b/app/helpers/edit_template_helper.rb @@ -1,46 +1,4 @@ # Methods added to this helper will be available to all templates in the application. module EditTemplateHelper - private - - # Shows the blocks as defined in show_blocks adding the sortable and draggable elements. - # In this case the layout can be manipulated - def edit_blocks(box, main_content = "") - blocks = box.blocks_sort_by_position - content_tag(:ul, box.name + - link_to_remote(_('sort'), {:update => "sort_#{box.number}", :url => {:action => 'set_sort_mode', :box_id => box.id }}, :class => 'sort_button') + - blocks.map {|b| - content_tag(:li, b.name, :class =>"block_item_box_#{box.number}" , :id => "block_#{b.id}" ) + draggable("block_#{b.id}") - }.to_s, :id => "sort_#{box.number}" - ) + drag_drop_items(box) - end - - # Allows the biven box to have sortable elements - def sortable_block(box_number) - sortable_element "sort_#{box_number}", - :url => {:action => 'sort_box', :box_number => box_number }, - :complete => visual_effect(:highlight, "sort_#{box_number}") - end - - # Allows an element item to be draggable - def draggable(item) - draggable_element(item, :ghosting => true, :revert => true) - end - - # Allows an draggable element change between diferrents boxes - def drag_drop_items(box) - boxes = Box.find_not_box(box.id) - - boxes.map{ |b| - drop_receiving_element("box_#{box.number}", - :accept => "block_item_box_#{b.number}", - :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 deleted file mode 100644 index 2f74650..0000000 --- a/app/models/block.rb +++ /dev/null @@ -1,26 +0,0 @@ -#It's the class that define the block's content will be displayed on box in a determined web -class Block < ActiveRecord::Base - include ActionView::Helpers::TagHelper - belongs_to :box - - #position codl not be nil and must be an integer - validates_numericality_of :position, :only_integer => true , :message => _('%{fn} must be composed only of integers') - - # A block must be associated to a box - validates_presence_of :box_id - - # Method that define the html code displayed on the box. - # This method cannot be used directly it will be redefined by the children classes - def to_html - raise _("This is a main class, don't use it") - end - - # This method always return false excepted when redefined by the MainBlock class. It mean the current block it's not the result of a - # controller action. - # - # The child class MainBlock subscribes this method returning true. - def main? - false - end - -end diff --git a/app/models/box.rb b/app/models/box.rb deleted file mode 100644 index e61bd9b..0000000 --- a/app/models/box.rb +++ /dev/null @@ -1,21 +0,0 @@ -class Box < ActiveRecord::Base - has_many :blocks - belongs_to :owner, :polymorphic => true - - #we cannot have two boxs with the same number to the same owner - validates_uniqueness_of :number, :scope => [:owner_type, :owner_id] - - #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.') - - # Find all boxes except the box with the id given. - def self.find_not_box(box_id) - return Box.find(:all, :conditions => ['id != ?', box_id]) - end - - # Return all blocks of the current box object sorted by the position block - def blocks_sort_by_position - self.blocks.sort{|x,y| x.position <=> y.position} - end - -end diff --git a/app/models/main_block.rb b/app/models/main_block.rb deleted file mode 100644 index 5b3ac82..0000000 --- a/app/models/main_block.rb +++ /dev/null @@ -1,9 +0,0 @@ -class MainBlock < Block - - #This method always return true. It means the current block have to display the result of controller action. - #It has the same result of put the yield variable on the application layout - def main? - true - end - -end diff --git a/db/migrate/004_acts_as_taggable_migration.rb b/db/migrate/004_acts_as_taggable_migration.rb new file mode 100644 index 0000000..4d8b7ee --- /dev/null +++ b/db/migrate/004_acts_as_taggable_migration.rb @@ -0,0 +1,28 @@ +class ActsAsTaggableMigration < ActiveRecord::Migration + def self.up + create_table :tags do |t| + t.column :name, :string + t.column :parent_id, :integer + t.column :pending, :bool + end + + create_table :taggings do |t| + t.column :tag_id, :integer + t.column :taggable_id, :integer + + # You should make sure that the column created is + # long enough to store the required class names. + t.column :taggable_type, :string + + t.column :created_at, :datetime + end + + add_index :taggings, :tag_id + add_index :taggings, [:taggable_id, :taggable_type] + end + + def self.down + drop_table :taggings + drop_table :tags + end +end diff --git a/db/migrate/004_create_boxes.rb b/db/migrate/004_create_boxes.rb deleted file mode 100644 index 8e1eddf..0000000 --- a/db/migrate/004_create_boxes.rb +++ /dev/null @@ -1,14 +0,0 @@ -class CreateBoxes < ActiveRecord::Migration - def self.up - create_table :boxes do |t| - t.column :name, :string - t.column :number, :integer - t.column :owner_type, :string - t.column :owner_id, :integer - end - end - - def self.down - drop_table :boxes - end -end diff --git a/db/migrate/005_create_blocks.rb b/db/migrate/005_create_blocks.rb deleted file mode 100644 index c16b331..0000000 --- a/db/migrate/005_create_blocks.rb +++ /dev/null @@ -1,14 +0,0 @@ -class CreateBlocks < ActiveRecord::Migration - def self.up - create_table :blocks do |t| - t.column :name, :string - t.column :box_id, :integer - t.column :position, :integer - t.column :type, :string - end - end - - def self.down - drop_table :blocks - end -end diff --git a/db/migrate/006_acts_as_taggable_migration.rb b/db/migrate/006_acts_as_taggable_migration.rb deleted file mode 100644 index 4d8b7ee..0000000 --- a/db/migrate/006_acts_as_taggable_migration.rb +++ /dev/null @@ -1,28 +0,0 @@ -class ActsAsTaggableMigration < ActiveRecord::Migration - def self.up - create_table :tags do |t| - t.column :name, :string - t.column :parent_id, :integer - t.column :pending, :bool - end - - create_table :taggings do |t| - t.column :tag_id, :integer - t.column :taggable_id, :integer - - # You should make sure that the column created is - # long enough to store the required class names. - t.column :taggable_type, :string - - t.column :created_at, :datetime - end - - add_index :taggings, :tag_id - add_index :taggings, [:taggable_id, :taggable_type] - end - - def self.down - drop_table :taggings - drop_table :tags - end -end diff --git a/test/fixtures/blocks.yml b/test/fixtures/blocks.yml deleted file mode 100644 index ce96174..0000000 --- a/test/fixtures/blocks.yml +++ /dev/null @@ -1,40 +0,0 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -#Main Block -one: - id: 1 - box_id: 2 - position: 1 - type: 'MainBlock' - name: 'Main Content' -# Link Blocks -two: - id: 2 - box_id: 1 - position: 2 - type: 'LinkBlock' - name: 'List of Links 1' -three: - id: 3 - box_id: 1 - position: 3 - type: 'LinkBlock' - name: 'List of Link 2' -#List Blocks -four: - id: 4 - box_id: 1 - position: 1 - type: 'ListBlock' - name: 'List of Names 1' -five: - id: 5 - box_id: 3 - position: 1 - type: 'ListBlock' - name: 'List of Names 2' -six: - id: 6 - box_id: 3 - position: 2 - type: 'ListBlock' - name: 'List of Names 3' diff --git a/test/fixtures/boxes.yml b/test/fixtures/boxes.yml deleted file mode 100644 index a0f7c4d..0000000 --- a/test/fixtures/boxes.yml +++ /dev/null @@ -1,19 +0,0 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -one: - id: 1 - number: 1 - owner_type: 'Profile' - owner_id: 1 - name: 'Box One' -two: - id: 2 - number: 2 - owner_type: 'Profile' - owner_id: 1 - name: 'Box Two' -three: - id: 3 - number: 3 - owner_type: 'Profile' - owner_id: 1 - name: 'Box Three' diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb deleted file mode 100644 index 2b0a7cb..0000000 --- a/test/unit/block_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -class BlockTest < Test::Unit::TestCase - fixtures :blocks - - # Replace this with your real tests. - def test_create - count = Block.count - b = Block.new - assert !b.valid? - assert b.errors.invalid?(:box_id) - assert b.errors.invalid?(:position) - - u = User.new - assert u.save - box = Box.new - box.owner = u - box.number = 1000 - assert box.save - b.box = box - assert !b.valid? - assert b.errors.invalid?(:position) - - b.position=1 - assert b.save - - assert_equal count + 1, Block.count - end - - def test_box_presence - b = Block.new - b.position = 1000 - assert !b.valid? - assert b.errors.invalid?(:box_id) - - u = User.new - assert u.save - box = Box.new - box.owner = u - box.number = 1000 - assert box.save - b.box = box - assert b.valid? - - end - - def test_destroy - b = Block.find(1) - assert b.destroy - end - - def test_valid_fixtures - Block.find(:all).each do |b| - assert b.valid? - end - end - -end diff --git a/test/unit/box_test.rb b/test/unit/box_test.rb deleted file mode 100644 index 31dc94c..0000000 --- a/test/unit/box_test.rb +++ /dev/null @@ -1,52 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' - -class BoxTest < Test::Unit::TestCase - fixtures :boxes, :blocks - - def test_destroy - count = Box.count - assert Box.find(1).destroy - assert_equal count - 1, Box.count - end - - def test_create - count = Box.count - b = Box.new - b.number = 2 - assert b.save - assert count + 1, Box.count - end - - - def test_number_format - b = Box.new - b.number = "none" - assert !b.valid? - assert b.errors.invalid?(:number) - - b = Box.new - b.number = 10.2 - assert !b.save - - b = Box.new - b.number = 10 - assert b.save - - end - - def test_unique_number - assert Box.delete_all - assert Box.create(:number => 1) - - b = Box.new(:number => 1) - assert !b.valid? - assert b.errors.invalid?(:number) - end - - def test_presence_number - b = Box.new(:number => nil) - assert !b.valid? - assert b.errors.invalid?(:number) - end - -end -- libgit2 0.21.2