Class: TagFollowingService

Inherits:
Object
  • Object
show all
Defined in:
app/services/tag_following_service.rb

Defined Under Namespace

Classes: DuplicateTag

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ TagFollowingService

Returns a new instance of TagFollowingService.


4
5
6
# File 'app/services/tag_following_service.rb', line 4

def initialize(user=nil)
  @user = user
end

Instance Method Details

#create(name) ⇒ Object

Raises:

  • (ArgumentError)

8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/services/tag_following_service.rb', line 8

def create(name)
  name_normalized = ActsAsTaggableOn::Tag.normalize(name)
  raise ArgumentError, "Name field null or empty" if name_normalized.blank?

  tag = ActsAsTaggableOn::Tag.find_or_create_by(name: name_normalized)
  raise DuplicateTag if @user.tag_followings.exists?(tag_id: tag.id)

  tag_following = @user.tag_followings.new(tag_id: tag.id)
  raise "Can't process tag entity" unless tag_following.save

  tag
end

#destroy(id) ⇒ Object


26
27
28
29
# File 'app/services/tag_following_service.rb', line 26

def destroy(id)
  tag_following = @user.tag_followings.find_by!(tag_id: id)
  tag_following.destroy
end

#destroy_by_name(name) ⇒ Object


31
32
33
34
35
# File 'app/services/tag_following_service.rb', line 31

def destroy_by_name(name)
  name_normalized = ActsAsTaggableOn::Tag.normalize(name)
  followed_tag = @user.followed_tags.find_by!(name: name_normalized)
  destroy(followed_tag.id)
end

#find(name) ⇒ Object


21
22
23
24
# File 'app/services/tag_following_service.rb', line 21

def find(name)
  name_normalized = ActsAsTaggableOn::Tag.normalize(name)
  ActsAsTaggableOn::Tag.find_or_create_by(name: name_normalized)
end

#indexObject


37
38
39
# File 'app/services/tag_following_service.rb', line 37

def index
  @user.followed_tags
end