From 4fe9e656de8f1516d6efdb53b1bf3cd24cfdf39d Mon Sep 17 00:00:00 2001 From: Tallys Martins Date: Fri, 24 Oct 2014 20:10:33 +0000 Subject: [PATCH] Refactoring Work Assignment Plugin --- lib/noosfero/plugin.rb | 1 + plugins/work_assignment/controllers/myprofile/work_assignment_plugin_cms_controller.rb | 4 ---- plugins/work_assignment/controllers/public/work_assignment_plugin_content_viewer_controller.rb | 19 +++++++++++++++++++ plugins/work_assignment/lib/ext/email_contact.rb | 26 +++++++++++--------------- plugins/work_assignment/lib/ext/person.rb | 7 ------- plugins/work_assignment/lib/work_assignment_plugin.rb | 30 +++++++++++++++++++++--------- plugins/work_assignment/lib/work_assignment_plugin/helper.rb | 5 ++++- plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb | 7 ++++--- plugins/work_assignment/public/text_field_css.js | 22 ---------------------- plugins/work_assignment/test/functional/cms_controller_test.rb | 4 ++-- plugins/work_assignment/test/functional/content_viewer_controller_test.rb | 4 ++-- plugins/work_assignment/views/cms/_notify_text_field.html.erb | 3 +-- 12 files changed, 65 insertions(+), 67 deletions(-) delete mode 100644 plugins/work_assignment/controllers/myprofile/work_assignment_plugin_cms_controller.rb create mode 100644 plugins/work_assignment/controllers/public/work_assignment_plugin_content_viewer_controller.rb delete mode 100644 plugins/work_assignment/lib/ext/person.rb delete mode 100644 plugins/work_assignment/public/text_field_css.js diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index a2e644b..575f3df 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -422,6 +422,7 @@ class Noosfero::Plugin def article_extra_fields(article) nil end + # -> Adds fields to the signup form # returns = proc that creates html code def signup_extra_contents diff --git a/plugins/work_assignment/controllers/myprofile/work_assignment_plugin_cms_controller.rb b/plugins/work_assignment/controllers/myprofile/work_assignment_plugin_cms_controller.rb deleted file mode 100644 index 7a63d98..0000000 --- a/plugins/work_assignment/controllers/myprofile/work_assignment_plugin_cms_controller.rb +++ /dev/null @@ -1,4 +0,0 @@ -class WorkAssignmentPluginCmsController < CmsController - append_view_path File.join(File.dirname(__FILE__) + '/../../views') - -end \ No newline at end of file diff --git a/plugins/work_assignment/controllers/public/work_assignment_plugin_content_viewer_controller.rb b/plugins/work_assignment/controllers/public/work_assignment_plugin_content_viewer_controller.rb new file mode 100644 index 0000000..7919205 --- /dev/null +++ b/plugins/work_assignment/controllers/public/work_assignment_plugin_content_viewer_controller.rb @@ -0,0 +1,19 @@ +class WorkAssignmentPluginContentViewerController < ContentViewerController + def toggle_friends_permission + folder = environment.articles.find_by_id(params[:folder_id]) + puts "#{params[:folder_id]}" + + if folder + author = folder.author + work_assignment = folder.parent + + if !work_assignment.only_friends.include?(author) + work_assignment.only_friends << author + else + work_assignment.only_friends.delete(author) + end + end + redirect_to :action => :index + #render :action => 'view' + end +end \ No newline at end of file diff --git a/plugins/work_assignment/lib/ext/email_contact.rb b/plugins/work_assignment/lib/ext/email_contact.rb index 9541334..5a25fb0 100644 --- a/plugins/work_assignment/lib/ext/email_contact.rb +++ b/plugins/work_assignment/lib/ext/email_contact.rb @@ -15,13 +15,13 @@ class EmailContact attr_accessor :message attr_accessor :email attr_accessor :receive_a_copy - attr_accessor :receiver attr_accessor :sender + attr_accessor :receiver N_('Subject'); N_('Message'); N_('e-Mail'); N_('Name') - validates_presence_of :subject, :email, :message, :name - validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) + validates_presence_of :receiver, :subject, :message, :sender + validates_format_of :receiver, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) def deliver return false unless self.valid? @@ -31,29 +31,25 @@ class EmailContact class EmailSender < ActionMailer::Base def notification(email_contact) - @name = email_contact.name - @email = email_contact.email + @name = email_contact.sender.name + @email = email_contact.sender.email @message = email_contact.message @target = email_contact.receiver options = { content_type: 'text/html', - to: email_contact.receiver, - reply_to: email_contact.email, + to: @target, + reply_to: @email, subject: email_contact.subject, - body: email_contact.message, - from: "#{email_contact.name} <#{email_contact.email}>" + body: @message, + from: "#{email_contact.sender.environment.name} <#{email_contact.sender.environment.contact_email}>", } - if email_contact.receive_a_copy == "1" - options.merge!(cc: "#{email_contact.email}") - end - mail(options) end end - def build_mail_message(environment, uploaded_files, parent_id) + def build_mail_message!(environment, uploaded_files, parent_id) @article = environment.articles.find_by_id(parent_id) @message = "" if !@article.nil? && @article.type == "WorkAssignmentPlugin::WorkAssignment" @@ -63,7 +59,7 @@ class EmailContact @real_file_url = "http://#{file.url[:host]}:#{file.url[:port]}/#{file.url[:profile]}/#{file.path}" @message += "
#{@real_file_url}" end - @message + self.message = @message end end diff --git a/plugins/work_assignment/lib/ext/person.rb b/plugins/work_assignment/lib/ext/person.rb deleted file mode 100644 index 0eb64c3..0000000 --- a/plugins/work_assignment/lib/ext/person.rb +++ /dev/null @@ -1,7 +0,0 @@ -require_dependency 'person' - -class Person - def build_email_contact(params = {}) - EmailContact.new(params.merge(:name => name, :email => email, :sender => self)) - end -end \ No newline at end of file diff --git a/plugins/work_assignment/lib/work_assignment_plugin.rb b/plugins/work_assignment/lib/work_assignment_plugin.rb index 5ebe57e..405ba93 100644 --- a/plugins/work_assignment/lib/work_assignment_plugin.rb +++ b/plugins/work_assignment/lib/work_assignment_plugin.rb @@ -10,7 +10,19 @@ class WorkAssignmentPlugin < Noosfero::Plugin def self.can_download_submission?(user, submission) work_assignment = submission.parent.parent - work_assignment.publish_submissions || (user && (submission.author == user || user.has_permission?('view_private_content', work_assignment.profile))) + + if work_assignment.publish_submissions + if work_assignment.only_friends.include?(submission.author) + submission.author.friends.include?(user) + else + true + end + elsif (user && (submission.author == user || user.has_permission?('view_private_content', work_assignment.profile))) + #work_assignment.publish_submissions || (user && (submission.author == user || user.has_permission?('view_private_content', work_assignment.profile))) + true + else + false + end end def self.is_submission?(content) @@ -55,9 +67,9 @@ class WorkAssignmentPlugin < Noosfero::Plugin block = proc do if request.post? && params[:uploaded_files] @email_notification = params[:article_email_notification] - unless @email_notification.include? "example@example.com, example2@example.com.br" - @email_contact = user.build_email_contact(:name => user.name, :subject => @parent.name, :email => user.email, :receiver => @email_notification, :sender => user) - @email_contact.message = @email_contact.build_mail_message(environment, @uploaded_files, @parent.id) + unless !@email_notification || @email_notification.empty? + @email_contact = EmailContact.new(:subject => @parent.name, :receiver => @email_notification, :sender => user) + @email_contact.build_mail_message!(environment, @uploaded_files, @parent.id) if @email_contact.deliver session[:notice] = _('Notification successfully sent') else @@ -75,11 +87,11 @@ class WorkAssignmentPlugin < Noosfero::Plugin def article_extra_fields(article) proc do - @article = Article.find_by_id(article) - if params[:parent_id] && !@article.nil? && @article.type == "WorkAssignmentPlugin::WorkAssignment" - render :partial => 'notify_text_field', :locals => { :size => '45'} - end + @article = Article.find_by_id(article) + if params[:parent_id] && !@article.nil? && @article.type == "WorkAssignmentPlugin::WorkAssignment" + render :partial => 'notify_text_field', :locals => { :size => '45'} + end end end -end \ No newline at end of file +end diff --git a/plugins/work_assignment/lib/work_assignment_plugin/helper.rb b/plugins/work_assignment/lib/work_assignment_plugin/helper.rb index a0ba207..6423f2d 100644 --- a/plugins/work_assignment/lib/work_assignment_plugin/helper.rb +++ b/plugins/work_assignment/lib/work_assignment_plugin/helper.rb @@ -15,11 +15,14 @@ module WorkAssignmentPlugin::Helper def display_author_folder(author_folder, user) return if author_folder.children.empty? + only_friends = author_folder.parent.only_friends + action = 'toggle_friends_permission' content_tag('tr', content_tag('td', link_to_last_submission(author_folder, user)) + content_tag('td', time_format(author_folder.children.last.created_at)) + content_tag('td', author_folder.children.count, :style => 'text-align: center') + - content_tag('td', content_tag('button', _('View all versions'), :class => 'view-author-versions', 'data-folder-id' => author_folder.id)) + content_tag('td', content_tag('button', _('View all versions'), :class => 'view-author-versions', 'data-folder-id' => author_folder.id)) + + 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)) ).html_safe + author_folder.children.map {|submission| display_submission(submission, user)}.join("\n").html_safe end 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 616090c..0f95034 100644 --- a/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb +++ b/plugins/work_assignment/lib/work_assignment_plugin/work_assignment.rb @@ -2,9 +2,11 @@ class WorkAssignmentPlugin::WorkAssignment < Folder settings_items :publish_submissions, :type => :boolean, :default => false settings_items :default_email, :type => :string, :default => "" + settings_items :only_friends, :type => Array, :default => [] attr_accessible :publish_submissions attr_accessible :default_email + attr_accessible :only_friends def self.icon_name(article = nil) 'work-assignment' @@ -37,7 +39,7 @@ class WorkAssignmentPlugin::WorkAssignment < Folder end def find_or_create_author_folder(author) - children.find_by_slug(author.name.to_slug) || Folder.create!(:name => author.name, :parent => self, :profile => profile) + children.find_by_slug(author.name.to_slug) || Folder.create!(:name => author.name, :parent => self, :profile => profile, :author => author) end def submissions @@ -47,6 +49,5 @@ class WorkAssignmentPlugin::WorkAssignment < Folder def cache_key_with_person(params = {}, user = nil, language = 'en') cache_key_without_person + (user && profile.members.include?(user) ? "-#{user.identifier}" : '') end - alias_method_chain :cache_key, :person - + alias_method_chain :cache_key, :person end diff --git a/plugins/work_assignment/public/text_field_css.js b/plugins/work_assignment/public/text_field_css.js deleted file mode 100644 index 32c134e..0000000 --- a/plugins/work_assignment/public/text_field_css.js +++ /dev/null @@ -1,22 +0,0 @@ - var element = jQuery("input[name='article_email_notification']"); - var initialVal="example@example.com, example2@example.com.br"; - var isEdited=false; - element.val(initialVal); - - element.focus(function(){ - if(!isEdited){ - element.val(""); - isEdited=true; - } - - }); - - element.blur(function(){ - if(element.val()==""){ - element.val(initialVal); - isEdited=false; - } - - }); - - diff --git a/plugins/work_assignment/test/functional/cms_controller_test.rb b/plugins/work_assignment/test/functional/cms_controller_test.rb index 6766aa3..49b110b 100644 --- a/plugins/work_assignment/test/functional/cms_controller_test.rb +++ b/plugins/work_assignment/test/functional/cms_controller_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require File.expand_path(File.dirname(__FILE__) + "/../../../../test/test_helper") require 'cms_controller' # Re-raise errors caught by the controller. @@ -22,7 +22,7 @@ class CmsControllerTest < ActionController::TestCase get :upload_files, :profile => organization.identifier, :parent_id => work_assignment.id assert_response :forbidden - assert_template 'access_denied.rhtml' + assert_template 'access_denied' organization.add_member(person) diff --git a/plugins/work_assignment/test/functional/content_viewer_controller_test.rb b/plugins/work_assignment/test/functional/content_viewer_controller_test.rb index dc6b28f..9de1af2 100644 --- a/plugins/work_assignment/test/functional/content_viewer_controller_test.rb +++ b/plugins/work_assignment/test/functional/content_viewer_controller_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require File.expand_path(File.dirname(__FILE__) + "/../../../../test/test_helper") require 'content_viewer_controller' # Re-raise errors caught by the controller. @@ -31,7 +31,7 @@ class ContentViewerControllerTest < ActionController::TestCase get :view_page, :profile => organization.identifier, :page => submission.explode_path assert_response :forbidden - assert_template 'access_denied.rhtml' + assert_template 'access_denied' WorkAssignmentPlugin.stubs(:can_download_submission?).returns(true) diff --git a/plugins/work_assignment/views/cms/_notify_text_field.html.erb b/plugins/work_assignment/views/cms/_notify_text_field.html.erb index 491fb5f..2190d13 100644 --- a/plugins/work_assignment/views/cms/_notify_text_field.html.erb +++ b/plugins/work_assignment/views/cms/_notify_text_field.html.erb @@ -1,5 +1,4 @@
<%= _('If you want to notify someone about this action, fill the field below with the emails of the destinies, separated by comma.') %>
-<%= labelled_text_field(_('Send notification to: '), 'article_email_notification', "", :style => 'width: 60%;') %> +<%= labelled_text_field(_('Send notification to: '), 'article_email_notification', user.email, :style => 'width: 60%;') %> -<%= javascript_include_tag '../plugins/work_assignment/text_field_css' %> \ No newline at end of file -- libgit2 0.21.2