Class: Ez::Resources::CollectionCell

Inherits:
ApplicationCell show all
Includes:
Pagy::Frontend, Ransack::Helpers::FormHelper
Defined in:
app/cells/ez/resources/collection_cell.rb

Constant Summary

Constants inherited from ApplicationCell

ApplicationCell::CSS_SCOPE

Instance Method Summary collapse

Methods inherited from ApplicationCell

#css_for, #custom_css_map, #div_for, form, #t

Instance Method Details



78
79
80
81
82
83
# File 'app/cells/ez/resources/collection_cell.rb', line 78

def as_a_link(record, column)
  return unless model.actions.include?(:show)
  return unless Manager::Hooks.can?(:can_read?, model, record)

  link_to record.public_send(column.name), model.path_for(action: :show, id: record.id)
end

#collectionObject



19
20
21
# File 'app/cells/ez/resources/collection_cell.rb', line 19

def collection
  @collection ||= model.data
end

#column_title(column) ⇒ Object



122
123
124
# File 'app/cells/ez/resources/collection_cell.rb', line 122

def column_title(column)
  column.sortable ? sort_link(search, column.name, column.title) : column.title
end

#custom_action_label(action) ⇒ Object



59
60
61
# File 'app/cells/ez/resources/collection_cell.rb', line 59

def custom_action_label(action)
  action.options[:title] || t("actions.#{action.name}")
end


85
86
87
88
89
90
91
# File 'app/cells/ez/resources/collection_cell.rb', line 85

def edit_link(record)
  return unless model.actions.include?(:edit)
  return unless Manager::Hooks.can?(:can_read?, model, record)

  link_to t('actions.edit'), model.path_for(action: :edit, id: record.id),
          class: css_for('collection-table-td-action-item')
end

#header_textObject



23
24
25
# File 'app/cells/ez/resources/collection_cell.rb', line 23

def header_text
  resources_name
end


63
64
65
66
67
68
# File 'app/cells/ez/resources/collection_cell.rb', line 63

def new_link
  return unless model.actions.include?(:new)
  return unless Manager::Hooks.can?(:can_create?, model)

  link_to t('actions.add'), model.path_for(action: :new), class: css_for('actions-new-link')
end

#paginationObject



102
103
104
105
106
107
108
# File 'app/cells/ez/resources/collection_cell.rb', line 102

def pagination
  if Ez::Resources.config.pagination_method
    instance_exec paginator, &Ez::Resources.config.pagination_method
  else
    pagy_nav(paginator)
  end
end

#record_column_value(record, column) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/cells/ez/resources/collection_cell.rb', line 27

def record_column_value(record, column)
  result = case column.type
           when :association
             column.getter.call(record)
           when :boolean
             maybe_use_custom_boolean_presenter(record.public_send(column.name))
           when :custom
             column.builder.call(record)
           when :image
             url = column.getter.call(record)

             image_tag url, column.options if url
           when :link
             as_a_link(record, column)
           else
             record.public_send(column.name)
           end

  result = column.presenter.call(record) if column.presenter
  result
end

#record_tr(record, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/cells/ez/resources/collection_cell.rb', line 49

def record_tr(record, &block)
  if model.actions.include?(:show) && Manager::Hooks.can?(:can_read?, model, record)
     :tr,
                class: css_for('collection-table-tr'), id: "#{model.model.name.demodulize.pluralize.downcase}-#{record.id}", data: { link: model.path_for(action: :show, id: record.id).to_s }, &block
  else
     :tr,
                class: css_for('collection-table-tr'), id: "#{model.model.name.demodulize.pluralize.downcase}-#{record.id}", &block
  end
end


93
94
95
96
97
98
99
100
# File 'app/cells/ez/resources/collection_cell.rb', line 93

def remove_link(record)
  return unless model.actions.include?(:destroy)
  return unless Manager::Hooks.can?(:can_destroy?, model, record)

  link_to t('actions.remove'), model.path_for(action: :destroy, id: record.id),
          method: :delete,
          class:  css_for('collection-table-td-action-item')
end

#showObject



11
12
13
14
15
16
17
# File 'app/cells/ez/resources/collection_cell.rb', line 11

def show
  if model.params[:view]
    render params[:view]
  else
    render :table
  end
end


70
71
72
73
74
75
76
# File 'app/cells/ez/resources/collection_cell.rb', line 70

def show_link(record)
  return unless model.actions.include?(:show)
  return unless Manager::Hooks.can?(:can_read?, model, record)

  link_to t('actions.show'), model.path_for(action: :show, id: record.id),
          class: css_for('collection-table-td-action-item')
end


110
111
112
113
114
115
116
117
118
119
120
# File 'app/cells/ez/resources/collection_cell.rb', line 110

def view_switch_link(view_name)
  return unless model.collection_views.present?

  params = model.params.to_unsafe_hash.slice(:q, :page, :s).symbolize_keys.merge(view: view_name)
  selected = css_for('collection-view-selected-link') if model.params[:view] == view_name.to_s

  link_to model.path_for(action: :index, params: params), id:    "ez-view-#{view_name}",
                                                          class: css_for("collection-view-link-#{view_name}", selected) do
     :i, nil, class: css_for("collection-view-link-#{view_name}-icon")
  end
end