Commit 4fe9e656de8f1516d6efdb53b1bf3cd24cfdf39d

Authored by Tallys Martins
1 parent 99c763b5

Refactoring Work Assignment Plugin

- Removed class Person on Work Assignment Plugin.
- Fixed rails3 file extension on functional Tests.
- changed the email_contact 'from' to be the environment email.
- Changed notification text field to show the user email by default

Signed-off-by: Andre Bernardes <andrebsguedes@gmail.com>
Signed-off-by: Dylan Guedes <djmgguedes@gmail.com>
Signed-off-by: Filipe Ribeiro <firibeiro77@live.com>
Signed-off-by: Dylan Guedes <djmgguedes@gmail.com>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
lib/noosfero/plugin.rb
@@ -422,6 +422,7 @@ class Noosfero::Plugin @@ -422,6 +422,7 @@ class Noosfero::Plugin
422 def article_extra_fields(article) 422 def article_extra_fields(article)
423 nil 423 nil
424 end 424 end
  425 +
425 # -> Adds fields to the signup form 426 # -> Adds fields to the signup form
426 # returns = proc that creates html code 427 # returns = proc that creates html code
427 def signup_extra_contents 428 def signup_extra_contents
plugins/work_assignment/controllers/myprofile/work_assignment_plugin_cms_controller.rb
@@ -1,4 +0,0 @@ @@ -1,4 +0,0 @@
1 -class WorkAssignmentPluginCmsController < CmsController  
2 - append_view_path File.join(File.dirname(__FILE__) + '/../../views')  
3 -  
4 -end  
5 \ No newline at end of file 0 \ No newline at end of file
plugins/work_assignment/controllers/public/work_assignment_plugin_content_viewer_controller.rb 0 → 100644
@@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
  1 +class WorkAssignmentPluginContentViewerController < ContentViewerController
  2 + def toggle_friends_permission
  3 + folder = environment.articles.find_by_id(params[:folder_id])
  4 + puts "#{params[:folder_id]}"
  5 +
  6 + if folder
  7 + author = folder.author
  8 + work_assignment = folder.parent
  9 +
  10 + if !work_assignment.only_friends.include?(author)
  11 + work_assignment.only_friends << author
  12 + else
  13 + work_assignment.only_friends.delete(author)
  14 + end
  15 + end
  16 + redirect_to :action => :index
  17 + #render :action => 'view'
  18 + end
  19 +end
0 \ No newline at end of file 20 \ No newline at end of file
plugins/work_assignment/lib/ext/email_contact.rb
@@ -15,13 +15,13 @@ class EmailContact @@ -15,13 +15,13 @@ class EmailContact
15 attr_accessor :message 15 attr_accessor :message
16 attr_accessor :email 16 attr_accessor :email
17 attr_accessor :receive_a_copy 17 attr_accessor :receive_a_copy
18 - attr_accessor :receiver  
19 attr_accessor :sender 18 attr_accessor :sender
  19 + attr_accessor :receiver
20 20
21 N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name') 21 N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name')
22 22
23 - validates_presence_of :subject, :email, :message, :name  
24 - validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) 23 + validates_presence_of :receiver, :subject, :message, :sender
  24 + validates_format_of :receiver, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?})
25 25
26 def deliver 26 def deliver
27 return false unless self.valid? 27 return false unless self.valid?
@@ -31,29 +31,25 @@ class EmailContact @@ -31,29 +31,25 @@ class EmailContact
31 class EmailSender < ActionMailer::Base 31 class EmailSender < ActionMailer::Base
32 32
33 def notification(email_contact) 33 def notification(email_contact)
34 - @name = email_contact.name  
35 - @email = email_contact.email 34 + @name = email_contact.sender.name
  35 + @email = email_contact.sender.email
