Class: Faq::Update

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
app/services/faq/update.rb

Overview

Applies a patch to an existing FAQ and refreshes the content surfaces that
can cache or embed it.

Defined Under Namespace

Classes: Result

Constant Summary collapse

FAQ_CHANGE_KEYS =

FAQ attributes that callers may patch.

%i[subject solution description sales support].freeze
DISPLAY_FIELD_NAMES =

Public field labels returned for changed FAQ attributes.

{
  subject: 'question',
  solution: 'answer',
  description: 'description',
  sales: 'show_on_sales_pages',
  support: 'show_on_support_pages'
}.freeze

Instance Method Summary collapse

Methods included from Service

#attributes_used, #build_result, call, #logger

Methods included from Service::Initializer

#initialize

Instance Method Details

#callResult

Validate and apply the requested FAQ patch and synchronization work.

Returns:

  • (Result)

    structured update outcome



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/services/faq/update.rb', line 74

def call
  return failure('FAQ must be an existing ArticleFaq') unless faq.is_a?(ArticleFaq) && faq.persisted?
  return failure('No FAQ updates provided') unless update_requested?

  prepare_inputs
  return failure(@validation_error) if @validation_error

  before = snapshot
  persist_update!
  after = snapshot

  changed_fields = changed_fields(before, after)
  added_tags = after[:tags] - before[:tags]
  removed_tags = before[:tags] - after[:tags]
  refreshed_posts = run_post_save_syncs(changed_fields)
  cache_purged = purge_cache

  Result.new(
    success: true,
    faq: faq,
    changed_fields: changed_fields,
    added_tags: added_tags,
    removed_tags: removed_tags,
    added_page_paths: changed_page_paths(@normalized_add_page_paths, added_tags),
    removed_page_paths: changed_page_paths(@normalized_remove_page_paths, removed_tags),
    refreshed_posts: refreshed_posts,
    cache_purged: cache_purged,
    warnings: @warnings
  )
rescue ActiveRecord::RecordInvalid => e
  failure("Failed to update FAQ: #{e.record.errors.full_messages.join(', ')}")
rescue StandardError => e
  ErrorReporting.error(e, { tool: 'update_faq', faq_id: faq&.id })
  failure("Unexpected error updating FAQ: #{e.message}")
end