suggest_article.rb
2.26 KB
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
class SuggestArticle < Task
  validates_presence_of :target_id
  validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? }
  validates_associated :article_object
  settings_items :email, :type => String
  settings_items :name, :type => String
  settings_items :ip_address, :type => String
  settings_items :user_agent, :type => String
  settings_items :referrer, :type => String
  settings_items :article, :type => Hash, :default => {}
  after_create :schedule_spam_checking
  def schedule_spam_checking
    self.delay.check_for_spam
  end
  include Noosfero::Plugin::HotSpot
  def sender
    requestor ? "#{requestor.name}" : "#{name} (#{email})"
  end
  def author_name
    sender
  end
  def article_object
    if @article_object.nil?
      @article_object = article_type.new(article.merge(target.present? ? {:profile => target} : {}).except(:type))
      if requestor.present?
        @article_object.author = requestor
      else
        @article_object.author_name = name
      end
    end
    @article_object
  end
  def article_type
    if article[:type].present?
      type = article[:type].constantize
      return type if type < Article
    end
    TinyMceArticle
  end
  def perform
    article_object.save!
  end
  def title
    _("Article suggestion")
  end
  def article_name
    article[:name]
  end
  def subject
    article_name
  end
  def information
    variables = requestor.blank? ? {:requestor => sender} : {}
    { :message => _('%{requestor} suggested the publication of the article: %{subject}.'),
      :variables => variables }
  end
  def accept_details
    true
  end
  def icon
    result = {:type => :defined_image, :src => '/images/icons-app/article-minor.png', :name => article_name}
  end
  def target_notification_description
    _('%{requestor} suggested the publication of the article: %{article}.') %
    {:requestor => sender, :article => article_name}
  end
  def target_notification_message
    target_notification_description + "\n\n" +
    _('You need to login on %{system} in order to approve or reject this article.') % { :system => target.environment.name }
  end
  def after_spam!
    SpammerLogger.log(ip_address, self)
    self.delay.marked_as_spam
  end
  def after_ham!
    self.delay.marked_as_ham
  end
end