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[
  breadcrumbs
  button
  error-message
  heading
  hint
  input
  label
  search
  search-with-autocomplete
  skip-link
  textarea
  title
  cookie-banner
  cross-service-header
  feedback
  layout-footer
  layout-for-public
  layout-header
  layout-super-navigation-header
].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