36 @message = email_contact.message 36 @message = email_contact.message
37 @target = email_contact.receiver 37 @target = email_contact.receiver
38 38
39 options = { 39 options = {
40 content_type: 'text/html', 40 content_type: 'text/html',
41 - to: email_contact.receiver,  
42 - reply_to: email_contact.email, 41 + to: @target,
  42 + reply_to: @email,
43 subject: email_contact.subject, 43 subject: email_contact.subject,
44 - body: email_contact.message,  
45 - from: "#{email_contact.name} <#{email_contact.email}>" 44 + body: @message,
  45 + from: "#{email_contact.sender.environment.name} <#{email_contact.sender.environment.contact_email}>",
46 } 46 }
47 47
48 - if email_contact.receive_a_copy == "1"  
49 - options.merge!(cc: "#{email_contact.email}")  
50 - end  
51 -  
52 mail(options) 48 mail(options)
53 end 49 end
54 end 50 end
55 51
56 - def build_mail_message(environment, uploaded_files, parent_id) 52 + def build_mail_message!(environment, uploaded_files, parent_id)
57 @article = environment.articles.find_by_id(parent_id) 53 @article = environment.articles.find_by_id(parent_id)
58 @message = "" 54 @message = ""
59 if !@article.nil? && @article.type == "WorkAssignmentPlugin::WorkAssignment" 55 if !@article.nil? && @article.type == "WorkAssignmentPlugin::WorkAssignment"
@@ -63,7 +59,7 @@ class EmailContact @@ -63,7 +59,7 @@ class EmailContact
63 @real_file_url = "http://#{file.url[:host]}:#{file.url[:port]}/#{file.url[:profile]}/#{file.path}" 59 @real_file_url = "http://#{file.url[:host]}:#{file.url[:port]}/#{file.url[:profile]}/#{file.path}"
64 @message += "<br><a href='#{@real_file_url}'>#{@real_file_url}</a>" 60 @message += "<br><a href='#{@real_file_url}'>#{@real_file_url}</a>"
65 end 61 end
66 - @message 62 + self.message = @message
67 end 63 end
68 64
69 end 65 end
plugins/work_assignment/lib/ext/person.rb
@@ -1,7 +0,0 @@ @@ -1,7 +0,0 @@
1 -require_dependency 'person'  
2 -  
3 -class Person  
4 - def build_email_contact(params = {})  
5 - EmailContact.new(params.merge(:name => name, :email => email, :sender => self))  
6 - end  
7 -end  
8 \ No newline at end of file 0 \ No newline at end of file
plugins/work_assignment/lib/work_assignment_plugin.rb
@@ -10,7 +10,19 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin @@ -10,7 +10,19 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin
10 10
11 def self.can_download_submission?(user, submission) 11 def self.can_download_submission?(user, submission)
12 work_assignment = submission.parent.parent 12 work_assignment = submission.parent.parent
13 - work_assignment.publish_submissions || (user && (submission.author == user || user.has_permission?('view_private_content', work_assignment.profile))) 13 +
  14 + if work_assignment.publish_submissions
  15 + if work_assignment.only_friends.include?(submission.author)
  16 + submission.author.friends.include?(user)
  17 + else
  18 + true
  19 + end
  20 + elsif (user && (submission.author == user || user.has_permission?('view_private_content', work_assignment.profile)))
  21 + #work_assignment.publish_submissions || (user && (submission.author == user || user.has_permission?('view_private_content', work_assignment.profile)))
  22 + true
  23 + else
  24 + false
  25 + end
14 end 26 end
15 27
16 def self.is_submission?(content) 28 def self.is_submission?(content)
@@ -55,9 +67,9 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin @@ -55,9 +67,9 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin
55 block = proc do 67 block = proc do
56 if request.post? && params[:uploaded_files] 68 if request.post? && params[:uploaded_files]
57 @email_notification = params[:article_email_notification] 69 @email_notification = params[:article_email_notification]
58 - unless @email_notification.include? "example@example.com, example2@example.com.br"  
59 - @email_contact = user.build_email_contact(:name => user.name, :subject => @parent.name, :email => user.email, :receiver => @email_notification, :sender => user)  
60 - @email_contact.message = @email_contact.build_mail_message(environment, @uploaded_files, @parent.id) 70 + unless !@email_notification || @email_notification.empty?
  71 + @email_contact = EmailContact.new(:subject => @parent.name, :receiver => @email_notification, :sender => user)
  72 + @email_contact.build_mail_message!(environment, @uploaded_files, @parent.id)
