Class: Trackers::TagService

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
app/services/partner_service/trackers/tag_service.rb

Overview

The TagService class tracks changes to restaurant tags, including sub-tags and primary tags. It captures changes such as additions, removals, and modifications for better traceability.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTagService

Initializes the TagService with default values.



9
10
11
# File 'app/services/partner_service/trackers/tag_service.rb', line 9

def initialize
  @changes = {}
end

Instance Attribute Details

#changesObject

Returns the value of attribute changes.



6
7
8
# File 'app/services/partner_service/trackers/tag_service.rb', line 6

def changes
  @changes
end

Returns the value of attribute footer_text.



6
7
8
# File 'app/services/partner_service/trackers/tag_service.rb', line 6

def footer_text
  @footer_text
end

Returns the value of attribute navigation.



6
7
8
# File 'app/services/partner_service/trackers/tag_service.rb', line 6

def navigation
  @navigation
end

Instance Method Details

#track_primary_tag(restaurant, new_tag_id, category_code) ⇒ void

This method returns an undefined value.

Tracks changes in the primary tag of a restaurant.

Parameters:

  • restaurant (Restaurant)

    The restaurant for which the primary tag is being updated.

  • new_tag_id (Integer)

    The ID of the new primary tag.

  • category_code (String)

    The category code of the tag.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/services/partner_service/trackers/tag_service.rb', line 59

def track_primary_tag(restaurant, new_tag_id, category_code)
  old_primary_tag = restaurant.primary_tag_by_category(category_code)&.restaurant_tag
  new_primary_tag = RestaurantTag.find(new_tag_id)
  return if old_primary_tag.nil? && new_primary_tag.nil?
  return if old_primary_tag&.id == new_primary_tag&.id

  if old_primary_tag.nil? && new_primary_tag.present?
    category, title = extract_category_and_title(new_primary_tag.title)
    track_changes(label: "(#{category.underscore.humanize}) Display Tag has been added",
                  nav: 'restaurant', type: 'text', old_value: nil, new_value: title.humanize)
  elsif new_primary_tag.nil? && old_primary_tag.present?
    category, title = extract_category_and_title(old_primary_tag.title)
    track_changes(label: "(#{category.underscore.humanize}) Display Tag has been removed",
                  nav: 'restaurant', type: 'text', old_value: title.humanize, new_value: nil)
  elsif old_primary_tag&.id != new_primary_tag&.id
    category, title = extract_category_and_title(old_primary_tag.title)
    new_category, new_title = extract_category_and_title(new_primary_tag.title)
    track_changes(label: "(#{category.underscore.humanize}) Display Tag has been changed",
                  nav: 'restaurant', type: 'text', old_value: title.humanize, new_value: new_title.humanize)
  end
end

#track_tag(old_ids, new_ids) ⇒ void

This method returns an undefined value.

Tracks changes in sub-tags of a restaurant, including added, removed, or modified tags.

Parameters:

  • old_ids (Array<Integer>)

    The old tag IDs before the update.

  • new_ids (Array<Integer>)

    The new tag IDs after the update.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/partner_service/trackers/tag_service.rb', line 18

def track_tag(old_ids, new_ids)
  return if (old_ids - new_ids).blank? && (new_ids - old_ids).blank?

  removed_ids = old_ids - new_ids
  added_ids = new_ids - old_ids

  old_tags = RestaurantTag.where(id: removed_ids)
  new_tags = RestaurantTag.where(id: added_ids)

  max_length = [new_tags.size, old_tags.size].max
  (0...max_length).each do |i|
    new_tag = new_tags[i]
    old_tag = old_tags[i]
    next if new_tag&.id == old_tag&.id

    if new_tag && old_tag
      old_category, old_title = extract_category_and_title(old_tag.title)
      new_category, new_title = extract_category_and_title(new_tag.title)

      if old_title != new_title || old_category != new_category
        label = "(#{old_category.underscore.humanize}) Sub Tag has been changed"
        track_changes(label: label, nav: 'restaurant', type: 'text', old_value: old_title, new_value: new_title)
      end
    elsif new_tag.present? && old_tag.blank?
      category, title = extract_category_and_title(new_tag.title)
      label = "(#{category.underscore.humanize}) Sub Tag has been added"
      track_changes(label: label, nav: 'restaurant', type: 'text', old_value: nil, new_value: title)
    elsif old_tag.present? && new_tag.blank?
      category, title = extract_category_and_title(old_tag.title)
      label = "(#{category.underscore.humanize}) Sub Tag has been removed"
      track_changes(label: label, nav: 'restaurant', type: 'text', old_value: title, new_value: nil)
    end
  end
end