Class: GovukPublishingComponents::ComponentGuideController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/govuk_publishing_components/component_guide_controller.rb

Constant Summary collapse

MATCH_COMPONENTS =
/(?<=govuk_publishing_components\/components\/)[\/a-zA-Z_-]+(?=['"])/

Instance Method Summary collapse

Instance Method Details

#components_in_use_jsObject



74
75
76
77
78
79
80
81
82
# File 'app/controllers/govuk_publishing_components/component_guide_controller.rb', line 74

def components_in_use_js
  additional_files = "//= require govuk_publishing_components/lib\n"

  components = find_all_partials_in(get_used_component_names)

  components.map { |component|
    "//= require govuk_publishing_components/components/#{component.gsub('_', '-')}" if component_has_js_file(component.gsub("_", "-"))
  }.compact.uniq.sort.join("\n").squeeze("\n").prepend(additional_files)
end

#components_in_use_sassObject



65
66
67
68
69
70
71
72
# File 'app/controllers/govuk_publishing_components/component_guide_controller.rb', line 65

def components_in_use_sass
  additional_files = "@import 'govuk_publishing_components/specific-components';\n"
  components = find_all_partials_in(get_used_component_names)

  components.map { |component|
    "@import 'govuk_publishing_components/components/#{component.gsub('_', '-')}';" if component_has_sass_file(component.gsub("_", "-"))
  }.compact.uniq.sort.join("\n").squeeze("\n").prepend(additional_files)
end

#exampleObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/govuk_publishing_components/component_guide_controller.rb', line 38

def example
  @component_doc = params[:flexible_section] ? flexible_section_docs.get(params[:flexible_section]) : component_docs.get(params[:component])
  @component_example = @component_doc.examples.find { |f| f.id == params[:example] }

  @guide_breadcrumbs = [index_breadcrumb]
  if params[:flexible_section]
    @preview_path = flexible_section_preview_path(@component_doc.id, @component_example.id)
    @guide_breadcrumbs << flexible_section_breadcrumb(@component_doc, @component_example)
  else
    @preview_path = component_preview_path(@component_doc.id, @component_example.id)
    @guide_breadcrumbs << component_breadcrumb(@component_doc, @component_example)
  end
  @guide_breadcrumbs << { title: @component_example.name }
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/govuk_publishing_components/component_guide_controller.rb', line 8

def index
  @application_path = Rails.root
  @component_gem_path = Gem.loaded_specs["govuk_publishing_components"].full_gem_path
  @component_docs = component_docs.all
  @gem_component_docs = gem_component_docs.all
  @used_components = used_components_names.get_component_docs
  @unused_components = unused_components_names.get_component_docs
  @components_in_use_sass = components_in_use_sass
  @components_in_use_js = components_in_use_js
  @flexible_section_docs = flexible_section_docs.all
end

#previewObject



53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/govuk_publishing_components/component_guide_controller.rb', line 53

def preview
  @component_examples = []
  @component_doc = params[:flexible_section] ? flexible_section_docs.get(params[:flexible_section]) : component_docs.get(params[:component])
  @preview = true

  if params[:example].present?
    @component_examples.push(@component_doc.examples.find { |f| f.id == params[:example] })
  else
    @component_examples = @component_doc.examples
  end
end

#showObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/govuk_publishing_components/component_guide_controller.rb', line 20

def show
  if params[:flexible_section]
    @component_doc = flexible_section_docs.get(params[:flexible_section])
    @example_path = flexible_section_example_path(@component_doc.id, "default")
    @preview_path = flexible_section_preview_path(@component_doc.id, "default")
    @preview_all_path = flexible_section_preview_all_path(@component_doc.id)

    @guide_breadcrumbs = [index_breadcrumb, flexible_section_breadcrumb(@component_doc)]
  else
    @component_doc = component_docs.get(params[:component])
    @example_path = component_example_path(@component_doc.id, "default")
    @preview_path = component_preview_path(@component_doc.id, "default")
    @preview_all_path = component_preview_all_path(@component_doc.id)

    @guide_breadcrumbs = [index_breadcrumb, component_breadcrumb(@component_doc)]
  end
end