diff options
| author | Teddy Wing | 2017-06-08 11:36:03 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-06-08 11:36:03 +0200 |
| commit | c3d752af1ed040f64cd7cbfb964c2c5d5d8f7cde (patch) | |
| tree | 4f1b2fda6aa3d0195cbef9935b095ef4434b1468 | |
| parent | 212d1f740b17ae89b69603a3f1964351a38b86f6 (diff) | |
| download | chouette-core-c3d752af1ed040f64cd7cbfb964c2c5d5d8f7cde.tar.bz2 | |
Add in-progress `TableBuilderHelper` (WIP)
Porting `table_builder` code over from `NewapplicationHelper`. The code
is very much in progress and incomplete. Only decided to commit it now
because I keep making changes and want to have those tracked so I don't
lose anything.
A lot of the original is commented out and the initial test I wrote
doesn't pass. The test also doesn't use the proper data, I just copied
it from the website's output.
Anyway, this is sort of the starting point.
Refs #3479
| -rw-r--r-- | app/helpers/table_builder_helper.rb | 107 | ||||
| -rw-r--r-- | spec/helpers/table_builder_helper_spec.rb | 120 |
2 files changed, 227 insertions, 0 deletions
diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb new file mode 100644 index 000000000..91eef5a47 --- /dev/null +++ b/app/helpers/table_builder_helper.rb @@ -0,0 +1,107 @@ +module TableBuilderHelper + # TODO: rename this after migration from `table_builder` + def table_builder_2( + collection, + columns, + sortable: true, + selectable: false, # TODO: is this necessary? + # 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"? +# 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 + ) + # TODO: Maybe move this to a private method + head = 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 + # FIXME: error undefined `current_referential` + # hcont << content_tag(:th, sortable_columns(collection, k)) + # temporarily replaced with: + hcont << content_tag(:th, k) + end + end + # Inserts a blank column for the gear menu + hcont << content_tag(:th, '') if links.any? + + hcont.join.html_safe + end + end + + # TODO: refactor + body = 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' or attribute == 'comment' + lnk = [] + + # #is_a? ? ; or ? + # unless item.class == Calendar or 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 or 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 + # TODO: error undefined `current_referential` + # bcont << content_tag(:td, links_builder(item, links), class: 'actions') if links.any? + + bcont.join.html_safe + end + end.join.html_safe + end + + content_tag :table, head + body, class: cls + end +end diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb new file mode 100644 index 000000000..e9d1afed3 --- /dev/null +++ b/spec/helpers/table_builder_helper_spec.rb @@ -0,0 +1,120 @@ +require 'spec_helper' + +describe TableBuilderHelper, type: :helper do + describe "#table_builder_2" do + it "builds a table" do + workbenches = [ + build_stubbed(:referential) + ] + + expected = <<-HTML +<table class="table has-filter has-search"> + <thead> + <tr> + <th> + <div class="checkbox"><input type="checkbox" name="0" id="0" value="all" /><label for="0"></label></div> + </th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=name">Nom<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=status">Etat<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=organisation">Organisation<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=validity_period">Période de validité englobante<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=lines">Lignes<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=created_at">Créé le<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=updated_at">Edité le<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th><a href="/workbenches/1?direction=desc&q%5Barchived_at_not_null%5D=1&q%5Barchived_at_null%5D=1&sort=published_at">Intégré le<span class="orderers"><span class="fa fa-sort-asc active"></span><span class="fa fa-sort-desc "></span></span></a></th> + <th></th> + </tr> + </thead> + <tbody> + <tr> + <td> + <div class="checkbox"><input type="checkbox" name="4" id="4" value="4" /><label for="4"></label></div> + </td> + <td title="Voir"><a href="/referentials/4">Referential Yanis Gaillard</a></td> + <td> + <div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>En préparation</span></div> + </td> + <td>STIF</td> + <td>01/05/2017 > 31/08/2017</td> + <td>1</td> + <td>02/05/2017</td> + <td>02/05/2017</td> + <td></td> + <td class="actions"> + <div class="btn-group"> + <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> + <ul class="dropdown-menu"> + <li><a href="/referentials/4">Consulter</a></li> + <li><a href="/referentials/4/edit">Editer</a></li> + <li><a rel="nofollow" data-method="put" href="/referentials/4/archive">Conserver</a></li> + <li class="delete-action"><a data-confirm="Etes-vous sûr(e) de vouloir effectuer cette action ?" rel="nofollow" data-method="delete" href="/referentials/4"><span class="fa fa-trash"></span>Supprimer</a></li> + </ul> + </div> + </td> + </tr> + <tr> + <td> + <div class="checkbox"><input type="checkbox" name="3" id="3" value="3" /><label for="3"></label></div> + </td> + <td title="Voir"><a href="/referentials/3">Test Referential 2017.04.25</a></td> + <td> + <div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>En préparation</span></div> + </td> + <td>STIF</td> + <td>25/04/2017 > 25/05/2022</td> + <td>6</td> + <td>25/04/2017</td> + <td>25/04/2017</td> + <td></td> + <td class="actions"> + <div class="btn-group"> + <div class="btn dropdown-toggle" data-toggle="dropdown"><span class="fa fa-cog"></span></div> + <ul class="dropdown-menu"> + <li><a href="/referentials/3">Consulter</a></li> + <li><a href="/referentials/3/edit">Editer</a></li> + <li><a rel="nofollow" data-method="put" href="/referentials/3/archive">Conserver</a></li> + <li class="delete-action"><a data-confirm="Etes-vous sûr(e) de vouloir effectuer cette action ?" rel="nofollow" data-method="delete" href="/referentials/3"><span class="fa fa-trash"></span>Supprimer</a></li> + </ul> + </div> + </td> + </tr> + </tbody> +</table> + HTML +# <div class="select_toolbox noselect"> +# <ul> +# <li class="st_action"><a data-path="/workbenches/1/referentials" data-confirm="Etes-vous sûr(e) de vouloir effectuer cette action ?" title="Supprimer" rel="nofollow" data-method="delete" href="#"><span class="fa fa-trash"></span></a></li> +# </ul><span class="info-msg"><span>0</span> élément(s) sélectionné(s)</span> +# </div> + + minified_expected = expected.gsub(/^\s+/, '').gsub("\n", '') + + expect(helper.table_builder_2( + workbenches, + { :name => 'name', + :status => Proc.new {|w| w.archived? ? ("<div class='td-block'><span class='fa fa-archive'></span><span>Conservé</span></div>").html_safe : ("<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>En préparation</span></div>").html_safe}, + :status => Proc.new {|w| ("<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>En préparation</span></div>").html_safe}, + :organisation => Proc.new {|w| w.organisation.name}, + :validity_period => Proc.new {|w| w.validity_period.nil? ? '-' : t('validity_range', debut: l(w.try(:validity_period).try(:begin), format: :short), end: l(w.try(:validity_period).try(:end), format: :short))}, + :lines => Proc.new {|w| w.lines.count}, + :created_at => Proc.new {|w| l(w.created_at, format: :short)}, + :updated_at => Proc.new {|w| l(w.updated_at, format: :short)}, + :published_at => ''}, + links: [:show, :edit, :archive, :unarchive, :delete], + cls: 'table has-filter has-search' + )).to eq(minified_expected) + end + end +end + + +# Replace table builder on workspaces#show page +# Make the builder work without a `current_referential` so we can actually test it +# Make a way to define a column as non-sortable. By default, columns will be sortable. Unless sortable==false and no columns should be sortable. +# +# +# TODO: +# - Finish writing workbench test +# - Port some code over to the new table builder +# - Ask Jean-Paul if there's anything he wishes could be changed or improved about the existing table builder +# - Thing that Jean-Paul didn't like was the link generation |
