aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/refinery/blog/comment.rb
blob: afd806faecbc5b1b23f9f4ad9de207069c3a9181 (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
module Refinery
  module Blog
    class Comment < ActiveRecord::Base

      attr_accessible :name, :email, :message

      filters_spam :author_field => :name,
                   :email_field => :email,
                   :message_field => :body

      belongs_to :post, :foreign_key => 'blog_post_id'

      acts_as_indexed :fields => [:name, :email, :message]

      alias_attribute :message, :body

      validates :name, :message, :presence => true
      validates :email, :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i }

      class << self
        def unmoderated
          where(:state => nil)
        end

        def approved
          where(:state => 'approved')
        end

        def rejected
          where(:state => 'rejected')
        end
      end

      self.per_page = Refinery::Blog.comments_per_page

      def approve!
        self.update_column(:state, 'approved')
      end

      def reject!
        self.update_column(:state, 'rejected')
      end

      def rejected?
        self.state == 'rejected'
      end

      def approved?
        self.state == 'approved'
      end

      def unmoderated?
        self.state.nil?
      end

      def self.toggle!
        currently = Refinery::Setting.find_or_set(:comments_allowed, true, {
          :scoping => 'blog'
        })
        Refinery::Setting.set(:comments_allowed, {:value => !currently, :scoping => 'blog'})
      end

      before_create do |comment|
        unless Moderation.enabled?
          comment.state = comment.ham? ? 'approved' : 'rejected'
        end
      end

      module Moderation
        class << self
          def enabled?
            Refinery::Setting.find_or_set(:comment_moderation, true, {
              :scoping => 'blog',
              :restricted => false
            })
          end

          def toggle!
            new_value = {
              :value => !Blog::Comment::Moderation.enabled?,
              :scoping => 'blog',
              :restricted => false
            }
            Refinery::Setting.set(:comment_moderation, new_value)
          end
        end
      end

      module Notification
        class << self
          def recipients
            Refinery::Setting.find_or_set(:comment_notification_recipients, (Refinery::Role[:refinery].users.first.email rescue ''),
            {
              :scoping => 'blog',
              :restricted => false
            })
          end

          def recipients=(emails)
            new_value = {
              :value => emails,
              :scoping => 'blog',
              :restricted => false
            }
            Refinery::Setting.set(:comment_notification_recipients, new_value)
          end

          def subject
            Refinery::Setting.find_or_set(:comment_notification_subject, "New inquiry from your website", {
              :scoping => 'blog',
              :restricted => false
            })
          end

          def subject=(subject_line)
            new_value = {
              :value => subject_line,
              :scoping => 'blog',
              :restricted => false
            }
            Refinery::Setting.set(:comment_notification_subject, new_value)
          end
        end
      end

    end
  end
end