diff --git a/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb b/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb index b0bd3d5..ef89de3 100644 --- a/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb +++ b/plugins/communities_ratings/controllers/communities_ratings_plugin_profile_controller.rb @@ -47,12 +47,15 @@ class CommunitiesRatingsPluginProfileController < ProfileController community_rating.value = params[:community_rating_value] if params[:community_rating_value] if params[:comments] and (not params[:comments][:body].empty?) - comment = Comment.new(params[:comments]) - comment.author = community_rating.person - comment.community = community_rating.community - comment.save - community_rating.comment = comment + create_comment = CreateCommunityRatingComment.create!( + params[:comments].merge( + :requestor => community_rating.person, + :source => community_rating.community, + :community_rating => community_rating, + :environment => environment + ) + ) end if community_rating.save diff --git a/plugins/communities_ratings/db/migrate/20150806192933_create_community_rating_comment_task.rb b/plugins/communities_ratings/db/migrate/20150806192933_create_community_rating_comment_task.rb new file mode 100644 index 0000000..e5d6a98 --- /dev/null +++ b/plugins/communities_ratings/db/migrate/20150806192933_create_community_rating_comment_task.rb @@ -0,0 +1,13 @@ +class CreateCommunityRatingCommentTask < ActiveRecord::Migration + def up + change_table :tasks do |t| + t.belongs_to :community_rating + t.belongs_to :source, :foreign_key => :source_id + end + end + + def down + remove_column :tasks, :community_ratings_id + remove_column :tasks, :source_id + end +end diff --git a/plugins/communities_ratings/lib/create_community_rating_comment.rb b/plugins/communities_ratings/lib/create_community_rating_comment.rb new file mode 100644 index 0000000..5589d73 --- /dev/null +++ b/plugins/communities_ratings/lib/create_community_rating_comment.rb @@ -0,0 +1,97 @@ +class CreateCommunityRatingComment < Task + include Rails.application.routes.url_helpers + + validates_presence_of :requestor_id, :community_rating, :target_id + + attr_accessible :community_rating, :source, :body, :requestor, :reject_explanation, :environment + belongs_to :source, :class_name => 'Community', :foreign_key => :source_id + belongs_to :community_rating + + alias :environment :target + alias :environment= :target= + + DATA_FIELDS = ['body'] + DATA_FIELDS.each do |field| + settings_items field.to_sym + end + + + def perform + comment = Comment.create!(:source => self.source, :body => self.body, :author => self.requestor) + + self.community_rating.comment = comment + self.community_rating.save! + end + + def title + _("New Comment") + end + + def information + message = _("%{requestor} wants to create a comment in the \"%{source}\" community. \n\n\n Comment: \n\n \"%{body}\"") % + {:requestor => self.requestor.name, :source => self.source.name, :body => self.body } + + {:message => message} + end + + def reject_details + true + end + + # tells if this request was rejected + def rejected? + self.status == Task::Status::CANCELLED + end + + # tells if this request was appoved + def approved? + self.status == Task::Status::FINISHED + end + + def target_notification_description + _('%{requestor} wants to create a comment in the \"%{source}\" community') % + {:requestor => self.requestor.name, :source => self.source.name } + end + + def target_notification_message + _("User \"%{user}\" just requested to create a comment in the \"%{source}\" community. + You have to approve or reject it through the \"Pending Validations\" + section in your control panel.\n") % + { :user => self.requestor.name, :source => self.source.name } + end + + def task_created_message + + _("Your request for commenting at %{source} was + just sent. Environment administrator will receive it and will approve or + reject your request according to his methods and criteria. + + You will be notified as soon as environment administrator has a position + about your request.") % + { :source => self.source.name } + end + + def task_cancelled_message + _("Your request for commenting at %{source} was + not approved by the environment administrator. The following explanation + was given: \n\n%{explanation}") % + { :source => self.source.name, + :explanation => self.reject_explanation } + end + + def task_finished_message + _('Your request for commenting was approved. + You can access %{url} to see your comment.') % + { :url => mount_url } + end + + private + + def mount_url + identifier = self.source.identifier + # The use of url_for doesn't allow the /social within the Public Software + # portal. That's why the url is mounted so 'hard coded' + url = "#{environment.top_url}/profile/#{identifier}" + end + +end -- libgit2 0.21.2