From ba62ba336cc0213957027bbef7af2ae8c84ecbc0 Mon Sep 17 00:00:00 2001 From: Alinson Santos Date: Tue, 22 Apr 2008 12:02:51 -0300 Subject: [PATCH] Anexos aninhados --- app/controllers/application.rb | 1 + app/controllers/attachments_controller.rb | 2 + app/helpers/attachments_helper.rb | 51 +++++++++++++++++++++++ app/models/attachment.rb | 3 ++ app/models/course.rb | 2 +- app/views/attachments/_form.html.haml | 4 ++ app/views/attachments/show.html.haml | 3 ++ app/views/courses/show.html.haml | 8 ++-- app/views/stylesheets/wiki.css.erb | 10 +++++ db/schema.rb | 3 +- 10 files changed, 80 insertions(+), 7 deletions(-) diff --git a/app/controllers/application.rb b/app/controllers/application.rb index e0f6391..70986bd 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -19,6 +19,7 @@ require 'yaml' class ApplicationController < ActionController::Base include AuthenticationSystem + helper :all before_filter :startup around_filter :set_timezone diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index a5beb06..28159b1 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -30,6 +30,7 @@ class AttachmentsController < ApplicationController def create @attachment.course_id = @course.id + @attachment.path = params[:attachment][:path] @attachment.description = params[:attachment][:description] unless params[:attachment][:file].kind_of?(String) @attachment.file = params[:attachment][:file] @@ -54,6 +55,7 @@ class AttachmentsController < ApplicationController end def update + @attachment.path = params[:attachment][:path] @attachment.description = params[:attachment][:description] unless params[:attachment][:file].kind_of?(String) @attachment.file = params[:attachment][:file] diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index a4792a6..db6e625 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -15,4 +15,55 @@ # along with this program. If not, see . module AttachmentsHelper + + def attachments_to_nested_hash(atts) + paths = atts.collect { |item| item.path.nil? ? [] : item.path.split("/") } + return nest_path(atts, paths, 0, paths.size-1, 0) + end + + def nest_path(items, paths, from, to, level) + result = {} + + base = from - 1 + base = base + 1 while base+1 <= to and paths[base+1][level].nil? + if base >= from then + result['/'] = items[from..base] + end + + start = base+1 + + return result if start > to + + folder = paths[start][level] + (base+1).upto(to) do |i| + if paths[i][level] != folder + result[folder] = nest_path(items, paths, start, i-1, level+1) + start = i + folder = paths[i][level] + end + end + + if start <= to then + result[folder] = nest_path(items, paths, start, to, level+1) + end + + return result + end + + def nested_attachments_to_html(atts, level=0) + out = (level > 0 ? "