Module: Findable::ClassMethods

Included in:
Author, Book, Episode, Genre
Defined in:
lib/podcast_book_club/concerns/findable.rb

Instance Method Summary collapse

Instance Method Details

#find_by_name(name) ⇒ Object



3
4
5
# File 'lib/podcast_book_club/concerns/findable.rb', line 3

def find_by_name(name)
    self.all.detect {|i| i.name == name}
end

#find_by_title(title) ⇒ Object



7
8
9
# File 'lib/podcast_book_club/concerns/findable.rb', line 7

def find_by_title(title)
    self.all.detect {|i| i.title.downcase.include?(title.downcase)}
end

#find_or_create_by_name(name) ⇒ Object



11
12
13
14
# File 'lib/podcast_book_club/concerns/findable.rb', line 11

def find_or_create_by_name(name)
    instance = self.find_by_name(name) || self.create({name: name})
    instance
end

#find_or_create_by_title(attributes) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/podcast_book_club/concerns/findable.rb', line 16

def find_or_create_by_title(attributes)
    title = attributes[:title]

    if self.find_by_title(title)
        instance = self.find_by_title
        instance.episode = attributes[:episode]
    else
        self.create(attributes)
    end
    
    instance
end