Skip to content

Commit

Permalink
[WEBDEV-584] Update components serialisation for the search results
Browse files Browse the repository at this point in the history
- Update Hint, Heading and Search Form component serialisers
- Update Search Result and Pagination helpers
- Disable CyclomaticComplexity cop in Rubocop
  • Loading branch information
cazwazacz committed Jul 5, 2018
1 parent d6788a6 commit c258b8a
Show file tree
Hide file tree
Showing 42 changed files with 657 additions and 468 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Metrics/LineLength:
Metrics/AbcSize:
Max: 30

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/ParameterLists:
Max: 10

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/pagination_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def create_number_cards
page_range.map do |page|
data = {}.tap do |hash|
hash[:url] = number_card_url(page)
hash[:data_number] = page
hash[:number] = page
hash[:total_count] = "of #{total_pages}"
hash[:active] = true if page == current_page
end
Expand Down
43 changes: 15 additions & 28 deletions app/controllers/concerns/search_result_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,39 @@ class << self
def create_search_results(results)
results.entries.map do |entry|
ComponentSerializer::CardComponentSerializer.new(
'card__search-result',
search_result_data(heading_text: entry.title, url: entry.url, list_hint: list_hint_data(entry), paragraph_text: entry.content)
'card__search__search-result',
search_result_data(entry: entry, heading_text: entry.title, url: entry.url, hint: hint_data(entry), short_url: entry.formatted_url, paragraph_content: entry.content)
).to_h
end
end

private

def search_result_data(heading_text: nil, url: nil, list_hint: nil, paragraph_text: nil)
def search_result_data(entry: nil, heading_text: nil, url: nil, hint: nil, short_url: nil, paragraph_content: nil)
{}.tap do |hash|
hash[:heading_text] = heading_text if heading_text
hash[:url] = url if url
hash[:list_hint] = list_hint if list_hint
hash[:paragraph_text] = paragraph_text if paragraph_text
hash[:hints] = hint unless no_hint?(entry) || !hint
hash[:short_url] = short_url if short_url
hash[:paragraph_content] = paragraph_content if paragraph_content
end
end

def list_hint_data(entry)
{}.tap do |hash|
handle_hint_type_and_text(hash, entry)
hash[:url] = entry.formatted_url
end
end

def list_hint_type(entry)
types = ['hint']
types << 'theme--grey-4' unless entry.hint_types.include?('Beta')

types.join(' ')
def hint_data(entry)
{ name: 'hint', data: hint_display_data(entry) }
end

def list_hint_text(entry)
entry.hint_types.first
def hint_display_data(entry)
{}.tap do |hash|
hash[:component] = 'hint'
hash[:variant] = 'theme'
hash[:modifier] = 'grey-4' unless entry.hint_types.include?('Beta')
hash[:content] = entry.hint_types.first
end
end

def no_hint?(entry)
entry&.hint_types&.empty?
end

def handle_hint_type_and_text(hash, entry)
return hash if no_hint?(entry)

hash.tap do |h|
h[:type] = list_hint_type(entry)
h[:text] = list_hint_text(entry)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module ComponentSerializer
class HeadingComponentSerializer < ComponentSerializer::BaseComponentSerializer
# Initialise a heading component.
#
# @param [Array<String>] heading strings of text to be wrapped in <h> tags.
# @param [Array<String>] content strings of text to be used within heading elements.
# @param [String] translation_key a translation block.
# @param [Hash] translation_data a hash of data that is to be used in the translation block.
# @param [Integer] size number from 1 - 4 to be placed in the <h> tag.
def initialize(heading: nil, translation_key: nil, translation_data: nil, size: nil)
@heading = heading
def initialize(content: nil, translation_key: nil, translation_data: nil, size: nil)
@content = content
@translation_key = translation_key
@translation_data = translation_data
@size = size
Expand All @@ -21,7 +21,7 @@ def name

