Sha256: f387480233f3ed0bcea4327dd2d1d87ea87cc395741ce320988a53637ac7da61

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 KB

Contents

module NotifyUser
  class Unsubscribe < ActiveRecord::Base
    self.table_name = "notify_user_unsubscribes"

    # The user to send the notification to
    belongs_to :target, polymorphic: true

    validates_presence_of :target_id, :target_type, :target, :type

    validate :is_unsubscribable

    validates :type, :uniqueness => {:scope => [:target_type, :target_id]}

    self.inheritance_column = :_type_disabled

    if Rails.version.to_i < 4
      attr_accessible :target, :type
    end

    def self.for_target(target)
      where(target_id: target.id)
      .where(target_type: target.class.base_class)
    end

    def self.toggle_status(target, type)
      if NotifyUser::Unsubscribe.has_unsubscribed_from(target, type).empty?
        NotifyUser::Unsubscribe.create(target: target, type: type)
      else
        NotifyUser::Unsubscribe.unsubscribe(target,type)
      end 
    end

    def self.unsubscribe(target, type)
      #deletes unsubscribe object in essence subscribing a user
      where(target_id: target.id)
      .where(target_type: target.class.base_class)
      .where(type: type).destroy_all
    end    

    def self.has_unsubscribed_from(target, type)
      where(target_id: target.id)
      .where(target_type: target.class.base_class)
      .where(type: type)
    end

    private

    #only throw error if both are false
    def is_unsubscribable
      errors.add(:type, ("not found")) if (NotifyUser.unsubscribable_notifications.include? self.type) == false && 
                            NotifyUser::BaseNotification.channels.has_key?(self.type.to_sym) == false
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
notify_user-0.0.20 app/models/notify_user/unsubscribe.rb
notify_user-0.0.19 app/models/notify_user/unsubscribe.rb
notify_user-0.0.18 app/models/notify_user/unsubscribe.rb
notify_user-0.0.16 app/models/notify_user/unsubscribe.rb
notify_user-0.0.15 app/models/notify_user/unsubscribe.rb
notify_user-0.0.14 app/models/notify_user/unsubscribe.rb
notify_user-0.0.12 app/models/notify_user/unsubscribe.rb
notify_user-0.0.10 app/models/notify_user/unsubscribe.rb
notify_user-0.0.9 app/models/notify_user/unsubscribe.rb
notify_user-0.0.8 app/models/notify_user/unsubscribe.rb
notify_user-0.0.7 app/models/notify_user/unsubscribe.rb
notify_user-0.0.6 app/models/notify_user/unsubscribe.rb
notify_user-0.0.5 app/models/notify_user/unsubscribe.rb