Sha256: 2f8aa9af11b3b5a1d1931825b7f7390dde2e679a29c4ae71f9683e614cdaa3be

Contents?: true

Size: 1.25 KB

Versions: 13

Compression:

Stored size: 1.25 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

    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

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
notify_user-0.1.4 app/models/notify_user/unsubscribe.rb
notify_user-0.1.3 app/models/notify_user/unsubscribe.rb
notify_user-0.1.2 app/models/notify_user/unsubscribe.rb
notify_user-0.1.1 app/models/notify_user/unsubscribe.rb
notify_user-0.0.30 app/models/notify_user/unsubscribe.rb
notify_user-0.0.29 app/models/notify_user/unsubscribe.rb
notify_user-0.0.28 app/models/notify_user/unsubscribe.rb
notify_user-0.0.27 app/models/notify_user/unsubscribe.rb
notify_user-0.0.26 app/models/notify_user/unsubscribe.rb
notify_user-0.0.25 app/models/notify_user/unsubscribe.rb
notify_user-0.0.24 app/models/notify_user/unsubscribe.rb
notify_user-0.0.23 app/models/notify_user/unsubscribe.rb
notify_user-0.0.22 app/models/notify_user/unsubscribe.rb