Commit c13119fbe652161a543c379529453bc9435e4ea5

Authored by Tallys Martins
1 parent c9b33c17

Recatoring Work Assignment Plugin to fit the merge

 - Moved EmailContact class from ext directory to plugin scope
 - Refactored Work Assignment Plugin to accept general articles instead of only uploaded files
 - Protecting methods that are not supposed to be actions

Signed-off-by: Tallys Martins <tallysmartins@gmail.com>
Signed-off-by: André Bernardes <andrebsguedes@gmail.com>
Signed-off-by: Hebert Douglas <hebertdougl@gmail>
plugins/work_assignment/controllers/work_assignment_plugin_myprofile_controller.rb
@@ -5,13 +5,6 @@ helper CmsHelper @@ -5,13 +5,6 @@ helper CmsHelper
5 5
6 before_filter :protect_if, :only => [:edit_visibility] 6 before_filter :protect_if, :only => [:edit_visibility]
7 7
8 -def protect_if  
9 - article = environment.articles.find_by_id(params[:article_id])  
10 - render_access_denied unless (user && !article.nil? && (user.is_member_of? article.profile) &&  
11 - article.parent.allow_visibility_edition && article.folder? &&  
12 - (article.author == user || user.has_permission?('view_private_content', profile)))  
13 -end  
14 -  
15 def edit_visibility 8 def edit_visibility
16 unless params[:article_id].blank? 9 unless params[:article_id].blank?
17 folder = profile.environment.articles.find_by_id(params[:article_id]) 10 folder = profile.environment.articles.find_by_id(params[:article_id])
@@ -32,4 +25,14 @@ def edit_visibility @@ -32,4 +25,14 @@ def edit_visibility
32 result = profile.members.find(:all, :conditions => ['LOWER(name) LIKE ?', "%#{arg}%"]) 25 result = profile.members.find(:all, :conditions => ['LOWER(name) LIKE ?', "%#{arg}%"])
33 render :text => prepare_to_token_input(result).to_json 26 render :text => prepare_to_token_input(result).to_json
34 end 27 end
  28 +
  29 + protected
  30 +
  31 + def protect_if
  32 + article = environment.articles.find_by_id(params[:article_id])
  33 + render_access_denied unless (user && !article.nil? && (user.is_member_of? article.profile) &&
  34 + article.parent.allow_visibility_edition && article.folder? &&
  35 + (article.author == user || user.has_permission?('view_private_content', profile)))
  36 + end
  37 +
35 end 38 end
plugins/work_assignment/lib/ext/article.rb 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +require_dependency 'article'
  2 +
  3 +class Article
  4 + before_validation :work_assignment_save_into_author_folder
  5 + after_validation :work_assignment_change_visibility
  6 +
  7 + def work_assignment_save_into_author_folder
  8 + if not self.is_a? Folder and self.parent.kind_of? WorkAssignmentPlugin::WorkAssignment
  9 + author_folder = self.parent.find_or_create_author_folder(self.author)
  10 + self.name = WorkAssignmentPlugin::WorkAssignment.versioned_name(self, author_folder)
  11 + self.parent = author_folder
  12 + end
  13 + end
  14 +
  15 + def work_assignment_change_visibility
  16 + if self.parent && self.parent.parent && self.parent.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment)
  17 + self.published = self.parent.published
  18 + end
  19 + end
  20 +end
