Module: Anchor::DescribableInput

Included in:
CheckBoxComponent, RadioButtonComponent
Defined in:
app/components/anchor/describable_input.rb

Instance Method Summary collapse

Instance Method Details

#aria_descriptionObject



3
4
5
# File 'app/components/anchor/describable_input.rb', line 3

def aria_description
  { describedby: description_id }
end

#descriptionObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/components/anchor/describable_input.rb', line 7

def description
  if object.is_a?(Hash)
    object[:description]
  elsif object.respond_to?(:description)
    object.description
  elsif I18n.exists?(description_translation_html_scope)
    human_attribute_html_description
  elsif I18n.exists?(description_translation_scope)
    human_attribute_description
  else
    false
  end
end

#description_idObject



21
22
23
# File 'app/components/anchor/describable_input.rb', line 21

def description_id
  "#{attribute}-#{value}-description"
end

#description_textObject



25
26
27
# File 'app/components/anchor/describable_input.rb', line 25

def description_text
  human_attribute_description
end

#description_translation_html_scopeObject



29
30
31
32
33
34
35
# File 'app/components/anchor/describable_input.rb', line 29

def description_translation_html_scope
  [
    "activerecord.values",
    i18n_model_key,
    "#{description_id.gsub('-', '.')}_html",
  ]
end

#description_translation_scopeObject



37
38
39
# File 'app/components/anchor/describable_input.rb', line 37

def description_translation_scope
  ["activerecord.values", i18n_model_key, description_id.gsub("-", ".")]
end

#human_attribute_descriptionObject



45
46
47
# File 'app/components/anchor/describable_input.rb', line 45

def human_attribute_description
  I18n.t description_translation_scope.join(".")
end

#human_attribute_html_descriptionObject



41
42
43
# File 'app/components/anchor/describable_input.rb', line 41

def human_attribute_html_description
  I18n.t(description_translation_html_scope.join(".")).html_safe
end

#i18n_model_keyObject



49
50
51
52
53
# File 'app/components/anchor/describable_input.rb', line 49

def i18n_model_key
  if (form_object = form_builder.object)
    form_object.class.model_name.i18n_key
  end
end