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/_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



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

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



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

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

#add_stylesheet_path(component_path) ⇒ Object



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

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



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

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

#all_component_stylesheets_being_usedObject



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

def all_component_stylesheets_being_used
  @all_component_stylesheets_being_used ||= []
end

#application_stylesheet_in_use?Boolean

Returns:

  • (Boolean)


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

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

#get_component_css_pathsObject



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

def get_component_css_paths
  COMPONENT_CSS_PATHS
end

#render_component_stylesheetsObject



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

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