61 if @email_contact.deliver 73 if @email_contact.deliver
62 session[:notice] = _('Notification successfully sent') 74 session[:notice] = _('Notification successfully sent')
63 else 75 else
@@ -75,11 +87,11 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin @@ -75,11 +87,11 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin
75 87
76 def article_extra_fields(article) 88 def article_extra_fields(article)
77 proc do 89 proc do
78 - @article = Article.find_by_id(article)  
79 - if params[:parent_id] && !@article.nil? && @article.type == "WorkAssignmentPlugin::WorkAssignment"  
80 - render :partial => 'notify_text_field', :locals => { :size => '45'}  
81 - end 90 + @article = Article.find_by_id(article)
  91 + if params[:parent_id] && !@article.nil? && @article.type == "WorkAssignmentPlugin::WorkAssignment"
  92 + render :partial => 'notify_text_field', :locals => { :size => '45'}
  93 + end
82 end 94 end
83 end 95 end
84 96
85 -end  
86 \ No newline at end of file 97 \ No newline at end of file
  98 +end
plugins/work_assignment/lib/work_assignment_plugin/helper.rb
@@ -15,11 +15,14 @@ module WorkAssignmentPlugin::Helper @@ -15,11 +15,14 @@ module WorkAssignmentPlugin::Helper
15 15
16 def display_author_folder(author_folder, user) 16 def display_author_folder(author_folder, user)
17 return if author_folder.children.empty? 17 return if author_folder.children.empty?
  18 + only_friends = author_folder.parent.only_friends
  19 + action = 'toggle_friends_permission'
18 content_tag('tr', 20 content_tag('tr',
19 content_tag('td', link_to_last_submission(author_folder, user)) + 21 content_tag('td', link_to_last_submission(author_folder, user)) +
20 content_tag('td', time_format(author_folder.children.last.created_at)) + 22 content_tag('td', time_format(author_folder.children.last.created_at)) +
21 content_tag('td', author_folder.children.count, :style => 'text-align: center') + 23 content_tag('td', author_folder.children.count, :style => 'text-align: center') +
22 - content_tag('td', content_tag('button', _('View all versions'), :class => 'view-author-versions', 'data-folder-id' => author_folder.id)) 24 + content_tag('td', content_tag('button', _('View all versions'), :class => 'view-author-versions', 'data-folder-id' => author_folder.id)) +
  25 + content_tag('td', button('toggle_friends_permission', only_friends.include?(author_folder.author) ? _('All') : _('Only Friends'),:controller => 'work_assignment_plugin_content_viewer', :action => action, :folder_id => author_folder.id))
23 ).html_safe + 26 ).html_safe +
24 author_folder.children.map {|submission| display_submission(submission, user)}.join("\n").html_safe 27 author_folder.children.map {|submission| display_submission(submission, user)}.join("\n").html_safe
25 end 28 end
plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb
@@ -2,9 +2,11 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder @@ -2,9 +2,11 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder
2 2
3 settings_items :publish_submissions, :type => :boolean, :default => false 3 settings_items :publish_submissions, :type => :boolean, :default => false
4 settings_items :default_email, :type => :string, :default => "" 4 settings_items :default_email, :type => :string, :default => ""
  5 + settings_items :only_friends, :type => Array, :default => []
5 6
6 attr_accessible :publish_submissions 7 attr_accessible :publish_submissions
7 attr_accessible :default_email 8 attr_accessible :default_email
  9 + attr_accessible :only_friends
