Module: Admin::Resources::TableHelper

Defined in:
app/helpers/admin/resources/table_helper.rb

Instance Method Summary collapse

Instance Method Details

#build_table(model, fields, items, link_options = {}, association = nil, association_name = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'app/helpers/admin/resources/table_helper.rb', line 3

def build_table(model, fields, items, link_options = {}, association = nil, association_name = nil)
  locals = { :model => model,
             :fields => fields,
             :items => items,
             :link_options => link_options,
             :headers => table_header(model, fields),
             :association_name => association_name }

  render "helpers/admin/resources/table", locals
end

#table_actions(model, item, association_name = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/admin/resources/table_helper.rb', line 45

def table_actions(model, item, association_name = nil)
  resource_actions.reject! do |body, url, options, proc|
    admin_user.cannot?(url[:action], model.name)
  end

  resource_actions.map do |body, url, options, proc|
    next if proc && proc.respond_to?(:call) && proc.call(item) == false
    { :message => Typus::I18n.t(body),
      :url => params.dup.cleanup.merge({ :controller => "/admin/#{model.to_resource}", :id => item.id }).merge(url),
      :options => options }
  end
end

#table_fields_for_item(item, fields) ⇒ Object



41
42
43
# File 'app/helpers/admin/resources/table_helper.rb', line 41

def table_fields_for_item(item, fields)
  fields.map { |k, v| send("table_#{v}_field", k, item) }
end

#table_header(model, fields, params = params) ⇒ Object



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
# File 'app/helpers/admin/resources/table_helper.rb', line 14

def table_header(model, fields, params = params)
  fields.map do |key, value|

    key = key.gsub(".", " ") if key.to_s.match(/\./)
    content = model.human_attribute_name(key)

    if params[:action].eql?('index') && model.typus_options_for(:sortable)
      association = model.reflect_on_association(key.to_sym)
      order_by = association ? association.foreign_key : key

      if (model.model_fields.map(&:first).map(&:to_s).include?(key) || model.reflect_on_all_associations(:belongs_to).map(&:name).include?(key.to_sym))
        sort_order = case params[:sort_order]
                     when 'asc' then ['desc', '↓']
                     when 'desc' then ['asc', '↑']
                     else [nil, nil]
                     end
        switch = sort_order.last if params[:order_by].eql?(order_by)
        options = { :order_by => order_by, :sort_order => sort_order.first }
        message = [content, switch].compact.join(" ").html_safe
        content = link_to(message, params.merge(options))
      end
    end

    content
  end
end