aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers/table_builder_helper.rb
blob: 3e8602dac7a14e98af42c41955e62abe6b262093 (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
module TableBuilderHelper
  # TODO: rename this after migration from `table_builder`
  def table_builder_2(
    collection,
    columns,
    current_referential: nil,
    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
    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, selectable, links) +
        tbody(collection, columns, selectable, links),
      class: cls
  end

  private

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

        # Adds checkbox to table header
        if selectable
          cbx = content_tag :div, '', class: 'checkbox' do
            check_box_tag('0', 'all').concat(content_tag(:label, '', for: '0'))
          end
          hcont << content_tag(:th, cbx)
        end

        columns.map do |k, v|
          # These columns are hard-coded to not be sortable
          if ["ID Codif", "Oid", "OiD", "ID Reflex", "Arrêt de départ", "Arrêt d'arrivée", "Période de validité englobante", "Période englobante", "Nombre de courses associées", "Journées d'application"].include? k
            hcont << content_tag(:th, k)
          else
            hcont << content_tag(:th, sortable_columns(collection, k))
          end
        end
        # Inserts a blank column for the gear menu
        hcont << content_tag(:th, '') if links.any?

        hcont.join.html_safe
      end
    end
  end

  def tbody(collection, columns, selectable, links)
    # TODO: refactor
    content_tag :tbody do
      collection.collect do |item|

        content_tag :tr do
          bcont = []

          # Adds item checkboxes whose value = the row object's id
          # Apparently the object id is also used in the HTML id attribute without any prefix
          if selectable
            # TODO: Extract method `build_checkbox(attribute)`
            cbx = content_tag :div, '', class: 'checkbox' do
              check_box_tag(item.try(:id), item.try(:id)).concat(content_tag(:label, '', for: item.try(:id)))
            end
            bcont << content_tag(:td, cbx)
          end

          columns.map do |k, attribute|
            value =
              if Proc === attribute
                attribute.call(item)
              else
                item.try(attribute)
              end
            # if so this column's contents get transformed into a link to the object
            if attribute == 'name' || attribute == 'comment'
              lnk = []

              # #is_a? ? ; or ?
              unless item.class == Calendar || item.class == Referential
                if current_referential
                  lnk << current_referential
                  lnk << item.line if item.respond_to? :line
                  lnk << item.route.line if item.class == Chouette::RoutingConstraintZone
                  lnk << item if item.respond_to? :line_referential
                  lnk << item.stop_area if item.respond_to? :stop_area
                  lnk << item if item.respond_to? :stop_points || item.class.to_s == 'Chouette::TimeTable'
                elsif item.respond_to? :referential
                  lnk << item.referential
                end
              else
                lnk << item
              end

              bcont << content_tag(:td, link_to(value, lnk), title: 'Voir')
            else
              bcont << content_tag(:td, value)
            end
          end
          bcont << content_tag(:td, links_builder(item, links), class: 'actions') if links.any?

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

  # TODO: `def build_link[s]`
  def links_builder(item, actions)
    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
      actions.collect do |action|
        polymorph_url = []

        unless [:show, :delete].include? action
          polymorph_url << action
        end

        unless item.class == Calendar || item.class == Referential
          if current_referential
            polymorph_url << current_referential
            polymorph_url << item.line if item.respond_to? :line
            polymorph_url << item.route.line if item.class == Chouette::RoutingConstraintZone
            polymorph_url << item if item.respond_to? :line_referential
            polymorph_url << item.stop_area if item.respond_to? :stop_area
            polymorph_url << item if item.respond_to? :stop_points || item.class.to_s == 'Chouette::TimeTable'
          elsif item.respond_to? :referential
            polymorph_url << item.referential
          end
        else
          polymorph_url << item
        end

        if action == :delete
          if policy(item).present?
            if policy(item).destroy?
              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

  # TODO: clean up?
  def sortable_columns collection, key
      # #<ActiveRecord::Relation [#<Referential id: 4, name: "Referential Yanis Gaillard", slug: "referential_yanis_gaillard", created_at: "2017-05-02 12:37:38", updated_at: "2017-05-02 12:37:49", prefix: "c4nqg22nvt", projection_type: nil, time_zone: "Paris", bounds: nil, organisation_id: 1, geographical_bounds: nil, user_id: nil, user_name: nil, data_format: "neptune", line_referential_id: 1, stop_area_referential_id: 1, workbench_id: 1, archived_at: nil, created_from_id: nil, ready: true>, #<Referential id: 3, name: "Test Referential 2017.04.25", slug: "test_referential_20170425", created_at: "2017-04-25 10:08:49", updated_at: "2017-04-25 10:08:51", prefix: "test_referential_20170425", projection_type: "", time_zone: "Paris", bounds: "SRID=4326;POLYGON((-5.2 42.25,-5.2 51.1,8.23 51.1,...", organisation_id: 1, geographical_bounds: nil, user_id: 1, user_name: "Wing Teddy", data_format: "neptune", line_referential_id: 1, stop_area_referential_id: 1, workbench_id: 1, archived_at: nil, created_from_id: nil, ready: true>]>
    # (byebug) collection.model
    # Referential(id: integer, name: string, slug: string, created_at: datetime, updated_at: datetime, prefix: string, projection_type: string, time_zone: string, bounds: string, organisation_id: integer, geographical_bounds: text, user_id: integer, user_name: string, data_format: string, line_referential_id: integer, stop_area_referential_id: integer, workbench_id: integer, archived_at: datetime, created_from_id: integer, ready: boolean)
    # params = {"controller"=>"workbenches", "action"=>"show", "id"=>"1", "q"=>{"archived_at_not_null"=>"1", "archived_at_null"=>"1"}}
    direction = (key.to_s == params[:sort] && params[:direction] == 'desc') ? 'asc' : 'desc'

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

      pics = content_tag :span, pic1 + pic2, class: 'orderers'
      # TODO: figure out a way to maybe explicitise the dynamicness of getting the model type from the `collection`.
      # TODO: rename `pics` to something like `icons` or arrow icons or some such

      (column_header_label(collection.model, key) + pics).html_safe
    end
  end

  def column_header_label(model, field)
    # Transform `Chouette::Line` into "line"
    model_key = model.to_s.demodulize.underscore

    I18n.t("activerecord.attributes.#{model_key}.#{field}")
  end
end