8 10
9 def self.icon_name(article = nil) 11 def self.icon_name(article = nil)
10 'work-assignment' 12 'work-assignment'
@@ -37,7 +39,7 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder @@ -37,7 +39,7 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder
37 end 39 end
38 40
39 def find_or_create_author_folder(author) 41 def find_or_create_author_folder(author)
40 - children.find_by_slug(author.name.to_slug) || Folder.create!(:name => author.name, :parent => self, :profile => profile) 42 + children.find_by_slug(author.name.to_slug) || Folder.create!(:name => author.name, :parent => self, :profile => profile, :author => author)
41 end 43 end
42 44
43 def submissions 45 def submissions
@@ -47,6 +49,5 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder @@ -47,6 +49,5 @@ class WorkAssignmentPlugin::WorkAssignment &lt; Folder
47 def cache_key_with_person(params = {}, user = nil, language = 'en') 49 def cache_key_with_person(params = {}, user = nil, language = 'en')
48 cache_key_without_person + (user && profile.members.include?(user) ? "-#{user.identifier}" : '') 50 cache_key_without_person + (user && profile.members.include?(user) ? "-#{user.identifier}" : '')
49 end 51 end
50 - alias_method_chain :cache_key, :person  
51 - 52 + alias_method_chain :cache_key, :person
52 end 53 end
plugins/work_assignment/public/text_field_css.js
@@ -1,22 +0,0 @@ @@ -1,22 +0,0 @@
1 - var element = jQuery("input[name='article_email_notification']");  
2 - var initialVal="example@example.com, example2@example.com.br";  
3 - var isEdited=false;  
4 - element.val(initialVal);  
5 -  
6 - element.focus(function(){  
7 - if(!isEdited){  
8 - element.val("");  
9 - isEdited=true;  
10 - }  
11 -  
12 - });  
13 -  
14 - element.blur(function(){  
15 - if(element.val()==""){  
16 - element.val(initialVal);  
17 - isEdited=false;  
18 - }  
19 -  
20 - });  
21 -  
22 -  
plugins/work_assignment/test/functional/cms_controller_test.rb
1 -require 'test_helper' 1 +require File.expand_path(File.dirname(__FILE__) + "/../../../../test/test_helper")
2 require 'cms_controller' 2 require 'cms_controller'
3 3
4 # Re-raise errors caught by the controller. 4 # Re-raise errors caught by the controller.
@@ -22,7 +22,7 @@ class CmsControllerTest &lt; ActionController::TestCase @@ -22,7 +22,7 @@ class CmsControllerTest &lt; ActionController::TestCase
22 22
23 get :upload_files, :profile => organization.identifier, :parent_id => work_assignment.id 23 get :upload_files, :profile => organization.identifier, :parent_id => work_assignment.id
24 assert_response :forbidden 24 assert_response :forbidden
25 - assert_template 'access_denied.rhtml' 25 + assert_template 'access_denied'
26 26
27 organization.add_member(person) 27 organization.add_member(person)
28 28
plugins/work_assignment/test/functional/content_viewer_controller_test.rb
1 -require 'test_helper' 1 +require File.expand_path(File.dirname(__FILE__) + "/../../../../test/test_helper")
2 require 'content_viewer_controller' 2 require 'content_viewer_controller'
3 3
4 # Re-raise errors caught by the controller. 4 # Re-raise errors caught by the controller.
@@ -31,7 +31,7 @@ class ContentViewerControllerTest &lt; ActionController::TestCase @@ -31,7 +31,7 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
31 31
32 get :view_page, :profile => organization.identifier, :page => submission.explode_path 32 get :view_page, :profile => organization.identifier, :page => submission.explode_path
33 assert_response :forbidden 33 assert_response :forbidden
34 - assert_template 'access_denied.rhtml' 34 + assert_template 'access_denied'
35 35
36 WorkAssignmentPlugin.stubs(:can_download_submission?).returns(true) 36 WorkAssignmentPlugin.stubs(:can_download_submission?).returns(true)
37 37
plugins/work_assignment/views/cms/_notify_text_field.html.erb
1 <h5><%= _('If you want to notify someone about this action, fill the field below with the emails of the destinies, separated by comma.') %></h5> 1 <h5><%= _('If you want to notify someone about this action, fill the field below with the emails of the destinies, separated by comma.') %></h5>
2 2
3 -<%= labelled_text_field(_('Send notification to: '), 'article_email_notification', "", :style => 'width: 60%;') %> 3 +<%= labelled_text_field(_('Send notification to: '), 'article_email_notification', user.email, :style => 'width: 60%;') %>
4 4
5 -<%= javascript_include_tag '../plugins/work_assignment/text_field_css' %>  
6 \ No newline at end of file 5 \ No newline at end of file