Module: Admin::TagsHelper

Defined in:
app/helpers/admin/tags_helper.rb

Instance Method Summary collapse

Instance Method Details



3
4
5
6
7
# File 'app/helpers/admin/tags_helper.rb', line 3

def breadcrumb_tag(controller, action_name)
  tag_1 = link_to(controller.controller_name_human, url_for(controller: controller_path))
  tag_2 = link_to(controller.action_name_human(action_name), 'javascript:;', class: 'active')
  tag_1.concat(tag_2)
end

#collection_filters_tag(decorator, filters_name) ⇒ Object

筛选栏



30
31
32
33
34
35
36
37
# File 'app/helpers/admin/tags_helper.rb', line 30

def collection_filters_tag(decorator, filters_name)
  Array(decorator.try(filters_name)).each do |filter_key, filter_name|
    concat(filter_tag(decorator, filter_key, filter_name))
  end

  # 这里要返回nil,因为用的each 有额外的返回值
  nil
end

#collection_search_tag(decorator, search_name) ⇒ Object

搜索栏



40
41
42
43
44
45
46
47
48
# File 'app/helpers/admin/tags_helper.rb', line 40

def collection_search_tag(decorator, search_name)
  (search_options = decorator.try(search_name)).blank? and return

  name, placeholder, action = search_options.values_at(:name, :placeholder, :action)
  form_tag(action, class: 'pull-right collection_search', method: :get, enforce_utf8: false) do
    concat(text_field_tag(name, params[name], placeholder: placeholder))
    concat(button_tag((:i, nil, class: 'glyphicon glyphicon-search'), type: nil, name: nil))
  end
end

#collection_table_tag(records, decorator, attribute_name, options = {}) ⇒ Object

列表table



93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/admin/tags_helper.rb', line 93

def collection_table_tag(records, decorator, attribute_name, options = {})
  (:table, class: 'table table-hover', id: decorator.model.name.downcase.pluralize) do
    concat(table_thead_tag(records.model, decorator, attribute_name))
    concat(table_tbody_tag(records, decorator, attribute_name, options))
    concat(hidden_field_tag(:per_page, records.limit_value))
    concat(hidden_field_tag(:current_page, records.current_page))
    concat(hidden_field_tag(:is_last_page, records.last_page?))
    concat(hidden_field_tag(:total_pages, records.total_pages))
  end
end

#filter_tag(decorator, filter_key, filter_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/admin/tags_helper.rb', line 18

def filter_tag(decorator, filter_key, filter_name)
  button_text = [filter_name, (:span, nil, class: 'caret')].join("\n").html_safe
  button_options = {type: 'button', name: filter_key}
  button_html_options = {class: 'btn btn-default btn-sm', data: {toggle: 'dropdown'}, aria: {haspopup: true, expanded: false}}

  (:div, class: 'pull-left dropdown filters') do
    concat(button_tag(button_text, button_options.merge(button_html_options)))
    concat((:ul, decorator.filter_for(filter_key), class: 'dropdown-menu'))
  end
end

#new_model_tag(klass) ⇒ Object

添加对象按钮TODO 添加权限



11
12
13
14
15
16
# File 'app/helpers/admin/tags_helper.rb', line 11

def new_model_tag(klass)
  name = [t('actions.new'), klass.model_name.human].join
  url = url_for(controller: klass.model_name.route_key, action: 'new')
  html_options = {class: 'btn btn-primary clearfix'}
  link_to(name, url, html_options)
end

#render_class_actions(decorator) ⇒ Object



62
63
64
65
66
# File 'app/helpers/admin/tags_helper.rb', line 62

def render_class_actions(decorator)
  Array(decorator.permitted_class_methods_for(action_name)).collect { |action|
    render_to_string("actions/#{action}", layout: false)
  }.join("\n").html_safe
end

#render_instance_actions(decorate) ⇒ Object

列表页操作按钮



69
70
71
72
73
# File 'app/helpers/admin/tags_helper.rb', line 69

def render_instance_actions(decorate)
  Array(decorate.class.permitted_instance_methods_for(action_name)).collect { |action|
    render_to_string("actions/index_#{action}", locals: {record: decorate.object}, layout: false)
  }.join("\n").html_safe
end

#table_tbody_tag(records, decorator, attribute_name, options) ⇒ Object

列表页数据内容



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/admin/tags_helper.rb', line 76

def table_tbody_tag(records, decorator, attribute_name, options)
  per_page = options.delete(:per_page)
  (:tbody) do
    records.each_with_index do |record, index|
      decorate = decorator.new(record)

      tds = Array(decorate.try(attribute_name)).collect do |field|
        td_val = field == '#' ? (( records.current_page - 1 ) * (per_page || records.default_per_page)) + index + 1 : decorate.human(field)
        (:td, td_val)
      end.join("\n").html_safe
      tds.concat((:td, render_instance_actions(decorate))) if decorator.permitted_instance_methods_for(action_name)
      concat((:tr, tds, 'data-id' => record.id))
    end
  end
end

#table_thead_tag(model, decorator, attribute_name) ⇒ Object

列表栏表头



51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/admin/tags_helper.rb', line 51

def table_thead_tag(model, decorator, attribute_name)
  (collection_attributes = decorator.try(attribute_name)).blank? and return

  (:thead) do
    (:tr) do
      collection_attributes.map { |field| concat((:th, model.human_attribute_name(field))) }
      concat((:th, t('action'))) if decorator.permitted_instance_methods_for(action_name)
    end
  end
end