Module: RailsI18nOnair::ApplicationHelper
- Defined in:
- app/helpers/rails_i18n_onair/application_helper.rb
Instance Method Summary collapse
- #active_nav_class(path) ⇒ Object
- #format_file_size(size) ⇒ Object
- #format_locale_filename(filename) ⇒ Object
-
#locale_file_param(file_info) ⇒ Object
Route identifier for a locale file: the relative filename minus .yml ("en", "devise.en").
- #mode_icon ⇒ Object
- #render_editable_yaml_fields(hash, prefix = "", level = 0) ⇒ Object
- #render_yaml_tree(hash, level = 0) ⇒ Object
- #storage_mode_badge ⇒ Object
- #syntax_highlight_yaml(content) ⇒ Object
- #translation_count_badge(count) ⇒ Object
- #translation_key_count(translation) ⇒ Object
Instance Method Details
#active_nav_class(path) ⇒ Object
126 127 128 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 126 def active_nav_class(path) current_page?(path) ? "active" : "" end |
#format_file_size(size) ⇒ Object
130 131 132 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 130 def format_file_size(size) number_to_human_size(size) end |
#format_locale_filename(filename) ⇒ Object
17 18 19 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 17 def format_locale_filename(filename) File.basename(filename, ".yml").upcase end |
#locale_file_param(file_info) ⇒ Object
Route identifier for a locale file: the relative filename minus .yml ("en", "devise.en"). Only valid for editable (top-level) files.
23 24 25 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 23 def locale_file_param(file_info) file_info[:filename].to_s.sub(/\.yml\z/, "") end |
#mode_icon ⇒ Object
118 119 120 121 122 123 124 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 118 def mode_icon if RailsI18nOnair.configuration.database_mode? content_tag(:i, "", class: "bi bi-database-fill text-success") else content_tag(:i, "", class: "bi bi-folder-fill text-info") end end |
#render_editable_yaml_fields(hash, prefix = "", level = 0) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 51 def render_editable_yaml_fields(hash, prefix = "", level = 0) return "" unless hash.is_a?(Hash) output = "" hash.each do |key, value| indent = " " * (level * 4) # Fix: Always use bracket notation for consistency field_name = prefix.empty? ? "[#{key}]" : "#{prefix}[#{key}]" if value.is_a?(Hash) # Render header row for nested objects with "Add Key" button # Build the dot notation path for this section section_path = field_name.gsub(/\[/, '.').gsub(/\]/, '').sub(/^\./, '') output << content_tag(:tr, class: "section-header") do label_cell = content_tag(:td, raw("#{indent}<strong>#{key}:</strong>"), colspan: 2, class: "bg-light") action_cell = content_tag(:td, class: "bg-light text-end", style: "width: 80px;") do content_tag(:button, type: "button", class: "btn btn-sm btn-outline-success add-section-key", "data-section-path": section_path, title: "Add key to this section") do content_tag(:i, "", class: "bi bi-plus-circle") end end label_cell + action_cell end output << render_editable_yaml_fields(value, field_name, level + 1) else # Render editable row with input field output << content_tag(:tr, class: "editable-row") do key_cell = content_tag(:td, raw("#{indent}#{key}")) value_cell = content_tag(:td) do text_field_tag("translations#{field_name}", value.to_s, class: "form-control form-control-sm", placeholder: "Enter translation value") end action_cell = content_tag(:td, style: "width: 80px;") do content_tag(:button, type: "button", class: "btn btn-sm btn-outline-danger delete-field", "data-field-name": field_name, title: "Delete this key") do content_tag(:i, "", class: "bi bi-trash") end end key_cell + value_cell + action_cell end end end raw(output) end |
#render_yaml_tree(hash, level = 0) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 27 def render_yaml_tree(hash, level = 0) return "" unless hash.is_a?(Hash) output = "" hash.each do |key, value| indent = " " * (level * 4) if value.is_a?(Hash) output << content_tag(:tr) do content_tag(:td, raw("#{indent}<strong>#{key}:</strong>"), colspan: 2, class: "bg-light") end output << render_yaml_tree(value, level + 1) else output << content_tag(:tr) do content_tag(:td, raw("#{indent}#{key}")) + content_tag(:td, value.to_s) end end end raw(output) end |
#storage_mode_badge ⇒ Object
3 4 5 6 7 8 9 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 3 def storage_mode_badge if RailsI18nOnair.configuration.database_mode? content_tag(:span, "Database", class: "badge bg-success") else content_tag(:span, "File", class: "badge bg-info") end end |
#syntax_highlight_yaml(content) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 109 def syntax_highlight_yaml(content) # Basic syntax highlighting for YAML in HTML # This is a simple implementation; for production, consider using a proper syntax highlighter content .gsub(/^(\s*)([a-z_]+):/, '<span style="color: #61afef;">\1\2:</span>') .gsub(/"([^"]*)"/, '<span style="color: #98c379;">"\1"</span>') .gsub(/'([^']*)'/, '<span style="color: #98c379;">\'\1\'</span>') end |
#translation_count_badge(count) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 134 def translation_count_badge(count) badge_class = if count.zero? "bg-secondary" elsif count < 10 "bg-warning" else "bg-success" end content_tag(:span, pluralize(count, "translation"), class: "badge #{badge_class}") end |
#translation_key_count(translation) ⇒ Object
11 12 13 14 15 |
# File 'app/helpers/rails_i18n_onair/application_helper.rb', line 11 def translation_key_count(translation) return 0 unless translation.translation.is_a?(Hash) count_keys(translation.translation) end |