diff --git a/app/models/article.rb b/app/models/article.rb index 033af35..7b6dbce 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -640,16 +640,16 @@ class Article < ActiveRecord::Base # Subclasses aren't (re)loaded, and acts_as_solr # depends on subclasses method to search # see http://stackoverflow.com/questions/4138957/activerecordsubclassnotfound-error-when-using-sti-in-rails/4139245 - UploadedFile - TextArticle - TinyMceArticle - TextileArticle - Folder - EnterpriseHomepage - Gallery - Blog - Forum - Event +# UploadedFile +# TextArticle +# TinyMceArticle +# TextileArticle +# Folder +# EnterpriseHomepage +# Gallery +# Blog +# Forum +# Event def self.f_type_proc(klass) klass.constantize.type_name diff --git a/plugins/work_assignment/lib/ext/uploaded_file.rb b/plugins/work_assignment/lib/ext/uploaded_file.rb new file mode 100644 index 0000000..8574b83 --- /dev/null +++ b/plugins/work_assignment/lib/ext/uploaded_file.rb @@ -0,0 +1,13 @@ +require_dependency 'uploaded_file' + +class UploadedFile < Article + after_save do |uploaded_file| + if uploaded_file.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment) + author_folder = uploaded_file.parent.find_or_create_author_folder(uploaded_file.author) + uploaded_file.name = WorkAssignmentPlugin::WorkAssignment.versioned_name(uploaded_file, author_folder) + uploaded_file.parent = author_folder + logger.info("\n\n==> #{uploaded_file.name}\n\n") + uploaded_file.save! + end + end +end diff --git a/plugins/work_assignment/lib/work_assignment_plugin.rb b/plugins/work_assignment/lib/work_assignment_plugin.rb index ca733fe..70527cf 100644 --- a/plugins/work_assignment/lib/work_assignment_plugin.rb +++ b/plugins/work_assignment/lib/work_assignment_plugin.rb @@ -1,3 +1,5 @@ +require_dependency 'ext/uploaded_file' + class WorkAssignmentPlugin < Noosfero::Plugin def self.plugin_name diff --git a/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb b/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb index 788576c..51b9584 100644 --- a/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb +++ b/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb @@ -1,14 +1,5 @@ class WorkAssignmentPlugin::WorkAssignment < Folder - after_save do |work_assignment| - work_assignment.children.select {|child| child.kind_of?(UploadedFile)}.each do |submission| - author_folder = work_assignment.find_or_create_author_folder(submission.author) - submission.name = versioned_name(submission, author_folder) - submission.parent = author_folder - submission.save(false) - end - end - settings_items :publish_submissions, :type => :boolean, :default => false def self.icon_name(article = nil) @@ -24,9 +15,7 @@ class WorkAssignmentPlugin::WorkAssignment < Folder end def self.versioned_name(submission, folder) - return submission.name if submission.name =~ /\(V[0-9]*\)/ - count = folder.children.include?(submission) ? folder.children.count : folder.children.count + 1 - "(V#{count}) #{submission.name}" + "(V#{folder.children.count + 1}) #{submission.name}" end def accept_comments? @@ -44,7 +33,7 @@ class WorkAssignmentPlugin::WorkAssignment < Folder end def find_or_create_author_folder(author) - children.find_by_slug(author.identifier) || Folder.create!(:name => author.name, :slug => author.identifier, :parent => self, :profile => profile) + children.find_by_slug(author.identifier.to_slug) || Folder.create!(:name => author.name, :slug => author.identifier.to_slug, :parent => self, :profile => profile) end def submissions diff --git a/plugins/work_assignment/test/functional/cms_controller_test.rb b/plugins/work_assignment/test/functional/cms_controller_test.rb index 7b9f94a..6766aa3 100644 --- a/plugins/work_assignment/test/functional/cms_controller_test.rb +++ b/plugins/work_assignment/test/functional/cms_controller_test.rb @@ -30,5 +30,15 @@ class CmsControllerTest < ActionController::TestCase assert_response :success end + should 'upload submission and automatically move it to the author folder' do + organization = fast_create(Organization) + work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Work Assignment', :profile => organization) + organization.add_member(person) + post :upload_files, :profile => organization.identifier, :parent_id => work_assignment.id, :uploaded_files => [fixture_file_upload('/files/test.txt', 'text/plain')] + + submission = UploadedFile.last + assert_equal work_assignment.find_or_create_author_folder(person), submission.parent + end + end diff --git a/plugins/work_assignment/test/unit/work_assingment_plugin_test.rb b/plugins/work_assignment/test/unit/work_assingment_plugin_test.rb index 5318a85..fd15061 100644 --- a/plugins/work_assignment/test/unit/work_assingment_plugin_test.rb +++ b/plugins/work_assignment/test/unit/work_assingment_plugin_test.rb @@ -9,12 +9,10 @@ class WorkAssignmentPluginTest < ActiveSupport::TestCase work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Work Assignment', :profile => organization) content.parent = work_assignment content.save! - assert !WorkAssignmentPlugin.is_submission?(content) - - author_folder = work_assignment.find_or_create_author_folder(fast_create(Person)) - content.parent = author_folder - content.save! assert WorkAssignmentPlugin.is_submission?(content) + + author_folder = work_assignment.find_or_create_author_folder(content.author) + assert_equal author_folder, content.parent end should 'be able to download submission if work_assignment published submissions' do -- libgit2 0.21.2