cirandas.net

ref: master

plugins/organization_ratings/lib/create_organization_rating_comment.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class CreateOrganizationRatingComment < Task
  include Rails.application.routes.url_helpers

  validates_presence_of :requestor_id, :organization_rating_id, :target_id

  settings_items :organization_rating_id, :type => Integer, :default => nil
  settings_items :organization_rating_comment_id, :type => Integer, :default => nil

  attr_accessible :organization_rating_id, :body, :requestor
  attr_accessible :reject_explanation, :target

  DATA_FIELDS = ['body']
  DATA_FIELDS.each do |field|
    settings_items field.to_sym
  end

  scope :with_rating, -> (user_rating){
    CreateOrganizationRatingComment.find_each do |task|
      return task if(task.organization_rating_id == user_rating.id)
    end
  }

  def perform
    if (self.body && !self.body.blank?)
      comment = Comment.create!(:source => self.target, :body => self.body, :author => self.requestor)

      self.organization_rating_comment_id = comment.id
      link_comment_with_its_rating(comment)
    end
  end

  def link_comment_with_its_rating(user_comment)
    rating = OrganizationRating.find(self.organization_rating_id)
    rating.comment = user_comment
    rating.save
  end

  def accept_details
    true
  end

  def title
    _("New Report")
  end

  def information
    message = _("<a href=%{requestor_url}>%{requestor}</a> wants to leave a report about this %{target_class}") %
    {:requestor_url => url_for(self.requestor.url), :requestor => self.requestor.name, :target_class => _(self.target.class.name)}

    {:message => message}
  end

  def reject_details
    true
  end

  def icon
    {:type => :profile_image, :profile => requestor, :url => requestor.url}
  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 leave a report about this \"%{target}\"") %
    {:requestor => self.requestor.name, :target => _(self.target.class.name.downcase) }
  end

  def target_notification_message
    _("User \"%{user}\" just made a report at %{target_class}
      \"%{target_name}\".
      You have to approve or reject it through the \"Pending Validations\"
      section in your control panel.\n") %
    { :user => self.requestor.name,
      :target_class => _(self.target.class.name.downcase),
      :target_name => self.target.name }
  end

  def task_created_message
    _("Your report at %{target_class} \"%{target}\" was
      just sent. The 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.") %
    { :target_class => _(self.target.class.name.downcase), :target => self.target.name }
  end

  def task_cancelled_message
    _("Your report at %{target_class} \"%{target}\" was
      not approved by the administrator. The following explanation
      was given: \n\n%{explanation}") %
    { :target_class => _(self.target.class.name.downcase),
      :target => self.target.name,
      :explanation => self.reject_explanation }
  end

  def task_finished_message
    _("Your report at %{target_class} \"%{target}\" was approved.
      You can access %{url} to see your comment.") %
    { :target_class => _(self.target.class.name.downcase), :target => self.target.name, :url => ratings_url }
  end

  private

  def ratings_url
    url_for(self.target.public_profile_url) + "/plugin/organization_ratings/new_rating"
  end

end