Module: GovukPublishingComponents::AppHelpers::AssetHelper

Defined in:
lib/govuk_publishing_components/app_helpers/asset_helper.rb

Constant Summary collapse

COMPONENT_CSS_PATHS =

This is a list of all the components that have stylesheets - so a new component needs to be added to this list rather than being laboriously added and removed from every single rendering application.

Dir.glob("#{GovukPublishingComponents::Config.gem_directory}/app/assets/stylesheets/govuk_publishing_components/components/_*.scss").map { |path|
  filename = path.split("/").last
  component_name = filename.sub("_", "").sub(".scss", "")
  "govuk_publishing_components/components/_#{component_name}.css"
}.freeze
STATIC_STYLESHEET_LIST =

This is used to dedupe stylesheets.

%w[
  govuk_publishing_components/components/_breadcrumbs.css
  govuk_publishing_components/components/_button.css
  govuk_publishing_components/components/_error-message.css
  govuk_publishing_components/components/_heading.css
  govuk_publishing_components/components/_hint.css
  govuk_publishing_components/components/_input.css
  govuk_publishing_components/components/_label.css
  govuk_publishing_components/components/_search.css
  govuk_publishing_components/components/_search-with-autocomplete.css
  govuk_publishing_components/components/_skip-link.css
  govuk_publishing_components/components/_textarea.css
  govuk_publishing_components/components/_title.css

  govuk_publishing_components/components/_cookie-banner.css
  govuk_publishing_components/components/_cross-service-header.css
  govuk_publishing_components/components/_feedback.css
  govuk_publishing_components/components/_layout-footer.css
  govuk_publishing_components/components/_layout-for-public.css
  govuk_publishing_components/components/_layout-header.css
  govuk_publishing_components/components/_layout-super-navigation-header.css
].freeze

Instance Method Summary collapse

Instance Method Details

#add_app_component_stylesheet(app_component) ⇒ Object

Used to add a component that exists in the application to the list



52
53
54
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 52

def add_app_component_stylesheet(app_component)
  add_stylesheet_path("components/_#{app_component}.css")
end

#add_gem_component_stylesheet(gem_component) ⇒ Object

Used to add a component that exists in the gem to the list



47
48
49
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 47

def add_gem_component_stylesheet(gem_component)
  add_stylesheet_path("govuk_publishing_components/components/_#{gem_component}.css")
end

#add_stylesheet_path(component_path) ⇒ Object



40
41
42
43
44
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 40

def add_stylesheet_path(component_path)
  unless is_already_used?(component_path)
    all_component_stylesheets_being_used << component_path
  end
end

#add_view_stylesheet(view_component) ⇒ Object

Some applications have view-specific stylesheets - use this method to add them to the list



58
59
60
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 58

def add_view_stylesheet(view_component)
  add_stylesheet_path("views/_#{view_component}.css")
end

#all_component_stylesheets_being_usedObject



62
63
64
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 62

def all_component_stylesheets_being_used
  @all_component_stylesheets_being_used ||= []
end

#application_stylesheet_in_use?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 77

def application_stylesheet_in_use?
  GovukPublishingComponents::Config.application_stylesheet.presence
end

#get_component_css_pathsObject



73
74
75
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 73

def get_component_css_paths
  COMPONENT_CSS_PATHS
end

#render_component_stylesheetsObject



66
67
68
69
70
71
# File 'lib/govuk_publishing_components/app_helpers/asset_helper.rb', line 66

def render_component_stylesheets
  list_of_stylesheets = all_component_stylesheets_being_used.map do |component|
    stylesheet_link_tag(component, integrity: false)
  end
  raw(list_of_stylesheets.join(""))
end