Module: EffectiveResourcesHelper

Defined in:
app/helpers/effective_resources_helper.rb

Instance Method Summary collapse

Instance Method Details

#approve_icon_to(path, options = {}) ⇒ Object



110
111
112
# File 'app/helpers/effective_resources_helper.rb', line 110

def approve_icon_to(path, options = {})
  glyphicon_to('ok', path, {title: 'Approve'}.merge(options))
end

#destroy_icon_to(path, options = {}) ⇒ Object



97
98
99
100
# File 'app/helpers/effective_resources_helper.rb', line 97

def destroy_icon_to(path, options = {})
  defaults = {title: 'Destroy', data: {method: :delete, confirm: 'Delete this item?'}}
  glyphicon_to('trash', path, defaults.merge(options))
end

#edit_icon_to(path, options = {}) ⇒ Object



93
94
95
# File 'app/helpers/effective_resources_helper.rb', line 93

def edit_icon_to(path, options = {})
  glyphicon_to('edit', path, {title: 'Edit'}.merge(options))
end

#format_resource_value(value) ⇒ Object



165
166
167
168
169
170
# File 'app/helpers/effective_resources_helper.rb', line 165

def format_resource_value(value)
  @format_resource_tags ||= ActionView::Base.sanitized_allowed_tags.to_a + ['table', 'thead', 'tbody', 'tfoot', 'tr', 'td', 'th']
  @format_resource_atts ||= ActionView::Base.sanitized_allowed_attributes.to_a + ['colspan', 'rowspan']

  simple_format(sanitize(value.to_s, tags: @format_resource_tags, attributes: @format_resource_atts), {}, sanitize: false)
end

#glyphicon_tag(icon) ⇒ Object



118
119
120
121
122
123
124
# File 'app/helpers/effective_resources_helper.rb', line 118

def glyphicon_tag(icon)
  if icon.to_s.start_with?('glyphicon-')
    (:span, '', class: "glyphicon #{icon}")
  else
    (:span, '', class: "glyphicon glyphicon-#{icon}")
  end
end

#glyphicon_to(icon, path, options = {}) ⇒ Object Also known as: bootstrap_icon_to, glyph_icon_to



126
127
128
129
130
# File 'app/helpers/effective_resources_helper.rb', line 126

def glyphicon_to(icon, path, options = {})
  (:a, options.merge(href: path)) do
    glyphicon_tag(icon)
  end
end

#number_to_duration(duration) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/effective_resources_helper.rb', line 76

def number_to_duration(duration)
  duration = duration.to_i
  value = duration.abs

  [
    ('-' if duration < 0),
    ("#{value / 60}h " if value >= 60),
    ("#{'%0.2d' % (value % 60)}m" if value > 0),
    ('0m' if value == 0),
  ].compact.join
end

#ok_icon_to(path, options = {}) ⇒ Object



106
107
108
# File 'app/helpers/effective_resources_helper.rb', line 106

def ok_icon_to(path, options = {})
  glyphicon_to('ok', path, {title: 'OK'}.merge(options))
end

#remove_icon_to(path, options = {}) ⇒ Object



114
115
116
# File 'app/helpers/effective_resources_helper.rb', line 114

def remove_icon_to(path, options = {})
  glyphicon_to('remove', path, {title: 'Remove'}.merge(options))
end

#render_resource_form(resource) ⇒ Object

When called from /admin/things/new.html.haml this will render ‘admin/things/form’, or ‘things/form’, or ‘thing/form’



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/effective_resources_helper.rb', line 62

def render_resource_form(resource)
  atts = {:namespace => (resource.namespace.to_sym if resource.namespace.present?), resource.name.to_sym => instance_variable_get('@' + resource.name)}.compact

  if lookup_context.template_exists?('form', controller._prefixes, :partial)
    render 'form', atts
  elsif lookup_context.template_exists?('form', resource.plural_name, :partial)
    render "#{resource.plural_name}/form", atts
  elsif lookup_context.template_exists?('form', resource.name, :partial)
    render "#{resource.name}/form", atts
  else
    render 'form', atts  # Will raise the regular error
  end
