aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers/table_builder_helper.rb
blob: af05a735417dddbb93cf4751fbd3772e9b9688df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
98
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
require 'table_builder_helper/column'
require 'table_builder_helper/custom_links'
require 'table_builder_helper/url'

# TODO: Add doc comment about neeeding to make a decorator for your collections
# TODO: Document global variables this uses
module TableBuilderHelper
  # TODO: rename this after migration from `table_builder`
  def table_builder_2(
    collection,
    columns,
    current_referential: nil,

    # When false, no columns will be sortable
    sortable: true,

    selectable: false,
    # selection_actions: [] ## this has been gotten rid of. The element based on this should be created elsewhere
    links: [],  # links: or actions: ? I think 'links' is better since 'actions' evokes Rails controller actions and we want to put `link_to`s here
    # TODO: get rid of this argument. Going with params instead
    sort_by: {},  # { column: 'name', direction: 'desc' }
    cls: ''  # can we rename this to "class"?
      # ^^ rename to html_options = {} at the end of the non-keyword arguments? Hrm, not a fan of combining hash args and keyword args
# sort column
# sort direction

# TODO: add `linked_column` or some such attribute that defines which column should be linked and what method to call to get it
  )

    content_tag :table,
      thead(collection, columns, sortable, selectable, links.any?) +
        tbody(collection, columns, selectable, links),
      class: cls
  end

  private

  def thead(collection, columns, sortable, selectable, has_links)
    content_tag :thead do
      content_tag :tr do
        hcont = []

        if selectable
          hcont << content_tag(:th, checkbox(id_name: '0', value: 'all'))
        end

        columns.each do |column|
          hcont << content_tag(:th, build_column_header(
            column,
            sortable,
            collection.model,
            params,
            params[:sort],
            params[:direction]
          ))
        end

        # Inserts a blank column for the gear menu
        hcont << content_tag(:th, '') if has_links

        hcont.join.html_safe
      end
    end
  end

  def tbody(collection, columns, selectable, links)
    content_tag :tbody do
      collection.map do |item|

        content_tag :tr do
          bcont = []

          if selectable
            bcont << content_tag(
              :td,
              checkbox(id_name: item.try(:id), value: item.try(:id))
            )
          end

          columns.each do |column|
            value = column.value(item)

            if column_is_linkable?(column)
              # Build a link to the `item`
              polymorph_url = URL.polymorphic_url_parts(item)
              bcont << content_tag(:td, link_to(value, polymorph_url), title: 'Voir')
            else
              bcont << content_tag(:td, value)
            end
          end

          if links.any?
            bcont << content_tag(
              :td,
              build_links(item, links),
              class: 'actions'
            )
          end

          bcont.join.html_safe
        end
      end.join.html_safe
    end
  end

  def build_links(item, links)
    trigger = content_tag :div, class: 'btn dropdown-toggle', data: { toggle: 'dropdown' } do
      content_tag :span, '', class: 'fa fa-cog'
    end

    menu = content_tag :ul, class: 'dropdown-menu' do
      (
        CustomLinks.new(item, pundit_user, links).links +
        item.action_links
      ).map do |link|
        # TODO: make a new method for this
        content_tag(
          :li,
          link_to(
            link.href,
            method: link.method,
            data: link.data
          ) do
            link.content || link.name
          end,
          class: link.method == :delete ? 'delete-action' : '',
        )
      end.join.html_safe

      # actions.map do |action|
      #   polymorph_url = []
      #
      #   unless [:show, :delete].include? action
      #     polymorph_url << action
      #   end
      #
      #   polymorph_url += polymorphic_url_parts(item)
      #
      #   if action == :delete
      #     if policy(item).present?
      #       if policy(item).destroy?
      #         # TODO: This tag is exactly the same as the one below it
      #         content_tag :li, '', class: 'delete-action' do
      #           link_to(polymorph_url, method: :delete, data: { confirm: 'Etes-vous sûr(e) de vouloir effectuer cette action ?' }) do
      #             txt = t("actions.#{action}")
      #             pic = content_tag :span, '', class: 'fa fa-trash'
      #             pic + txt
      #           end
      #         end
      #       end
      #     else
      #       content_tag :li, '', class: 'delete-action' do
      #         link_to(polymorph_url, method: :delete, data: { confirm: 'Etes-vous sûr(e) de vouloir effectuer cette action ?' }) do
      #           txt = t("actions.#{action}")
      #           pic = content_tag :span, '', class: 'fa fa-trash'
      #           pic + txt
      #         end
      #       end
      #     end
      #
      #   elsif action == :edit
      #     if policy(item).present?
      #       if policy(item).update?
      #         content_tag :li, link_to(t("actions.#{action}"), polymorph_url)
      #       end
      #     else
      #       content_tag :li, link_to(t("actions.#{action}"), polymorph_url)
      #     end
      #   elsif action == :archive
      #     unless item.archived?
      #       content_tag :li, link_to(t("actions.#{action}"), polymorph_url, method: :put)
      #     end
      #   elsif action == :unarchive
      #     if item.archived?
      #       content_tag :li, link_to(t("actions.#{action}"), polymorph_url, method: :put)
      #     end
      #   else
      #     content_tag :li, link_to(t("actions.#{action}"), polymorph_url)
      #   end
      # end.join.html_safe
    end

    content_tag :div, trigger + menu, class: 'btn-group'

  end

  def build_column_header(
    column,
    table_is_sortable,
    collection_model,
    params,
    sort_on,
    sort_direction
  )
    if !table_is_sortable
      return column.header_label(collection_model)
    end

    return column.name if !column.sortable

    direction =
      if column.key.to_s == sort_on && sort_direction == 'desc'
        'asc'
      else
        'desc'
      end

    link_to(params.merge({direction: direction, sort: column.key})) do
      arrow_up = content_tag(
        :span,
        '',
        class: "fa fa-sort-asc #{direction == 'desc' ? 'active' : ''}"
      )
      arrow_down = content_tag(
        :span,
        '',
        class: "fa fa-sort-desc #{direction == 'asc' ? 'active' : ''}"
      )

      arrow_icons = content_tag :span, arrow_up + arrow_down, class: 'orderers'

      (
        column.header_label(collection_model) +
        arrow_icons
      ).html_safe
    end
  end

  def checkbox(id_name:, value:)
    content_tag :div, '', class: 'checkbox' do
      check_box_tag(id_name, value).concat(
        content_tag(:label, '', for: id_name)
      )
    end
  end

  def column_is_linkable?(column)
    column.attribute == 'name' || column.attribute == 'comment'
  end
end