0 \ No newline at end of file 21 \ No newline at end of file
plugins/work_assignment/lib/ext/email_contact.rb
@@ -1,65 +0,0 @@ @@ -1,65 +0,0 @@
1 -class EmailContact  
2 -  
3 - include ActiveModel::Validations  
4 -  
5 - def initialize(attributes = nil)  
6 - if attributes  
7 - attributes.each do |attr,value|  
8 - self.send("#{attr}=", value)  
9 - end  
10 - end  
11 - end  
12 -  
13 - attr_accessor :name  
14 - attr_accessor :subject  
15 - attr_accessor :message  
16 - attr_accessor :email  
17 - attr_accessor :receive_a_copy  
18 - attr_accessor :sender  
19 - attr_accessor :receiver  
20 -  
21 - N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name')  
22 -  
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 -  
26 - def deliver  
27 - return false unless self.valid?  
28 - EmailContact::EmailSender.notification(self).deliver  
29 - end  
30 -  
31 - class EmailSender < ActionMailer::Base  
32 -  
33 - def notification(email_contact)  
34 - name = email_contact.sender.name  
35 - email = email_contact.sender.email  
36 - message = email_contact.message  
37 - target = email_contact.receiver  
38 -  
39 - options = {  
40 - content_type: 'text/html',  
41 - to: target,  
42 - reply_to: email,  
43 - subject: email_contact.subject,  
44 - body: message,  
45 - from: "#{email_contact.sender.environment.name} <#{email_contact.sender.environment.contact_email}>",  
46 - }  
47 -  
48 - mail(options)  
49 - end  
50 - end  
51 -  
52 - def build_mail_message!(environment, uploaded_files, parent_id)  
53 - article = environment.articles.find_by_id(parent_id)  
54 - message = ""  
55 - if !article.nil? && article.kind_of?(WorkAssignmentPlugin::WorkAssignment)  
56 - message = article.default_email + "<br>"  
57 - end  
58 - uploaded_files.each do |file|  
59 - file_url = "http://#{file.url[:host]}:#{file.url[:port]}/#{file.url[:profile]}/#{file.path}"  
60 - message += "<br><a href='#{file_url}'>#{file_url}</a>"  
61 - end  
62 - self.message = message  
63 - end  
64 -  
65 -end  
plugins/work_assignment/lib/ext/uploaded_file.rb
@@ -1,18 +0,0 @@ @@ -1,18 +0,0 @@
1 -require_dependency 'article'  
2 -require_dependency 'uploaded_file'  
3 -  
4 -class UploadedFile < Article  
5 - before_validation do |uploaded_file|  
6 - if uploaded_file.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment)  
7 - author_folder = uploaded_file.parent.find_or_create_author_folder(uploaded_file.author)  
8 - uploaded_file.name = WorkAssignmentPlugin::WorkAssignment.versioned_name(uploaded_file, author_folder)  
9 - uploaded_file.parent = author_folder  
10 - end  
11 - end  
12 -  
13 - after_validation do |uploaded_file|  
14 - if uploaded_file.parent && uploaded_file.parent.parent && uploaded_file.parent.parent.kind_of?(WorkAssignmentPlugin::WorkAssignment)  
15 - uploaded_file.published = uploaded_file.parent.published  
16 - end  
17 - end  
18 -end  
plugins/work_assignment/lib/work_assignment_plugin.rb
@@ -56,8 +56,8 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin @@ -56,8 +56,8 @@ class WorkAssignmentPlugin &lt; Noosfero::Plugin
56 if request.post? && params[:uploaded_files] 56 if request.post? && params[:uploaded_files]
57 email_notification = params[:article_email_notification] 57 email_notification = params[:article_email_notification]
58 unless !email_notification || email_notification.empty? 58 unless !email_notification || email_notification.empty?
59 - email_contact = EmailContact.new(:subject => @parent.name, :receiver => email_notification, :sender => user)  
60 - email_contact.build_mail_message!(environment, @uploaded_files, @parent.id) 59 + email_contact = WorkAssignmentPlugin::EmailContact.new(:subject => @parent.name, :receiver => email_notification, :sender => user)
  60 + WorkAssignmentPlugin::EmailContact::EmailSender.build_mail_message(email_contact, @uploaded_files)
61 if email_contact.deliver 61 if email_contact.deliver
62 session[:notice] = _('Notification successfully sent') 62 session[:notice] = _('Notification successfully sent')
63 else 63 else
plugins/work_assignment/lib/work_assignment_plugin/email_contact.rb 0 → 100644
@@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
  1 +class WorkAssignmentPlugin::EmailContact
  2 +
  3 + include ActiveModel::Validations
  4 +
  5 + def initialize(attributes = nil)
  6 + if attributes
  7 + attributes.each do |attr,value|
  8 + self.send("#{attr}=", value)
  9 + end
  10 + end
  11 + end
  12 +
  13 + attr_accessor :name
  14 + attr_accessor :subject
  15 + attr_accessor :message
  16 + attr_accessor :email
  17 + attr_accessor :receive_a_copy
  18 + attr_accessor :sender
  19 + attr_accessor :receiver
  20 +
  21 + N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name')
  22 +
  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 +
  26 + def deliver
  27 + return false unless self.valid?
  28 + WorkAssignmentPlugin::EmailContact::EmailSender.notification(self).deliver
  29 + end
  30 +
  31 + class EmailSender < ActionMailer::Base
  32 +
  33 + def notification(email_contact)
  34 + name = email_contact.sender.name
  35 + email = email_contact.sender.email
  36 + message = email_contact.message
  37 + target = email_contact.receiver
  38 +
  39 + options = {
  40 + content_type: 'text/html',
  41 + to: target,
  42 + reply_to: email,
  43 + subject: email_contact.subject,
  44 + body: message,
  45 + from: "#{email_contact.sender.environment.name} <#{email_contact.sender.environment.contact_email}>",
  46 + }
  47 +
  48 + mail(options)
  49 + end
  50 +
  51 + def build_mail_message(email_contact, uploaded_files)
  52 + message = ""
  53 + if uploaded_files && uploaded_files.first && uploaded_files.first.parent && uploaded_files.first.parent.parent
  54 + article = uploaded_files.first.parent.parent
  55 + message = article.default_email + "<br>"
  56 + uploaded_files.each do |file|
  57 + url = url_for(file.url)
  58 + message += "<br><a href='#{url}'>#{url}</a>"
  59 + end
  60 + end
  61 + email_contact.message = message
  62 + end
  63 + end
  64 +end