Module: Bookmarker::BookmarkMaker

Defined in:
lib/bookmarker/bookmark_maker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bookmarker/bookmark_maker.rb', line 4

def self.included(base)

  aliases = {
    :add_bookmark => [:new_bookmark],
    :unbookmark => [:remove_bookmark]
  }

  base.class_eval do

    belongs_to :bookmark_maker, :polymorphic => true

    has_many :bookmarks, :class_name => "Bookmarker::Bookmark", :as => :bookmark_maker

    aliases.each do |method, links|
      links.each do |new_method|
        alias_method(new_method, method)
      end
    end
  end
end

Instance Method Details

#add_bookmark(bookmarkable, description = nil) ⇒ Object



25
26
27
28
# File 'lib/bookmarker/bookmark_maker.rb', line 25

def add_bookmark bookmarkable, description = nil
  bookmark = Bookmarker::Bookmark.new( :bookmarkable => bookmarkable, :bookmark_maker => self, :description => description)
  bookmark.save ? true : false
end

#find_bookmarks(bookmarkable_model) ⇒ Object



30
31
32
# File 'lib/bookmarker/bookmark_maker.rb', line 30

def find_bookmarks bookmarkable_model
  bookmarks.where(:bookmarkable_type => bookmarkable_model.base_class.name)
end

#find_by_instance(bookmarkable) ⇒ Object



38
39
40
# File 'lib/bookmarker/bookmark_maker.rb', line 38

def find_by_instance bookmarkable
  bookmarks.where(:bookmarkable_id => bookmarkable.id, :bookmarkable_type => bookmarkable.class.name)
end

#has_in_bookmarks?(bookmarkable_instance) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/bookmarker/bookmark_maker.rb', line 47

def has_in_bookmarks? bookmarkable_instance
  bookmarks = find_by_instance(bookmarkable_instance)
  bookmarks.size > 0
end

#remove_bookmarksObject



34
35
36
# File 'lib/bookmarker/bookmark_maker.rb', line 34

def remove_bookmarks
  bookmarks.delete_all
end

#unbookmark(bookmarkable) ⇒ Object



42
43
44
45
# File 'lib/bookmarker/bookmark_maker.rb', line 42

def unbookmark bookmarkable
  bookmark = find_by_instance(bookmarkable).first        
  bookmark.destroy ? true : false
end