Sha256: 13fb6e4ac545c6e950fa273e2af01c5284152851ec68fe32476a289a0de17860

Contents?: true

Size: 803 Bytes

Versions: 2

Compression:

Stored size: 803 Bytes

Contents

# If you want to use this module, you need to add a boolean field
# 'marked_as_spam' to the model.
module Concerns
  module Spammable
    extend ActiveSupport::Concern

    included do
      include Rakismet::Model

      scope :spam, ->{ where(marked_as_spam: true) }
      scope :not_spam, -> { where('marked_as_spam = 0 OR marked_as_spam IS NULL') }

      after_initialize :spam_defaults
    end

    module ClassMethods
      def spammable(*args)
        rakismet_attrs *args
      end
    end

    def mark_as_spam!
      update_attribute :marked_as_spam, true
    end

    def unmark_as_spam!
      update_attribute :marked_as_spam, false
    end

    private

    def spam_defaults
      if self.new_record?
        self.marked_as_spam = false if marked_as_spam.nil?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
udongo-1.0.0 app/models/concerns/spammable.rb
udongo-0.1.0 app/models/concerns/spammable.rb