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 a04d227..5c6a51d 100644 --- a/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb +++ b/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb @@ -1,6 +1,15 @@ class WorkAssignmentPlugin::WorkAssignment < Folder - alias :submissions :children + 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) if !(submission.name =~ /\(V[0-9]*\)/) + submission.parent = author_folder + submission.save! + end + end + + settings_items :publish_submissions, :type => :boolean, :default => false def self.icon_name(article = nil) 'work-assignment' @@ -14,14 +23,30 @@ class WorkAssignmentPlugin::WorkAssignment < Folder _('Defines a work to be done by the members and receives their submissions about this work.') end + def self.versioned_name(submission, folder) + "(V#{folder.children.count + 1}) #{submission.name}" + end + def accept_comments? true end + def allow_create?(user) + profile.members.include?(user) + end + def to_html(options = {}) lambda do render :file => 'content_viewer/work_assignment.html.erb' end 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) + end + + def submissions + children.map(&:children).flatten.compact + end + end diff --git a/plugins/work_assignment/test/unit/work_assingment_plugin/work_assignment_test.rb b/plugins/work_assignment/test/unit/work_assingment_plugin/work_assignment_test.rb new file mode 100644 index 0000000..f9f0969 --- /dev/null +++ b/plugins/work_assignment/test/unit/work_assingment_plugin/work_assignment_test.rb @@ -0,0 +1,43 @@ +require "test_helper" + +class WorkAssignmentTest < ActiveSupport::TestCase + should 'find or create sub-folder based on author identifier' do + profile = fast_create(Profile) + author = fast_create(Person) + work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Sample Work Assignment', :profile => profile) + assert_nil work_assignment.children.find_by_slug(author.identifier) + + folder = work_assignment.find_or_create_author_folder(author) + assert_not_nil work_assignment.children.find_by_slug(author.identifier) + assert_equal folder, work_assignment.find_or_create_author_folder(author) + end + + should 'return versioned name' do + folder = fast_create(Folder) + profile = fast_create(Profile) + a1 = Article.create!(:name => "Article 1", :profile => profile) + a2 = Article.create!(:name => "Article 2", :profile => profile) + a3 = Article.create!(:name => "Article 3", :profile => profile) + klass = WorkAssignmentPlugin::WorkAssignment + + assert_equal "(V1) #{a1.name}", klass.versioned_name(a1, folder) + + a1.parent = folder + a1.save! + assert_equal "(V2) #{a2.name}", klass.versioned_name(a2, folder) + + a2.parent = folder + a2.save! + assert_equal "(V3) #{a3.name}", klass.versioned_name(a3, folder) + end + + should 'move submission to its correct author folder' do + organization = fast_create(Organization) + author = fast_create(Person) + work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Sample Work Assignment', :profile => organization) + submission = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => organization, :parent => work_assignment, :author => author) + + author_folder = work_assignment.find_or_create_author_folder(author) + assert author_folder, submission.parent + end +end -- libgit2 0.21.2