Module: ModelModalHelper
- Defined in:
- app/helpers/model_modal_helper.rb
Overview
Helper to create a modal dialog for a given model
Instance Method Summary collapse
- #dynamic_model_modal(model, options = {}) ⇒ Object
-
#dynamic_related_modal(related_model, edit_path, _options = {}) ⇒ Object
Only show the trigger with the edit path, let the rest be generated later.
- #fetch_related_model(model, relation) ⇒ Object
- #model_modal(model, edit_path, options = {}) ⇒ Object
- #model_modal_action(model, _options = {}) ⇒ Object
- #model_modal_content(model, options = {}) ⇒ Object
- #model_modal_field(model, field_name) ⇒ Object
- #model_modal_field_value(model, field_name) ⇒ Object
- #model_modal_fields(model, _options = {}) ⇒ Object
- #model_modal_fields_tab(model, options = {}) ⇒ Object
- #model_modal_footer(model, edit_path) ⇒ Object
- #model_modal_log(log) ⇒ Object
- #model_modal_logs_tab(model, _options = {}) ⇒ Object
- #model_modal_standard(model, _options = {}) ⇒ Object
- #model_modal_standard_tab(model, options = {}) ⇒ Object
- #model_modal_tabs(model, _options = {}) ⇒ Object
-
#model_modal_tag(model, edit_path, options = {}) ⇒ Object
Render a modal tag for the given model object.
-
#related_modal_tag(model, relation, options = {}) ⇒ Object
Render the relationship for the model, using caching to speed things up a bit.
- #related_model_edit_path(model, relation, related_model, options = {}) ⇒ Object
Instance Method Details
#dynamic_model_modal(model, options = {}) ⇒ Object
84 85 86 |
# File 'app/helpers/model_modal_helper.rb', line 84 def dynamic_model_modal(model, = {}) model_modal_content(model, ) + (model, "#{request.url}/edit") end |
#dynamic_related_modal(related_model, edit_path, _options = {}) ⇒ Object
Only show the trigger with the edit path, let the rest be generated later
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/helpers/model_modal_helper.rb', line 23 def (, edit_path, = {}) model_name = .class.to_s.underscore datum = { view_url: edit_path.gsub('/edit', '') } anchor_id = "#{model_name}-#{.id}" title = .respond_to?(:short_name) ? .short_name : .name content_tag(:div) do concat(content_tag(:a, href: "##{anchor_id}", class: 'modal-trigger') do concat(content_tag(:span, title)) end) concat(content_tag(:div, id: anchor_id, class: 'modal', data: datum) do concat(content_tag(:div, class: 'modal-content') do concat(content_tag(:h2, class: 'center') do concat(content_tag(:span, .name)) end) concat(content_tag(:div, class: 'center-align') do concat(content_tag(:div, class: 'progress red lighten-4') do tag(:div, class: 'indeterminate red') end) end) end) concat((, edit_path)) end) end end |
#fetch_related_model(model, relation) ⇒ Object
57 58 59 60 61 62 |
# File 'app/helpers/model_modal_helper.rb', line 57 def (model, relation) # First get the relationship from cache = model.send("#{relation}_id") key = "model_modal::#{relation}::#{}" Rails.cache.fetch(key, expires_in: 6.hours) { model.send(relation) } end |
#model_modal(model, edit_path, options = {}) ⇒ Object
77 78 79 80 81 82 |
# File 'app/helpers/model_modal_helper.rb', line 77 def model_modal(model, edit_path, = {}) content_tag(:div, class: 'modal', id: "modal-#{model.id}") do concat(model_modal_content(model, )) concat((model, edit_path)) end end |
#model_modal_action(model, _options = {}) ⇒ Object
71 72 73 74 75 |
# File 'app/helpers/model_modal_helper.rb', line 71 def model_modal_action(model, = {}) content_tag(:a, class: 'modal-trigger', href: "#modal-#{model.id}") do concat(model.name) end end |
#model_modal_content(model, options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/helpers/model_modal_helper.rb', line 88 def model_modal_content(model, = {}) content_tag(:div, class: 'modal-content') do concat(content_tag(:h2) { model.name }) concat(content_tag(:div, class: 'row') do concat(model_modal_tabs(model, )) concat(model_modal_fields_tab(model, )) concat(model_modal_standard_tab(model, )) concat(model_modal_logs_tab(model, )) if model.respond_to?(:logs) && [:show_audit_logs] end) end end |
#model_modal_field(model, field_name) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'app/helpers/model_modal_helper.rb', line 172 def model_modal_field(model, field_name) content_tag(:tr) do concat(content_tag(:th, class: 'right-align') do field_name end) concat(content_tag(:td) do model_modal_field_value(model, field_name) end) end end |
#model_modal_field_value(model, field_name) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/helpers/model_modal_helper.rb', line 183 def model_modal_field_value(model, field_name) value = model.respond_to?(field_name) ? model.send(field_name) : '' case value when BSON::ObjectId if field_name.eql?('_id') value.to_s else = (model, field_name.chomp('_id')) .present? ? .name : 'N/A' end when FalseClass 'No' when TrueClass 'Yes' when Mongoid::Boolean value ? 'Yes' : 'No' when Date, DateTime, Time current_user.local_time(value, :long) when Integer, Array, Hash value.to_s else value end end |
#model_modal_fields(model, _options = {}) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'app/helpers/model_modal_helper.rb', line 152 def model_modal_fields(model, = {}) content_tag(:table, class: 'center-align') do concat(content_tag(:tbody) do model.class.allowed_param_names.sort.each do |field_name| concat(model_modal_field(model, field_name)) end end) end end |
#model_modal_fields_tab(model, options = {}) ⇒ Object
120 121 122 123 124 |
# File 'app/helpers/model_modal_helper.rb', line 120 def model_modal_fields_tab(model, = {}) content_tag(:div, id: "field-tab-#{model.id}", class: 'col s12') do concat(model_modal_fields(model, )) end end |
#model_modal_footer(model, edit_path) ⇒ Object
208 209 210 211 212 |
# File 'app/helpers/model_modal_helper.rb', line 208 def (model, edit_path) content_tag(:div, class: 'modal-footer') do concat(edit_modal_tag(model, edit_path)) end end |
#model_modal_log(log) ⇒ Object
145 146 147 148 149 150 |
# File 'app/helpers/model_modal_helper.rb', line 145 def model_modal_log(log) content_tag(:tr) do concat(content_tag(:td) { log.created_at }) concat(content_tag(:td) { log. }) end end |
#model_modal_logs_tab(model, _options = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/helpers/model_modal_helper.rb', line 126 def model_modal_logs_tab(model, = {}) content_tag(:div, id: "logs-tab-#{model.id}", class: 'col s12') do content_tag(:table, class: 'center-align') do concat(content_tag(:thead) do concat(content_tag(:tr) do concat(content_tag(:th) { 'Created' }) concat(content_tag(:th) { 'Command' }) concat(content_tag(:th) { 'Message' }) end) end) concat(content_tag(:tbody) do model.logs.each do |log| concat(model_modal_log(log)) end end) end end end |
#model_modal_standard(model, _options = {}) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'app/helpers/model_modal_helper.rb', line 162 def model_modal_standard(model, = {}) content_tag(:table, class: 'center-align highlight') do concat(content_tag(:tbody) do %w[_id _type created_at updated_at search_text sort_text].each do |field_name| concat(model_modal_field(model, field_name)) end end) end end |
#model_modal_standard_tab(model, options = {}) ⇒ Object
114 115 116 117 118 |
# File 'app/helpers/model_modal_helper.rb', line 114 def model_modal_standard_tab(model, = {}) content_tag(:div, id: "standard-tab-#{model.id}", class: 'col s12') do concat(model_modal_standard(model, )) end end |
#model_modal_tabs(model, _options = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/helpers/model_modal_helper.rb', line 100 def model_modal_tabs(model, = {}) content_tag(:div, class: 'col s12') do concat(content_tag(:ul, class: 'tabs tabs-fixed-width') do concat(content_tag(:li, class: 'tab') do content_tag(:a, href: "#field-tab-#{model.id}") { model.class.to_s.titleize } end) concat(content_tag(:li, class: 'tab') { content_tag(:a, href: "#standard-tab-#{model.id}") { 'Details' } }) if model.respond_to?(:logs) concat(content_tag(:li, class: 'tab') { content_tag(:a, href: "#logs-tab-#{model.id}") { 'Logs' } }) end end) end end |
#model_modal_tag(model, edit_path, options = {}) ⇒ Object
Render a modal tag for the given model object
67 68 69 |
# File 'app/helpers/model_modal_helper.rb', line 67 def model_modal_tag(model, edit_path, = {}) model_modal_action(model, ) + model_modal(model, edit_path, ) end |
#related_modal_tag(model, relation, options = {}) ⇒ Object
Render the relationship for the model, using caching to speed things up a bit
This helper should be used on the related objects in the table, to cut down on the database queries when rendering the table.
13 14 15 16 17 18 |
# File 'app/helpers/model_modal_helper.rb', line 13 def (model, relation, = {}) = (model, relation) return if .blank? (, (model, relation, , ), ) end |
#related_model_edit_path(model, relation, related_model, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'app/helpers/model_modal_helper.rb', line 48 def (model, relation, , = {}) if [:parent].present? parent_model = (model, [:parent]) send("edit_#{[:parent]}_#{relation}_path", parent_model, ) else send("edit_#{relation}_path", ) end end |