Commit 525240d0a9ee1ebd1692316642e7e2e144b69925

Authored by Rodrigo Souto
1 parent 8f28228d

[work-assignment] Fixing loop with callbacks

  * Must fix the class calls commented on Article class
app/models/article.rb
... ... @@ -640,16 +640,16 @@ class Article < ActiveRecord::Base
640 640 # Subclasses aren't (re)loaded, and acts_as_solr
641 641 # depends on subclasses method to search
642 642 # see http://stackoverflow.com/questions/4138957/activerecordsubclassnotfound-error-when-using-sti-in-rails/4139245
643   - UploadedFile
644   - TextArticle
645   - TinyMceArticle
646   - TextileArticle
647   - Folder
648   - EnterpriseHomepage
649   - Gallery
650   - Blog
651   - Forum
652   - Event
  643 +# UploadedFile
  644 +# TextArticle
  645 +# TinyMceArticle
  646 +# TextileArticle
  647 +# Folder
  648 +# EnterpriseHomepage
  649 +# Gallery
  650 +# Blog
  651 +# Forum
  652 +# Event
653 653  
654 654 def self.f_type_proc(klass)
655 655 klass.constantize.type_name
... ...
plugins/work_assignment/lib/ext/uploaded_file.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +require_dependency 'uploaded_file'
  2 +
  3 +class UploadedFile < Article
  4 + after_save do |uploaded_file|
  5 + if uploaded_file.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment)
  6 + author_folder = uploaded_file.parent.find_or_create_author_folder(uploaded_file.author)
  7 + uploaded_file.name = WorkAssignmentPlugin::WorkAssignment.versioned_name(uploaded_file, author_folder)
  8 + uploaded_file.parent = author_folder
  9 + logger.info("\n\n==> #{uploaded_file.name}\n\n")
  10 + uploaded_file.save!
  11 + end
  12 + end
  13 +end
... ...
plugins/work_assignment/lib/work_assignment_plugin.rb
  1 +require_dependency 'ext/uploaded_file'
  2 +
1 3 class WorkAssignmentPlugin < Noosfero::Plugin
2 4  
3 5 def self.plugin_name
... ...
plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb
1 1 class WorkAssignmentPlugin::WorkAssignment < Folder
2 2  
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)
7   - submission.parent = author_folder
8   - submission.save(false)
9   - end
10   - end
11   -
12 3 settings_items :publish_submissions, :type => :boolean, :default => false
13 4  
14 5 def self.icon_name(article = nil)
... ... @@ -24,9 +15,7 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder
24 15 end
25 16  
26 17 def self.versioned_name(submission, folder)
27   - return submission.name if submission.name =~ /\(V[0-9]*\)/
28   - count = folder.children.include?(submission) ? folder.children.count : folder.children.count + 1
29   - "(V#{count}) #{submission.name}"
  18 + "(V#{folder.children.count + 1}) #{submission.name}"
30 19 end
31 20  
32 21 def accept_comments?
... ... @@ -44,7 +33,7 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder
44 33 end
45 34  
46 35 def find_or_create_author_folder(author)
47   - children.find_by_slug(author.identifier) || Folder.create!(:name => author.name, :slug => author.identifier, :parent => self, :profile => profile)
  36 + children.find_by_slug(author.identifier.to_slug) || Folder.create!(:name => author.name, :slug => author.identifier.to_slug, :parent => self, :profile => profile)
48 37 end
49 38  
50 39 def submissions
... ...
plugins/work_assignment/test/functional/cms_controller_test.rb
... ... @@ -30,5 +30,15 @@ class CmsControllerTest &lt; ActionController::TestCase
30 30 assert_response :success
31 31 end
32 32  
  33 + should 'upload submission and automatically move it to the author folder' do
  34 + organization = fast_create(Organization)
  35 + work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Work Assignment', :profile => organization)
  36 + organization.add_member(person)
  37 + post :upload_files, :profile => organization.identifier, :parent_id => work_assignment.id, :uploaded_files => [fixture_file_upload('/files/test.txt', 'text/plain')]
  38 +
  39 + submission = UploadedFile.last
  40 + assert_equal work_assignment.find_or_create_author_folder(person), submission.parent
  41 + end
  42 +
33 43 end
34 44  
... ...
plugins/work_assignment/test/unit/work_assingment_plugin_test.rb
... ... @@ -9,12 +9,10 @@ class WorkAssignmentPluginTest &lt; ActiveSupport::TestCase
9 9 work_assignment = WorkAssignmentPlugin::WorkAssignment.create!(:name => 'Work Assignment', :profile => organization)
10 10 content.parent = work_assignment
11 11 content.save!
12   - assert !WorkAssignmentPlugin.is_submission?(content)
13   -
14   - author_folder = work_assignment.find_or_create_author_folder(fast_create(Person))
15   - content.parent = author_folder
16   - content.save!
17 12 assert WorkAssignmentPlugin.is_submission?(content)
  13 +
  14 + author_folder = work_assignment.find_or_create_author_folder(content.author)
  15 + assert_equal author_folder, content.parent
18 16 end
19 17  
20 18 should 'be able to download submission if work_assignment published submissions' do
... ...