Module: AbAdmin::Views::AdminNavigationHelpers

Defined in:
lib/ab_admin/views/admin_navigation_helpers.rb

Instance Method Summary collapse

Instance Method Details


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 99

def action_link(action)
  case action
    when :new
      link_to_can? :create, t('admin.actions.new.link'), new_resource_path, class: 'btn btn-primary new_resource', remote: settings[:list_edit]
    when :edit
      link_to_can? :update, t('admin.actions.edit.link'), edit_resource_path, class: 'btn btn-primary'
    when :destroy
      link_to_can? :destroy, t('admin.actions.destroy.link'), resource_path, method: :delete, data: {confirm: t('admin.delete_confirmation')},
                   class: 'btn btn-danger'
    when :show
      link_to_can? :show, t('admin.actions.show.link'), resource_path, class: 'btn btn-info'
    when :preview
      if path = preview_resource_path(resource)
        link_to t('admin.actions.preview.link'), path, class: 'btn btn-inverse', title: t('admin.actions.preview.link'), target: '_blank'
      end
    when :history
      link_to_can? :history, t('admin.actions.history.link'), history_resource_path, class: 'btn btn-info'
    when AbAdmin::Config::ActionItem
      instance_exec(&action.data) if action.for_context?(self)
    else
      resource_action_link_method = "#{resource_instance_name}_action_link"
      list_link_method = "#{resource_instance_name}_#{action}_link"
      if respond_to?(list_link_method)
        send(list_link_method)
      elsif respond_to?(resource_action_link_method)
        send(resource_action_link_method, action)
      end
  end
end

#admin_comments_button(item) ⇒ Object


191
192
193
194
195
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 191

def admin_comments_button(item)
  title = [icon('comment', true), item.admin_comments_count_non_zero].compact.join(' ').html_safe
  link_to title, admin_admin_comments_path(resource_type: item.class.name, resource_id: item.id), remote: true,
          class: 'btn btn-info list_admin_comments_link'
end

241
242
243
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 241

def admin_edit_link(record, options = {})
  admin_record_link(record, :edit, options)
end

149
150
151
152
153
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 149

def admin_menu_link(name, path)
   :li, class: ('active' if path == request.path_info.split('/')[2]) do
    link_to name, "/admin/#{path}"
  end
end

155
156
157
158
159
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 155

def admin_menu_link_without_model(name, path, path_name)
   :li, class: ('active' if path_name == request.url.split('/').last) do
    link_to name, path
  end
end

249
250
251
252
253
254
255
256
257
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 249

def admin_record_link(record, action, options={})
  return unless record
  record_title = options.delete(:title) || AbAdmin.display_name(record)
  #url_options = {controller: record.class.model_name.plural, action: action, id: record.id}
  action_url_part = action == :show ? '' : "/#{action}"
  url = "/admin/#{record.class.model_name.plural}/#{record.id}#{action_url_part}"
  html_options = options.delete(:html_options) || {}
  link_to record_title, url, html_options
end

245
246
247
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 245

def admin_show_link(record, options = {})
  admin_record_link(record, :show, options)
end

229
230
231
232
233
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 229

def auto_edit_link(record, options = {})
  return options[:missing] unless record
  return options[:title] || AbAdmin.display_name(record) if cannot?(:edit, record)
  admin_edit_link(record, options)
end

235
236
237
238
239
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 235

def auto_show_link(record, options = {})
  return options[:missing] unless record
  return options[:title] || AbAdmin.display_name(record) if cannot?(:read, record)
  admin_show_link(record, options)
end

#batch_action_item(item) ⇒ Object


205
206
207
208
209
210
211
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 205

def batch_action_item(item)
  if settings[:batch]
     :td do
      check_box_tag 'by_ids[]', item.id, false, id: "batch_action_item_#{item.id}", class: 'batch_check'
    end
  end
end

#batch_action_toggleObject


197
198
199
200
201
202
203
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 197

def batch_action_toggle
  if settings[:batch]
     :th do
      check_box_tag '', '', false, id: nil, class: 'toggle'
    end
  end
end

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 213

def id_link(item, options={})
  if options.is_a?(TrueClass) || options.is_a?(FalseClass)
    ActiveSupport::Deprecation.warn("use `id_link(item, edit: false)` instead of`id_link(item, false)`")
    options = {edit: edit}
  end
  options.reverse_merge!(edit: true, title: item.id)
  link_html = {remote: settings[:list_edit], class: 'resource_id_link'}
  if options[:edit] && can?(:edit, item)
    link_to options[:title], edit_resource_path(item), link_html
  elsif can?(:read, item)
    link_to options[:title], resource_path(item), link_html
  else
    options[:title]
  end
end

#item_index_actions(item) ⇒ Object


183
184
185
186
187
188
189
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 183

def item_index_actions(item)
  actions = resource_action_items.map do |act|
    short_action_link(act, item)
  end
  actions << admin_comments_button(item) if settings[:comments] && settings[:comments][:list]
  actions.join(' ').html_safe
end

#item_index_actions_panel(item) ⇒ Object


172
173
174
175
176
177
178
179
180
181
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 172

def item_index_actions_panel(item)
  content = "#{item_index_actions(item)}#{capture{yield item} if block_given?}"
  <<-HTML.html_safe
    <td class="actions_panel">
      <div class="actions_panel-wrap_outer">
        <div class="actions_panel-wrap_inner">#{content}</div>
      </div>
    </td>
  HTML
