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

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, options = {})
  model_modal_content(model, options) + model_modal_footer(model, "#{request.url}/edit")
end

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 dynamic_related_modal(related_model, edit_path, _options = {})
  model_name = related_model.class.to_s.underscore
  datum = { view_url: edit_path.gsub('/edit', '') }
  anchor_id = "#{model_name}-#{related_model.id}"
  title = related_model.respond_to?(:short_name) ? related_model.short_name : related_model.name
  (:div) do
    concat((:a, href: "##{anchor_id}", class: 'modal-trigger') do
      concat((:span, title))
    end)
    concat((:div, id: anchor_id, class: 'modal', data: datum) do
      concat((:div, class: 'modal-content') do
        concat((:h2, class: 'center') do
          concat((:span, related_model.name))
        end)
        concat((:div, class: 'center-align') do
          concat((:div, class: 'progress red lighten-4') do
            tag(:div, class: 'indeterminate red')
          end)
        end)
      end)
      concat(model_modal_footer(related_model, edit_path))
    end)
  end
end


57
58
59
60
61
62
# File 'app/helpers/model_modal_helper.rb', line 57

def fetch_related_model(model, relation)
  # First get the relationship from cache
  related_id = model.send("#{relation}_id")
  key = "model_modal::#{relation}::#{related_id}"
  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, options = {})
  (:div, class: 'modal', id: "modal-#{model.id}") do
    concat(model_modal_content(model, options))
    concat(model_modal_footer(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, _options = {})
  (: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, options = {})
  (:div, class: 'modal-content') do
    concat((:h2) { model.name })
    concat((:div, class: 'row') do
      concat(model_modal_tabs(model, options))
      concat(model_modal_fields_tab(model, options))
      concat(model_modal_standard_tab(model, options))
      concat(model_modal_logs_tab(model, options)) if model.respond_to?(:logs) && options[: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)
  (:tr) do
    concat((:th, class: 'right-align') do
      field_name
    end)
    concat((: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
      related_model = fetch_related_model(model, field_name.chomp('_id'))
      related_model.present? ? related_model.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, _options = {})
  (:table, class: 'center-align') do
    concat((: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, options = {})
  (:div, id: "field-tab-#{model.id}", class: 'col s12') do
    concat(model_modal_fields(model, options))
  end
end


208
209
210
211
212
# File 'app/helpers/model_modal_helper.rb', line 208

def model_modal_footer(model, edit_path)
  (: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)
  (:tr) do
    concat((:td) { log.created_at })
    concat((:td) { log.display_message })
  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, _options = {})
  (:div, id: "logs-tab-#{model.id}", class: 'col s12') do
    (:table, class: 'center-align') do
      concat((:thead) do
        concat((:tr) do
          concat((:th) { 'Created' })
          concat((:th) { 'Command' })
          concat((:th) { 'Message' })
        end)
      end)
      concat((: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, _options = {})
  (:table, class: 'center-align highlight') do
    concat((: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, options = {})
  (:div, id: "standard-tab-#{model.id}", class: 'col s12') do
    concat(model_modal_standard(model, options))
  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, _options = {})
  (:div, class: 'col s12') do
    concat((:ul, class: 'tabs tabs-fixed-width') do
      concat((:li, class: 'tab') do
        (:a, href: "#field-tab-#{model.id}") { model.class.to_s.titleize }
      end)
      concat((:li, class: 'tab') { (:a, href: "#standard-tab-#{model.id}") { 'Details' } })
      if model.respond_to?(:logs)
        concat((:li, class: 'tab') { (: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, options = {})
  model_modal_action(model, options) + model_modal(model, edit_path, options)
end

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 related_modal_tag(model, relation, options = {})
  related_model = fetch_related_model(model, relation)
  return if related_model.blank?

  dynamic_related_modal(related_model, related_model_edit_path(model, relation, related_model, options), options)
end


48
49
50
51
52
53
54
55
# File 'app/helpers/model_modal_helper.rb', line 48

def related_model_edit_path(model, relation, related_model, options = {})
  if options[:parent].present?
    parent_model = fetch_related_model(model, options[:parent])
    send("edit_#{options[:parent]}_#{relation}_path", parent_model, related_model)
  else
    send("edit_#{relation}_path", related_model)
  end
end