aboutsummaryrefslogtreecommitdiffstats
path: root/spec/helpers
diff options
context:
space:
mode:
authorTeddy Wing2017-06-15 12:33:57 +0200
committerTeddy Wing2017-06-15 13:52:31 +0200
commitd79a0db0301d14e0ab4e5ecb2d8b60560f4490a9 (patch)
tree9749f6cdde5431a013028ce8e9e1944ddf6febb0 /spec/helpers
parent34ee3f25dd9e903e27bbbbcdf85ae2803a869c76 (diff)
downloadchouette-core-d79a0db0301d14e0ab4e5ecb2d8b60560f4490a9.tar.bz2
TableBuilder: Make table-level `sortable` work
We need the ability to say that an entire table is not sortable. By default, all tables are sortable, and individual columns can have sorting deactivated. This blanket deactivates sorting for all columns. Take our `sortable` argument and pass it to `#thead`. Then `#build_column_header` takes that value into account when creating a header column value. The actual label used in the column header is determined by the old `#column_header_label` method. We've now moved it into the `TableBuilderHelper::Column` class because it makes more sense there. The method will now return the column's `:name` property if it was defined, and otherwise gets the translation for `:key` as before. Add a test on `TableBuilderHelper` that verifies that a table with `sortable: false` returns the correct HTML without sorting links. Refs #3479
Diffstat (limited to 'spec/helpers')
-rw-r--r--spec/helpers/table_builder_helper/column_spec.rb23
-rw-r--r--spec/helpers/table_builder_helper_spec.rb108
2 files changed, 131 insertions, 0 deletions
diff --git a/spec/helpers/table_builder_helper/column_spec.rb b/spec/helpers/table_builder_helper/column_spec.rb
new file mode 100644
index 000000000..0f27703b2
--- /dev/null
+++ b/spec/helpers/table_builder_helper/column_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe TableBuilderHelper::Column do
+ describe "#header_label" do
+ it "returns the column @name if present" do
+ expect(
+ TableBuilderHelper::Column.new(
+ name: 'ID Codif',
+ attribute: nil
+ ).header_label
+ ).to eq('ID Codif')
+ end
+
+ it "returns the I18n translation of @key if @name not present" do
+ expect(
+ TableBuilderHelper::Column.new(
+ key: :phone,
+ attribute: 'phone'
+ ).header_label(Chouette::Company)
+ ).to eq('Numéro de téléphone')
+ end
+ end
+end
diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb
index 89660dd56..c006e33c0 100644
--- a/spec/helpers/table_builder_helper_spec.rb
+++ b/spec/helpers/table_builder_helper_spec.rb
@@ -270,6 +270,114 @@ describe TableBuilderHelper, type: :helper do
expect(beautified_html).to eq(expected.chomp)
end
+
+ it "can set all columns as non-sortable" do
+ company = build_stubbed(:company)
+ line_referential = build_stubbed(
+ :line_referential,
+ companies: [company]
+ )
+ referential = build_stubbed(
+ :referential,
+ line_referential: line_referential
+ )
+
+ user_context = OpenStruct.new(
+ user: build_stubbed(
+ :user,
+ organisation: referential.organisation,
+ permissions: [
+ 'referentials.create',
+ 'referentials.edit',
+ 'referentials.destroy'
+ ]
+ ),
+ context: { referential: referential }
+ )
+ allow(helper).to receive(:current_user).and_return(user_context)
+ allow(TableBuilderHelper::URL).to receive(:current_referential)
+ .and_return(referential)
+
+ companies = [company]
+
+ allow(companies).to receive(:model).and_return(Chouette::Company)
+
+ allow(helper).to receive(:params).and_return({
+ controller: 'referential_companies',
+ action: 'index',
+ referential_id: referential.id
+ })
+
+ companies = ModelDecorator.decorate(
+ companies,
+ with: CompanyDecorator
+ )
+
+ expected = <<-HTML
+<table class="table has-search">
+ <thead>
+ <tr>
+ <th>ID Codif</th>
+ <th>Nom</th>
+ <th>Numéro de téléphone</th>
+ <th>Email</th>
+ <th>Page web associée</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>#{company.objectid.local_id}</td>
+ <td title="Voir"><a href="/referentials/#{referential.id}/companies/#{company.id}">#{company.name}</a></td>
+ <td></td>
+ <td></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/#{referential.id}/companies/#{company.id}">Consulter</a></li>
+ </ul>
+ </div>
+ </td>
+ </tr>
+ </tbody>
+</table>
+ HTML
+
+ html_str = helper.table_builder_2(
+ companies,
+ [
+ TableBuilderHelper::Column.new(
+ name: 'ID Codif',
+ attribute: Proc.new { |n| n.try(:objectid).try(:local_id) }
+ ),
+ TableBuilderHelper::Column.new(
+ key: :name,
+ attribute: 'name'
+ ),
+ TableBuilderHelper::Column.new(
+ key: :phone,
+ attribute: 'phone'
+ ),
+ TableBuilderHelper::Column.new(
+ key: :email,
+ attribute: 'email'
+ ),
+ TableBuilderHelper::Column.new(
+ key: :url,
+ attribute: 'url'
+ ),
+ ],
+ sortable: false,
+ links: [:show, :edit, :delete],
+ cls: 'table has-search'
+ )
+
+ beautified_html = HtmlBeautifier.beautify(html_str, indent: ' ')
+
+ expect(beautified_html).to eq(expected.chomp)
+ end
end
end