def data
{}.tap do |hash|
hash[:heading] = @heading if @heading
hash[:content] = @content if @content
hash[:translation] = translation_hash if @translation_key && @translation_data
hash[:size] = @size
end
Expand Down
20 changes: 8 additions & 12 deletions app/serializers/component_serializer/hint_component_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@ class HintComponentSerializer < ComponentSerializer::BaseComponentSerializer
# Initialise a hint component. In the front-end, this is a span element with the class of hint.
#
# @param [String] content a translation block that is evaluated in the front-end
def initialize(content)
# @param [Array<Hash>] display_data all the css classes for the component
def initialize(content: nil, display_data: [])
@content = content
@display_data = display_data
end

private

def name
'span'
'hint'
end

def data
{
display: {
data: [
{
component: 'hint'
}
]
},
content: @content
}
{}.tap do |hash|
hash[:display] = display_hash(@display_data) if @display_data
hash[:content] = @content if @content
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def name
'link'
end

# rubocop:disable Metrics/CyclomaticComplexity
def data
{}.tap do |hash|
hash[:link] = @link if @link
Expand All @@ -32,6 +31,5 @@ def data
hash[:components] = @components if @components
end
end
# rubocop:enable Metrics/CyclomaticComplexity
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def name
def data
{}.tap do |hash|
hash[:value] = @query if @query
hash[:key_word] = 'search.key-word'
hash[:label] = 'search.key-word'
hash[:components] = @components
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def content_with_query

def section_primary_components(results_heading)
[
ComponentSerializer::HeadingComponentSerializer.new(heading: [results_heading], size: 1).to_h,
ComponentSerializer::HeadingComponentSerializer.new(content: [results_heading], size: 1).to_h,
ComponentSerializer::SearchFormComponentSerializer.new(@query, [ComponentSerializer::SearchIconComponentSerializer.new.to_h]).to_h
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def footer_components_data
end

def footer_components_display_data
display_data(component: 'list')
[display_data(component: 'list')]
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def status_banner_display_data

def status_banner_components
[
ComponentSerializer::HintComponentSerializer.new('status-banner.beta').to_h,
ComponentSerializer::HintComponentSerializer.new(content: 'status-banner.beta').to_h,
ComponentSerializer::ListComponentSerializer.new(display: 'generic', contents: ['status-banner.pages-tested', 'status-banner.current-website'], display_data: [display_data(component: 'list', variant: 'inline')], type: ComponentSerializer::ListComponentSerializer::Type::UNORDERED).to_h
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
- name: navigation__number__card
data:
url: some_url?count=123&q=hello&start_index=1
data-number: 1
number: 1
total-count: of 3
active: true
- name: navigation__number__card
data:
url: some_url?count=123&q=hello&start_index=124
data-number: 2
number: 2
total-count: of 3
- name: navigation__number__card
data:
url: some_url?count=123&q=hello&start_index=247
data-number: 3
number: 3
total-count: of 3
38 changes: 22 additions & 16 deletions spec/fixtures/controllers/concerns/search_result_helper/fixture.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
---
- name: card__search-result
- name: card__search__search-result
data:
heading-text: Result one
url: link
list-hint:
type: hint theme--grey-4
text: pdf
url: link...
paragraph-text: Content one
- name: card__search-result
hints:
name: hint
data:
component: hint
variant: theme
modifier: grey-4
content: pdf
short-url: link...
paragraph-content: Content one
- name: card__search__search-result
data:
heading-text: Result two
url: link
list-hint:
url: link...
paragraph-text: Content two
- name: card__search-result
short-url: link...
paragraph-content: Content two
- name: card__search__search-result
data:
heading-text: Result three
url: link
list-hint:
type: hint
text: Beta
url: link...
paragraph-text: Content three
hints:
name: hint
data:
component: hint
variant: theme
content: Beta
short-url: link...
paragraph-content: Content three
Loading

0 comments on commit c258b8a

Please sign in to comment.