Class: GovukPublishingComponents::ComponentExample

Inherits:
Object
  • Object
show all
Defined in:
app/models/govuk_publishing_components/component_example.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, example) ⇒ ComponentExample

Returns a new instance of ComponentExample.



12
13
14
15
16
17
18
19
# File 'app/models/govuk_publishing_components/component_example.rb', line 12

def initialize(id, example)
  @id = id
  @data = example["data"] || {}
  @context = example["context"] || {}
  @description = example["description"] || false
  @block = @data.delete(:block) || false
  @embed = example["embed"]
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'app/models/govuk_publishing_components/component_example.rb', line 5

def block
  @block
end

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'app/models/govuk_publishing_components/component_example.rb', line 5

def context
  @context
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'app/models/govuk_publishing_components/component_example.rb', line 5

def data
  @data
end

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'app/models/govuk_publishing_components/component_example.rb', line 5

def description
  @description
end

#embedObject (readonly)

Returns the value of attribute embed.



5
6
7
# File 'app/models/govuk_publishing_components/component_example.rb', line 5

def embed
  @embed
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'app/models/govuk_publishing_components/component_example.rb', line 5

def id
  @id
end

Instance Method Details

#black_background?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/models/govuk_publishing_components/component_example.rb', line 82

def black_background?
  !!context["black_background"]
end

#contains_html?(input) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/govuk_publishing_components/component_example.rb', line 42

def contains_html?(input)
  ActionController::Base.helpers.strip_tags(input) != input
end

#dark_background?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/govuk_publishing_components/component_example.rb', line 78

def dark_background?
  context["dark_background"].present?
end

#data?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/govuk_publishing_components/component_example.rb', line 46

def data?
  data.any?
end

#has_block?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/models/govuk_publishing_components/component_example.rb', line 90

def has_block?
  block.present?
end

#has_embed?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/models/govuk_publishing_components/component_example.rb', line 94

def has_embed?
  embed.present?
end

#highlight_code(string_to_format = "") ⇒ Object



25
26
27
28
29
# File 'app/models/govuk_publishing_components/component_example.rb', line 25

def highlight_code(string_to_format = "")
  formatter = Rouge::Formatters::HTML.new
  lexer = Rouge::Lexers::ERB.new
  formatter.format(lexer.lex(string_to_format)).html_safe
end

#html_descriptionObject



86
87
88
# File 'app/models/govuk_publishing_components/component_example.rb', line 86

def html_description
  markdown_to_html(description) if description.present?
end

#html_safe_dataObject



50
51
52
# File 'app/models/govuk_publishing_components/component_example.rb', line 50

def html_safe_data
  html_safe_strings(data.dup)
end

#html_safe_strings(obj) ⇒ Object

Iterate through data object and recursively mark any found string as html_safe

Safe HTML can be passed to components, simulate by marking any string that comes from YAML as safe



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/govuk_publishing_components/component_example.rb', line 59

def html_safe_strings(obj)
  case obj
  when String
    obj.html_safe
  when Hash
    obj.each do |key, value|
      obj[key] = html_safe_strings(value)
    end
  when Array
    obj.map! { |e| html_safe_strings(e) }
  else
    obj
  end
end

#nameObject



21
22
23
# File 'app/models/govuk_publishing_components/component_example.rb', line 21

def name
  id.humanize
end

#pretty_dataObject



31
32
33
34
35
36
37
38
39
40
# File 'app/models/govuk_publishing_components/component_example.rb', line 31

def pretty_data
  json_key_regex = /"(\w*)":/ # matches quoted keys ending with a colon, i.e. "key":
  output = JSON.pretty_generate(data).gsub('\\n', "\n    ").gsub(json_key_regex, '\1:')

  quoted_string_regex = /"((?:[^"\\]|\\.)*)"/ # matches "some text" - ignores escaped quotes, i.e. \"
  output.gsub(quoted_string_regex) do |group|
    match = Regexp.last_match[1]
    contains_html?(match) ? "sanitize(#{group})" : group
  end
end

#right_to_left?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/govuk_publishing_components/component_example.rb', line 74

def right_to_left?
  context["right_to_left"].present?
end