Sha256: ca54e201959f1190a150491a1647c2a94a1329398062bf4a6a828dc868561622

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

module OpenConferenceWare

  # == Schema Information
  #
  # Table name: user_favorites
  #
  #  id          :integer          not null, primary key
  #  user_id     :integer
  #  proposal_id :integer
  #  created_at  :datetime
  #  updated_at  :datetime
  #

  class UserFavorite < OpenConferenceWare::Base
    # Associations
    belongs_to :user
    belongs_to :proposal

    # Validations
    validates_presence_of :user_id
    validates_presence_of :proposal_id

    # Add a favorite. Creates record if needed, else leaves as-is.
    def self.add(user_id, proposal_id)
      return self.find_or_create_by(user_id: user_id, proposal_id: proposal_id)
    end

    # Remove a favorite. Removes record if needed, else does nothing.
    def self.remove(user_id, proposal_id)
      if record = self.find_by_user_id_and_proposal_id(user_id, proposal_id)
        return record.destroy
      else
        return false
      end
    end

    # Return the ids of this +user+'s favorite proposals.
    def self.proposal_ids_for(user)
      return self.where(user_id: user.id).select('proposal_id').map(&:proposal_id)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
open_conference_ware-1.0.0.pre4 app/models/open_conference_ware/user_favorite.rb
open_conference_ware-1.0.0.pre3 app/models/open_conference_ware/user_favorite.rb
open_conference_ware-1.0.0.pre2 app/models/open_conference_ware/user_favorite.rb
open_conference_ware-1.0.0.pre1 app/models/open_conference_ware/user_favorite.rb