cirandas.net

ref: master

app/models/approve_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
class ApproveComment < Task
  validates_presence_of :target_id

  settings_items :comment_attributes, :closing_statment

  validates_presence_of :comment_attributes

  def comment
    unless @comment || self.comment_attributes.nil?
      @comment = Comment.new
      @comment.assign_attributes(ActiveSupport::JSON.decode(self.comment_attributes.to_s), :without_protection => true)
    end
    @comment
  end

  def requestor_name
    requestor ? requestor.name : (comment.name || _('Anonymous'))
  end

  def article
    Article.find_by id: comment.source_id unless self.comment.nil?
  end

  def article_name
    article ? article.name : _("Article removed.")
  end

  def perform
    comment.save!
  end

  def title
    _("New comment to article")
  end

  def icon
    result = {:type => :defined_image, :src => '/images/icons-app/article-minor.png'}
    result.merge!({:url => article.url}) if article
    result
  end

  def linked_subject
    {:text => article_name, :url => article.url} if article
  end

  def information
    if article
      if requestor
        {:message => _('%{requestor} commented on the article: %{linked_subject}.')}
      else
        { :message => _('%{requestor} commented on the article: %{linked_subject}.'),
          :variables => {:requestor => requestor_name} }
      end
    else
      {:message => _("The article was removed.")}
    end
  end

  def accept_details
    true
  end

  def reject_details
    true
  end

  def default_decision
    if article
      'skip'
    else
      'reject'
    end
  end

  def accept_disabled?
    article.blank?
  end

  def target_notification_description
    if article
      _('%{requestor} wants to comment the article: %{article}.') % {:requestor => requestor_name, :article => article.name}
    else
      _('%{requestor} wanted to comment the article but it was removed.') % {:requestor => requestor_name}
    end
  end

  def target_notification_message
    target_notification_description + "\n\n" +
    _('You need to login on %{system} in order to approve or reject this comment.') % { :system => target.environment.name }
  end

  def task_finished_message
    if !closing_statment.blank?
      _("Your comment to the article \"%{article}\" was approved. Here is the comment left by the admin who approved your comment:\n\n%{comment} ") % {:article => article_name, :comment => closing_statment}
    else
      _('Your request for comment the article "%{article}" was approved.') % {:article => article_name}
    end
  end

  def task_cancelled_message
    message = _('Your request for commenting the article "%{article}" was rejected.') % {:article => article_name}
    if !reject_explanation.blank?
      message += " " + _("Here is the reject explanation left by the administrator who rejected your comment: \n\n%{reject_explanation}") % {:reject_explanation => reject_explanation}
    end
    message
  end

end