class BookmarkSystem::Bookmark

Bookmark class

This class defines the bookmark model in bookmark system

Public Class Methods

bookmark(bookmarker, bookmarkee) click to toggle source

Creates a {Bookmark} relationship between a {Bookmarker} object and a {Bookmarkee} object

@param [Bookmarker] bookmarker - the {Bookmarker} of the relationship @param [Bookmarkee] bookmarkee - the {Bookmarkee} of the relationship @return [Boolean]

# File lib/bookmark_system/bookmark.rb, line 30
def self.bookmark(bookmarker, bookmarkee)
  validate_bookmarkee(bookmarkee)
  validate_bookmarker(bookmarker)

  if bookmarks?(bookmarker, bookmarkee)
    false
  else
    bookmark = scope_by_bookmarker(bookmarker).scope_by_bookmarkee(bookmarkee).build
    bookmark.save
    true
  end
end
bookmarks?(bookmarker, bookmarkee) click to toggle source

Specifies if a {Bookmarker} object bookmarks a {Bookmarkee} object

@param [Bookmarker] bookmarker - the {Bookmarker} object to test against @param [Bookmarkee] bookmarkee - the {Bookmarkee} object to test against @return [Boolean]

# File lib/bookmark_system/bookmark.rb, line 88
def self.bookmarks?(bookmarker, bookmarkee)
  validate_bookmarkee(bookmarkee)
  validate_bookmarker(bookmarker)

  scope_by_bookmarker(bookmarker).scope_by_bookmarkee(bookmarkee).exists?
end
scope_by_bookmarkee(bookmarkee) click to toggle source

Retrieves a scope of {Bookmark} objects filtered by a {Bookmarkee} object

@param [Bookmarkee] bookmarkee - the {Bookmarkee} to filter @return [ActiveRecord::Relation]

# File lib/bookmark_system/bookmark.rb, line 101
def self.scope_by_bookmarkee(bookmarkee)
  where(bookmarkee: bookmarkee)
end
scope_by_bookmarkee_type(klass) click to toggle source

Retrieves a scope of {Bookmark} objects filtered by a {Bookmarkee} type

@param [Class] klass - the {Class} to filter @return [ActiveRecord::Relation]

# File lib/bookmark_system/bookmark.rb, line 111
def self.scope_by_bookmarkee_type(klass)
  where(bookmarkee_type: klass.to_s.classify)
end
scope_by_bookmarker(bookmarker) click to toggle source

Retrieves a scope of {Bookmark} objects filtered by a {Bookmarker} object

@param [Bookmarker] bookmarker - the {Bookmarker} to filter @return [ActiveRecord::Relation]

# File lib/bookmark_system/bookmark.rb, line 121
def self.scope_by_bookmarker(bookmarker)
  where(bookmarker: bookmarker)
end
scope_by_bookmarker_type(klass) click to toggle source

Retrieves a scope of {Bookmark} objects filtered by a {Bookmarker} type

@param [Class] klass - the {Class} to filter @return [ActiveRecord::Relation]

# File lib/bookmark_system/bookmark.rb, line 131
def self.scope_by_bookmarker_type(klass)
  where(bookmarker_type: klass.to_s.classify)
end
toggle_bookmark(bookmarker, bookmarkee) click to toggle source

Toggles a {Bookmark} relationship between a {Bookmarker} object and a {Bookmarkee} object

@param [Bookmarker] bookmarker - the {Bookmarker} of the relationship @param [Bookmarkee] bookmarkee - the {Bookmarkee} of the relationship @return [Boolean]

# File lib/bookmark_system/bookmark.rb, line 70
def self.toggle_bookmark(bookmarker, bookmarkee)
  validate_bookmarkee(bookmarkee)
  validate_bookmarker(bookmarker)

  if bookmarks?(bookmarker, bookmarkee)
    unbookmark(bookmarker, bookmarkee)
  else
    bookmark(bookmarker, bookmarkee)
  end
end
unbookmark(bookmarker, bookmarkee) click to toggle source

Destroys a {Bookmark} relationship between a {Bookmarker} object and a {Bookmarkee} object

@param [Bookmarker] bookmarker - the {Bookmarker} of the relationship @param [Bookmarkee] bookmarkee - the {Bookmarkee} of the relationship @return [Boolean]

# File lib/bookmark_system/bookmark.rb, line 50
def self.unbookmark(bookmarker, bookmarkee)
  validate_bookmarkee(bookmarkee)
  validate_bookmarker(bookmarker)

  if bookmarks?(bookmarker, bookmarkee)
    bookmark = scope_by_bookmarker(bookmarker).scope_by_bookmarkee(bookmarkee).take
    bookmark.destroy
    true
  else
    false
  end
end

Private Class Methods

validate_bookmarkee(bookmarkee) click to toggle source

Validates a bookmarkee object

@raise [ArgumentError] if the bookmarkee object is invalid

# File lib/bookmark_system/bookmark.rb, line 141
def self.validate_bookmarkee(bookmarkee)
  raise ArgumentError.new unless bookmarkee.respond_to?(:is_bookmarkee?) && bookmarkee.is_bookmarkee?
end
validate_bookmarker(bookmarker) click to toggle source

Validates a bookmarker object

@raise [ArgumentError] if the bookmarker object is invalid

# File lib/bookmark_system/bookmark.rb, line 150
def self.validate_bookmarker(bookmarker)
  raise ArgumentError.new unless bookmarker.respond_to?(:is_bookmarker?) && bookmarker.is_bookmarker?
end