Class: EasyTags::TaggableContext

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_tags/taggable_context.rb

Overview

Handles tag context manipulation

Instance Method Summary collapse

Constructor Details

#initialize(context:, tags_association:, on_change:) ⇒ TaggableContext

Returns a new instance of TaggableContext.

Parameters:

  • context (String, Symbol)
  • tags_association (ActiveRecord::Relation)
  • on_change (Proc)


9
10
11
12
13
# File 'lib/easy_tags/taggable_context.rb', line 9

def initialize(context:, tags_association:, on_change:)
  self.context = context
  self.tags_association = tags_association
  self.on_change = on_change
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



85
86
87
88
89
# File 'lib/easy_tags/taggable_context.rb', line 85

def method_missing(name, *args, &block)
  return super unless respond_to_missing?(name)

  tags.send(name, *args, &block)
end

Instance Method Details

#add(*names) ⇒ Object

Add tags to the tag_list. Duplicate or blank tags will be ignored.

Example:

tag_list.add('Fun', 'Happy')


59
60
61
62
63
# File 'lib/easy_tags/taggable_context.rb', line 59

def add(*names)
  tags.add(*names).tap do
    notify_change
  end
end

#changed?true, false

Returns:

  • (true, false)


16
17
18
# File 'lib/easy_tags/taggable_context.rb', line 16

def changed?
  tags.sort != persisted_tags.sort
end

#new_tagsTagList

Returns:



39
40
41
# File 'lib/easy_tags/taggable_context.rb', line 39

def new_tags
  tags - persisted_tags
end

#persisted_tagsTagList

Returns:



26
27
28
# File 'lib/easy_tags/taggable_context.rb', line 26

def persisted_tags
  @persisted_tags ||= preloaded_persisted_tags
end

#refreshObject

clear memoized info and force a refresh



49
50
51
52
53
# File 'lib/easy_tags/taggable_context.rb', line 49

def refresh
  @tags = nil
  @persisted_tags = nil
  tags_association.reset
end

#remove(value) ⇒ Object

Remove item from list

Example:

tag_list.remove('Issues')


69
70
71
72
73
# File 'lib/easy_tags/taggable_context.rb', line 69

def remove(value)
  tags.remove(value).tap do
    notify_change
  end
end

#removed_tagsTagList

Returns:



44
45
46
# File 'lib/easy_tags/taggable_context.rb', line 44

def removed_tags
  persisted_tags - tags
end

#tagsTagList

Returns:



21
22
23
# File 'lib/easy_tags/taggable_context.rb', line 21

def tags
  @tags ||= TagList.new(persisted_tags)
end

#update(value) ⇒ TagList

Parameters:

  • value (String, Symbol)

Returns:



32
33
34
35
36
# File 'lib/easy_tags/taggable_context.rb', line 32

def update(value)
  @tags = TagList.new(value)

  notify_change
end