end

Returns:

  • (Boolean)

133
134
135
136
137
138
139
140
141
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 133

def item_link_to_can?(act, item, *args, &block)
  if can?(act, item)
    if block_given?
      link_to(*args, &block)
    else
      link_to(*args)
    end
  end
end

Returns:

  • (Boolean)

129
130
131
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 129

def link_to_can?(act, *args, &block)
  item_link_to_can?(act, get_subject, *args, &block)
end

18
19
20
21
22
23
24
25
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 18

def list_sort_link(attribute, options={})
  adapter = options[:adapter] || @search
  if adapter && adapter.klass == resource_class
    sort_link(adapter, attribute, options)
  else
    attribute.is_a?(Symbol) ? ha(attribute) : attribute
  end
end

143
144
145
146
147
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 143

def model_admin_menu_link(model)
   :li, class: ('active' if controller_name.split('/').last == model.model_name.plural) do
    link_to model.model_name.human(count: 9), "/admin/#{model.model_name.plural}"
  end
end

#order_indicator_for(order) ⇒ Object


56
57
58
59
60
61
62
63
64
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 56

def order_indicator_for(order)
  if order == 'asc'
    '&#9650;'
  elsif order == 'desc'
    '&#9660;'
  else
    '<span class="order_indicator">&#9650;&#9660;</span>'
  end
end

#pagination_info(c) ⇒ Object


161
162
163
164
165
166
167
168
169
170
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 161

def pagination_info(c)
  offset = c.offset
  total_entries = c.total_entries
  if total_entries.zero?
    t('will_paginate.pagination_info_empty')
  else
    per_page = (params[:per_page] || resource_class.per_page).to_i
    t('will_paginate.pagination_info', from: offset + 1, to: [offset + per_page, total_entries].min, count: total_entries).html_safe
  end
end

#search_admin_form_for(object, *args, &block) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 5

def search_admin_form_for(object, *args, &block)
  params[:q] ||= {}
  options = args.extract_options!
  options[:html] ||= {}
  options[:html][:id] ||= 'search_form'
  options[:html][:class] ||= 'pjax-form'
  options[:builder] ||= ::AbAdmin::Views::SearchFormBuilder
  options[:compact_labels] = settings.dig(:search_form, :compact_labels)
  options[:method] ||= :get
  options[:as] ||= 'q'
  form_for([:admin, object].flatten, *(args << options), &block)
end

66
67
68
69
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
97
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 66

def short_action_link(action, item)
  case action
    when :edit
      item_link_to_can? :update, item, icon('pencil', true), edit_resource_path(item), remote: settings[:list_edit],
                        class: 'btn btn-primary', title: t('admin.actions.edit.link')
    when :destroy
      item_link_to_can? :destroy, item, icon('trash', true), resource_path(item, return_to: request.url),
                        method: :delete, data: {confirm: t('admin.delete_confirmation')},
                        class: 'btn btn-danger', title: t('admin.actions.destroy.link')
    when :show
      item_link_to_can? :show, item, icon('info-sign', true), resource_path(item),
                        class: 'btn btn-info', title: t('admin.actions.show.link')
    when :preview
      preview_path = preview_resource_path(item)
      if preview_path
        link_to icon('eye-open', true), preview_path, class: 'btn btn-inverse', title: t('admin.actions.preview.link'), target: '_blank'
      end
    when :history
      item_link_to_can? :history, item, icon('book', true), history_resource_path(item),
                        class: 'btn btn-info', title: t('admin.actions.history.link')
    when AbAdmin::Config::ActionItem
      instance_exec(item, &action.data) if action.for_context?(self)
    else
      resource_action_link_method = "#{resource_instance_name}_short_action_link"
      list_link_method = "#{resource_instance_name}_#{action}_list_link"
      if respond_to?(list_link_method)
        send(list_link_method, item)
      elsif respond_to?(resource_action_link_method)
        send(resource_action_link_method, action, item)
      end
  end
end

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
# File 'lib/ab_admin/views/admin_navigation_helpers.rb', line 27

def sort_link(search, attribute, *args)
  name = attribute.is_a?(Symbol) ? ha(attribute) : attribute
  return name unless search

  options = args.first.is_a?(Hash) ? args.shift.dup : {}
  search_params = (params[:q] || {}).to_h.with_indifferent_access
  attr_name = (options.delete(:column) || attribute).to_s
  default_order = options.delete :default_order

  if existing_sort = search.sorts.detect { |s| s.name == attr_name }
    prev_attr, prev_dir = existing_sort.name, existing_sort.dir
    current_dir = prev_attr == attr_name ? prev_dir : nil
  else
    current_dir = nil
  end

  if current_dir
    new_dir = current_dir == 'desc' ? 'asc' : 'desc'
  else
    new_dir = default_order || 'asc'
  end

  html_options = options.delete(:html_options) || {}
  html_options[:class] = ['sort_link', current_dir, html_options[:class]].compact.join(' ')

  options.merge!(q: search_params.merge(s: "#{attr_name} #{new_dir}"))
  link_to [name, order_indicator_for(current_dir)].join(' ').html_safe, url_for(options), html_options
end