Class: BookmarkSystem::Bookmark
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- BookmarkSystem::Bookmark
- Defined in:
- lib/bookmark_system/bookmark.rb
Overview
Bookmark class
This class defines the bookmark model in bookmark system
Class Method Summary collapse
-
.bookmark(bookmarker, bookmarkee) ⇒ Boolean
Creates a Bookmark relationship between a Bookmarker object and a Bookmarkee object.
-
.bookmarks?(bookmarker, bookmarkee) ⇒ Boolean
Specifies if a Bookmarker object bookmarks a Bookmarkee object.
-
.scope_by_bookmarkee(bookmarkee) ⇒ ActiveRecord::Relation
Retrieves a scope of Bookmark objects filtered by a Bookmarkee object.
-
.scope_by_bookmarkee_type(klass) ⇒ ActiveRecord::Relation
Retrieves a scope of Bookmark objects filtered by a Bookmarkee type.
-
.scope_by_bookmarker(bookmarker) ⇒ ActiveRecord::Relation
Retrieves a scope of Bookmark objects filtered by a Bookmarker object.
-
.scope_by_bookmarker_type(klass) ⇒ ActiveRecord::Relation
Retrieves a scope of Bookmark objects filtered by a Bookmarker type.
-
.toggle_bookmark(bookmarker, bookmarkee) ⇒ Boolean
Toggles a Bookmark relationship between a Bookmarker object and a Bookmarkee object.
-
.unbookmark(bookmarker, bookmarkee) ⇒ Boolean
Destroys a Bookmark relationship between a Bookmarker object and a Bookmarkee object.
-
.validate_bookmarkee(bookmarkee) ⇒ Object
private
Validates a bookmarkee object.
-
.validate_bookmarker(bookmarker) ⇒ Object
private
Validates a bookmarker object.
Class Method Details
.bookmark(bookmarker, bookmarkee) ⇒ Boolean
Creates a BookmarkSystem::Bookmark relationship between a BookmarkSystem::Bookmarker object and a BookmarkSystem::Bookmarkee object
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
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
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
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
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
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
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
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
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
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 |