end

#settings_icon_to(path, options = {}) ⇒ Object



102
103
104
# File 'app/helpers/effective_resources_helper.rb', line 102

def settings_icon_to(path, options = {})
  glyphicon_to('cog', path, {title: 'Settings'}.merge(options))
end

#show_icon_to(path, options = {}) ⇒ Object

Icon Helpers for actions_column or elsewhere



89
90
91
# File 'app/helpers/effective_resources_helper.rb', line 89

def show_icon_to(path, options = {})
  glyphicon_to('eye-open', path, {title: 'Show'}.merge(options))
end

#simple_form_save(form, label = 'Save', options = {}, &block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/helpers/effective_resources_helper.rb', line 52

def simple_form_save(form, label = 'Save', options = {}, &block)
  wrapper_options = { class: 'form-actions' }.merge(options.delete(:wrapper_html) || {})
  options = { class: 'btn btn-primary', data: { disable_with: 'Saving...'} }.merge(options)

  (:div, wrapper_options) do
    form.button(:submit, label, options) + (capture(&block) if block_given?)
  end
end

#simple_form_submit(form, options = {}, &block) ⇒ 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
# File 'app/helpers/effective_resources_helper.rb', line 3

def simple_form_submit(form, options = {}, &block)
  resource = (@_effective_resource || Effective::Resource.new(controller_path))

  # Apply btn-primary to the first item, only if the class isn't already present
  actions = if controller.respond_to?(:member_actions_for)
    controller.member_actions_for(form.object).transform_values.with_index do |opts, index|
      if opts[:class].present?
        opts
      else
        opts[:class] = "btn #{index == 0 ? 'btn-primary' : 'btn-default'}"
      end

      opts
    end
  else
    {}.tap do |actions|
      actions['Save'] = { class: 'btn btn-primary', data: { disable_with: 'Saving...' }}

      if resource.action_path(:index) && EffectiveResources.authorized?(controller, :index, resource.klass)
        actions['Continue'] = { class: 'btn btn-default', data: { disable_with: 'Saving...' }}
      end

      if resource.action_path(:new) && EffectiveResources.authorized?(controller, :new, resource.klass)
        actions['Add New'] = { class: 'btn btn-default', data: { disable_with: 'Saving...' }}
      end

    end
  end

  wrapper_options = { class: 'form-actions' }.merge(options.delete(:wrapper_html) || {})

  (:div, wrapper_options) do
    buttons = actions.group_by { |_, args| args[:class] }.flat_map do |_, action|
      action.map { |action| form.button(:submit, *action) } + ['']
    end

    # I think this is a bug. I can't override default button class when passing my own class: variable. it merges them.
    if (btn_class = SimpleForm.button_class).present?
      buttons = buttons.map { |button| button.sub(btn_class, '') }
    end

    if block_given?
      buttons = [capture(&block), ''] + buttons
    end

    buttons.join('&nbsp;').html_safe
  end
end

#tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true) ⇒ Object

This is used by effective_orders, effective_logging, effective_trash and effective_mergery



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/helpers/effective_resources_helper.rb', line 137

def tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true)
  case obj
  when Hash
    if flatten && obj[:attributes].kind_of?(Hash)
      obj = obj[:attributes].merge(obj.except(:attributes))
    end

    (:table, class: table.presence) do
      (:tbody) do
        obj.map do |key, value|
          (:tr, class: key.to_param) do
            ((th == true ? :th : :td), key) +
            (:td) { tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) }
          end
        end.join.html_safe
      end
    end
  when Array
    obj.map { |value| tableize_hash(value, table: sub_table, th: sub_th, sub_table: sub_table, sub_th: sub_th, flatten: flatten) }.join('<br>')
  when Symbol
    ":#{obj}"
  when NilClass
    '-'
  else
    obj.to_s.presence || '""'
  end.html_safe
end