Module: Carload::DashboardHelper

Defined in:
app/helpers/carload/dashboard_helper.rb

Instance Method Summary collapse

Instance Method Details

#generate_input(form, model_name, attribute_name, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/carload/dashboard_helper.rb', line 3

def generate_input form, model_name, attribute_name, options = {}
  if options[:polymorphic]
    form.input attribute_name,
      collection: @model_class.send(attribute_name.to_s.pluralize),
      selected: options[:value],
      input_html: { class: 'use-select2' }
  elsif attribute_name =~ /_id$/
    class_name, association_name = associated_model_name model_name, attribute_name
    association = Dashboard.model(model_name).associations[association_name]
    label_attribute = association[:choose_by]
    form.association association_name,
      label_method: label_attribute,
      label: t("activerecord.models.#{class_name}"),
      input_html: {
        class: 'use-select2',
        data: {
          placeholder: t('carload.placeholder.select', thing: t("activerecord.attributes.#{class_name}.#{label_attribute}"))
        }
      }
  elsif attribute_name =~ /_ids$/
    class_name, association_name = associated_model_name model_name, attribute_name
    association = Dashboard.model(model_name).associations[association_name]
    label_attribute = association[:choose_by]
    form.input attribute_name,
      label: t("activerecord.attributes.#{class_name}.#{label_attribute}") + " (#{t("activerecord.models.#{class_name}")})",
      collection: class_name.to_s.camelize.constantize.all,
      label_method: label_attribute,
      value_method: :id,
      input_html: {
        class: 'use-select2',
        multiple: true,
        data: {
          placeholder: t('carload.placeholder.select', thing: t("activerecord.attributes.#{class_name}.#{label_attribute}"))
        }
      }
  elsif needs_upload?(model_name, attribute_name) and image?(attribute_name)
    upload_image form: form, image_name: attribute_name, width: 150, height: 150
  elsif options[:type] == :text
    form.input(attribute_name, label: raw("      <span class=\"control-label string optional\">\#{t(\"activerecord.attributes.\#{@model_name}.\#{attribute_name}\")}</span>\n      <a id='preview-\#{attribute_name}-button' class='btn btn-xs' data-toggle='on'>\#{t('carload.action.preview')} (Markdown)</a>\n    EOT\n    )) + raw(<<-EOT\n      <script>\n        $('.\#{@model_name}_\#{attribute_name}').append(\"<div id='preview-\#{attribute_name}-content' class='markdown-preview'></div>\")\n        $('#preview-\#{attribute_name}-button').click(function() {\n          if ($(this).data('toggle') == 'on') {\n            var md = new Remarkable()\n            var marked_content = md.render($('#\#{@model_name}_\#{attribute_name}').val())\n            $('#\#{@model_name}_\#{attribute_name}').hide()\n            $('#preview-\#{attribute_name}-content').html(marked_content)\n            $('#preview-\#{attribute_name}-content').show()\n            $(this).data('toggle', 'off')\n            $(this).html('\#{t('carload.action.edit')} (Markdown)')\n          } else if ($(this).data('toggle') == 'off') {\n            $('#\#{@model_name}_\#{attribute_name}').show()\n            $('#preview-\#{attribute_name}-content').hide()\n            $(this).data('toggle', 'on')\n            $(this).html('\#{t('carload.action.preview')} (Markdown)')\n          }\n        })\n      </script>\n    EOT\n    )\n  else\n    form.input attribute_name\n  end\nend\n"

#generate_show(object, attribute) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/carload/dashboard_helper.rb', line 102

def generate_show object, attribute
  case attribute
  when Symbol
    object.send attribute
  when String
    res = eval "object.#{attribute.gsub('.', '&.')}"
    case res
    when String
      res
    when Array
      raw res.map { |x| "<span class='label label-primary'>#{x}</span>" }.join(' ')
    end
  when Array
    if attribute.first == :pluck
      raise UnsupportedError.new("attribute #{attribute}") if attribute.size != 3
      generate_show object, "#{attribute[1].to_s.pluralize}.pluck(:#{attribute[2]})"
    else
      generate_show object, attribute.join('.')
    end
  end
end

#generate_show_title(attribute) ⇒ Object



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
# File 'app/helpers/carload/dashboard_helper.rb', line 72

def generate_show_title attribute
  case attribute
  when Symbol
    begin
      t("activerecord.attributes.#{@model_name}.#{attribute}", raise: true)
    rescue
      t("carload.activerecord.#{attribute}", raise: true)
    end
  when String
    begin
      t("activerecord.attributes.#{@model_name}.#{attribute}", raise: true)
    rescue
      "#{t("activerecord.attributes.#{attribute}", raise: true)} (#{t("activerecord.models.#{attribute.split('.').first.to_s.singularize}", raise: true)})"
    end
  when Array
    if attribute.first == :pluck
      raise UnsupportedError.new("attribute #{attribute}") if attribute.size != 3
      model_name = attribute[1].to_s.singularize
      attribute_name = attribute[2]
      begin
        "#{t("activerecord.attributes.#{model_name}.#{attribute_name}", raise: true)} (#{t("activerecord.models.#{model_name}", raise: true)})"
      rescue
        "#{t("activerecord.attributes.#{@model_name}.#{model_name}.#{attribute_name}", raise: true)}"
      end
    else
      "#{t("activerecord.attributes.#{attribute.join('.')}", raise: true)} (#{t("activerecord.models.#{attribute[0].to_s.singularize}", raise: true)})"
    end
  end
end