Class: Crm::ArticleRevisionDiffRowComponent

Inherits:
ApplicationComponent show all
Defined in:
app/components/crm/article_revision_diff_row_component.rb

Overview

Renders one side-by-side Article revision comparison row.

Defined Under Namespace

Classes: ContentDiff, DataValues, FragmentMarker, KeyedDiff, SegmentCursor, TokenDiff

Constant Summary collapse

FORMATS =

Supported comparison formats.

Returns:

  • (Array<Symbol>)

    the allowed +:format+ values

%i[collection rich_text source text].freeze
RICH_CONTENT_FIELDS =

Article content fields whose values hold rich-text HTML.

Returns:

  • (Array<Symbol>)

    fields rendered with the +:rich_text+ format

%i[description solution].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationComponent

#cms_link, #fetch_or_fallback, #image_asset_tag, #image_tag, #number_to_currency, #number_with_delimiter, #post_path, #post_url, #strip_tags

Constructor Details

#initialize(label:, current:, proposed:, format: :text) ⇒ ArticleRevisionDiffRowComponent

Returns a new instance of ArticleRevisionDiffRowComponent.

Parameters:

  • label (String)

    the human-readable field label for the row

  • current (Object)

    the current (existing) value for the row

  • proposed (Object)

    the proposed (incoming) value for the row

  • format (Symbol) (defaults to: :text)

    how to compare the values; one of FORMATS

Raises:

  • (ArgumentError)

    when +format+ is not in FORMATS



50
51
52
53
54
55
56
57
58
# File 'app/components/crm/article_revision_diff_row_component.rb', line 50

def initialize(label:, current:, proposed:, format: :text)
  super()
  raise ArgumentError, "Unsupported comparison format: #{format}" unless FORMATS.include?(format.to_sym)

  @label = label
  @current = current
  @proposed = proposed
  @format = format.to_sym
end

Class Method Details

.for_article_data(field:, current:, proposed:) ⇒ Crm::ArticleRevisionDiffRowComponent

Builds a row comparing one Article data (non-content) field as a
collection of labeled entries.

Parameters:

  • field (Symbol, String)

    the Article data field name

  • current (Object)

    the current (existing) field value

  • proposed (Object)

    the proposed (incoming) field value

Returns:



40
41
42
43
# File 'app/components/crm/article_revision_diff_row_component.rb', line 40

def self.for_article_data(field:, current:, proposed:)
  current_values, proposed_values = DataValues.new(field:, current:, proposed:).to_a
  new(label: field.to_s.humanize, current: current_values, proposed: proposed_values, format: :collection)
end

.for_content(field:, current:, proposed:) ⇒ Crm::ArticleRevisionDiffRowComponent

Builds a row comparing one Article content field.

Parameters:

  • field (Symbol, String)

    the Article content field name

  • current (Object)

    the current (existing) field value

  • proposed (Object)

    the proposed (incoming) field value

Returns:



21
22
23
24
25
26
27
28
29
30
31
# File 'app/components/crm/article_revision_diff_row_component.rb', line 21

def self.for_content(field:, current:, proposed:)
  format = if RICH_CONTENT_FIELDS.include?(field.to_sym)
             :rich_text
           elsif field.to_sym == :inline_js
             :source
           else
             :text
           end

  new(label: field.to_s.humanize, current:, proposed:, format:)
end