Commit 91942873684e600a0695e77020f8c59a53430b64

Authored by Rodrigo Souto
1 parent 61d0aa09

[work-assignment] Defining Work Assignment content logics

plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb
1 class WorkAssignmentPlugin::WorkAssignment < Folder 1 class WorkAssignmentPlugin::WorkAssignment < Folder
2 2
3 - alias :submissions :children 3 + after_save do |work_assignment|
  4 + work_assignment.children.select {|child| child.kind_of?(UploadedFile)}.each do |submission|
  5 + author_folder = work_assignment.find_or_create_author_folder(submission.author)
  6 + submission.name = versioned_name(submission, author_folder) if !(submission.name =~ /\(V[0-9]*\)/)
  7 + submission.parent = author_folder
  8 + submission.save!
  9 + end
  10 + end
  11 +
  12 + settings_items :publish_submissions, :type => :boolean, :default => false
4 13
5 def self.icon_name(article = nil) 14 def self.icon_name(article = nil)
6 'work-assignment' 15 'work-assignment'
@@ -14,14 +23,30 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder @@ -14,14 +23,30 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder
14 _('Defines a work to be done by the members and receives their submissions about this work.') 23 _('Defines a work to be done by the members and receives their submissions about this work.')
15 end 24 end
16 25
  26 + def self.versioned_name(submission, folder)
  27 + "(V#{folder.children.count + 1}) #{submission.name}"
  28 + end
  29 +
17 def accept_comments? 30 def accept_comments?
18 true 31 true
19 end 32 end
20 33
  34 + def allow_create?(user)
  35 + profile.members.include?(user)
  36 + end
  37 +
21 def to_html(options = {}) 38 def to_html(options = {})
22 lambda do 39 lambda do
23 render :file => 'content_viewer/work_assignment.html.erb' 40 render :file => 'content_viewer/work_assignment.html.erb'
24 end 41 end
25 end 42 end
26 43
  44 + def find_or_create_author_folder(author)
  45 + children.find_by_slug(author.identifier) || Folder.create!(:name => author.name, :slug => author.identifier, :parent => self, :profile => profile)
  46 + end
  47 +
  48 + def submissions
  49 + children.map(&:children).flatten.compact
  50 + end
  51 +
27 end 52 end
plugins/work_assignment/test/unit/work_assingment_plugin/work_assignment_test.rb 0 → 100644
@@ -0,0 +1,43 @@ @@ -0,0 +1,43 @@
  1 +require "test_helper"
  2 +
  3 +class WorkAssignmentTest < ActiveSupport::TestCase
  4 + should 'find or create sub-folder based on author identifier' do
  5 + profile = fast_create(Profile)
  6 + author = fast_create(Person)
  7 + work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Sample Work Assignment', :profile => profile)
  8 + assert_nil work_assignment.children.find_by_slug(author.identifier)
  9 +
  10 + folder = work_assignment.find_or_create_author_folder(author)
  11 + assert_not_nil work_assignment.children.find_by_slug(author.identifier)
  12 + assert_equal folder, work_assignment.find_or_create_author_folder(author)
  13 + end
  14 +
  15 + should 'return versioned name' do
  16 + folder = fast_create(Folder)
  17 + profile = fast_create(Profile)
  18 + a1 = Article.create!(:name => "Article 1", :profile => profile)
  19 + a2 = Article.create!(:name => "Article 2", :profile => profile)
  20 + a3 = Article.create!(:name => "Article 3", :profile => profile)
  21 + klass = WorkAssignmentPlugin::WorkAssignment
  22 +
  23 + assert_equal "(V1) #{a1.name}", klass.versioned_name(a1, folder)
  24 +
  25 + a1.parent = folder
  26 + a1.save!
  27 + assert_equal "(V2) #{a2.name}", klass.versioned_name(a2, folder)
  28 +
  29 + a2.parent = folder
  30 + a2.save!
  31 + assert_equal "(V3) #{a3.name}", klass.versioned_name(a3, folder)
  32 + end
  33 +
  34 + should 'move submission to its correct author folder' do
  35 + organization = fast_create(Organization)
  36 + author = fast_create(Person)
  37 + work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Sample Work Assignment', :profile => organization)
  38 + submission = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => organization, :parent => work_assignment, :author => author)
  39 +
  40 + author_folder = work_assignment.find_or_create_author_folder(author)
  41 + assert author_folder, submission.parent
  42 + end
  43 +end