Module: EffectiveResourcesHelper

Defined in:
app/helpers/effective_resources_helper.rb

Instance Method Summary collapse

Instance Method Details

#effective_submit(form, options = {}, &block) ⇒ Object

effective_bootstrap



3
4
5
6
7
8
9
10
11
# File 'app/helpers/effective_resources_helper.rb', line 3

def effective_submit(form, options = {}, &block) # effective_bootstrap
  resource = (controller.class.respond_to?(:effective_resource) ? controller.class.effective_resource : Effective::Resource.new(controller_path))
  actions = resource.submits_for(form.object, controller: controller)
  buttons = actions.map { |name, opts| form.save(name, opts) }.join.html_safe

  form.submit('', options) do
    (block_given? ? capture(&block) : ''.html_safe) + buttons
  end
end

#format_resource_value(value) ⇒ Object



98
99
100
101
102
103
# File 'app/helpers/effective_resources_helper.rb', line 98

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

#number_to_duration(duration) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/effective_resources_helper.rb', line 56

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

#render_resource_form(resource, atts = {}) ⇒ Object

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/helpers/effective_resources_helper.rb', line 42

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

  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

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



32
33
34
35
36
37
38
39
# File 'app/helpers/effective_resources_helper.rb', line 32

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

effective_form_inputs



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/effective_resources_helper.rb', line 14

def simple_form_submit(form, options = {}, &block)
  resource = (controller.class.respond_to?(:effective_resource) ? controller.class.effective_resource : Effective::Resource.new(controller_path))
  actions = resource.submits_for(form.object, controller: controller)

  buttons = actions.map { |action| form.button(:submit, *action) }

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

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

  (:div, wrapper_options) do
    (block_given? ? capture(&block) : ''.html_safe) + buttons.join('&nbsp;').html_safe
  end
end

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

Tableize attributes This is used by effective_orders, effective_logging, effective_trash and effective_mergery



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
# File 'app/helpers/effective_resources_helper.rb', line 70

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