Class: BookmarkSystem::Bookmark

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/bookmark_system/bookmark.rb

Overview

Bookmark class

This class defines the bookmark model in bookmark system

Class Method Summary collapse

Class Method Details

.bookmark(bookmarker, bookmarkee) ⇒ Boolean

Creates a BookmarkSystem::Bookmark relationship between a BookmarkSystem::Bookmarker object and a BookmarkSystem::Bookmarkee object

Parameters:

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Boolean

Specifies if a BookmarkSystem::Bookmarker object bookmarks a BookmarkSystem::Bookmarkee object

Parameters:

Returns:

  • (Boolean)


88
89
90
91
92
93
# 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) ⇒ ActiveRecord::Relation

Retrieves a scope of BookmarkSystem::Bookmark objects filtered by a BookmarkSystem::Bookmarkee object

Parameters:

Returns:

  • (ActiveRecord::Relation)


101
102
103
# File 'lib/bookmark_system/bookmark.rb', line 101

def self.scope_by_bookmarkee(bookmarkee)
  where(bookmarkee: bookmarkee)
end

.scope_by_bookmarkee_type(klass) ⇒ ActiveRecord::Relation

Retrieves a scope of BookmarkSystem::Bookmark objects filtered by a BookmarkSystem::Bookmarkee type

Parameters:

  • klass (Class)
    • the Class to filter

Returns:

  • (ActiveRecord::Relation)


111
112
113
# 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) ⇒ ActiveRecord::Relation

Retrieves a scope of BookmarkSystem::Bookmark objects filtered by a BookmarkSystem::Bookmarker object

Parameters:

Returns:

  • (ActiveRecord::Relation)


121
122
123
# File 'lib/bookmark_system/bookmark.rb', line 121

def self.scope_by_bookmarker(bookmarker)
  where(bookmarker: bookmarker)
end

.scope_by_bookmarker_type(klass) ⇒ ActiveRecord::Relation

Retrieves a scope of BookmarkSystem::Bookmark objects filtered by a BookmarkSystem::Bookmarker type

Parameters:

  • klass (Class)
    • the Class to filter

Returns:

  • (ActiveRecord::Relation)


131
132
133
# 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) ⇒ Boolean

Toggles a BookmarkSystem::Bookmark relationship between a BookmarkSystem::Bookmarker object and a BookmarkSystem::Bookmarkee object

Parameters:

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Boolean

Destroys a BookmarkSystem::Bookmark relationship between a BookmarkSystem::Bookmarker object and a BookmarkSystem::Bookmarkee object

Parameters:

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
# 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

.validate_bookmarkee(bookmarkee) ⇒ Object (private)

Validates a bookmarkee object

Raises:

  • (ArgumentError)

    if the bookmarkee object is invalid



141
142
143
# 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) ⇒ Object (private)

Validates a bookmarker object

Raises:

  • (ArgumentError)

    if the bookmarker object is invalid



150
151
152
# 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