aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/helpers/table_builder_helper.rb23
-rw-r--r--app/helpers/table_builder_helper/column.rb9
-rw-r--r--spec/helpers/table_builder_helper/column_spec.rb23
-rw-r--r--spec/helpers/table_builder_helper_spec.rb108
4 files changed, 152 insertions, 11 deletions
diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb
index 5f0265e89..4537a0795 100644
--- a/app/helpers/table_builder_helper.rb
+++ b/app/helpers/table_builder_helper.rb
@@ -10,8 +10,10 @@ module TableBuilderHelper
collection,
columns,
current_referential: nil,
- # TODO: use this when building column headers
+
+ # 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
@@ -26,14 +28,14 @@ module TableBuilderHelper
)
content_tag :table,
- thead(collection, columns, selectable, links.any?) +
+ thead(collection, columns, sortable, selectable, links.any?) +
tbody(collection, columns, selectable, links),
class: cls
end
private
- def thead(collection, columns, selectable, has_links)
+ def thead(collection, columns, sortable, selectable, has_links)
content_tag :thead do
content_tag :tr do
hcont = []
@@ -45,6 +47,7 @@ module TableBuilderHelper
columns.each do |column|
hcont << content_tag(:th, build_column_header(
column,
+ sortable,
collection.model,
params,
params[:sort],
@@ -179,11 +182,16 @@ module TableBuilderHelper
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 =
@@ -208,19 +216,12 @@ module TableBuilderHelper
arrow_icons = content_tag :span, arrow_up + arrow_down, class: 'orderers'
(
- column_header_label(collection_model, column.key) +
+ column.header_label(collection_model) +
arrow_icons
).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
-
def checkbox(id_name:, value:)
content_tag :div, '', class: 'checkbox' do
check_box_tag(id_name, value).concat(
diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb
index 54e63104d..b2fdf2b73 100644
--- a/app/helpers/table_builder_helper/column.rb
+++ b/app/helpers/table_builder_helper/column.rb
@@ -20,6 +20,15 @@ module TableBuilderHelper
obj.try(@attribute)
end
end
+
+ def header_label(model = nil)
+ return @name unless name.empty?
+
+ # Transform `Chouette::Line` into "line"
+ model_key = model.to_s.demodulize.underscore
+
+ I18n.t("activerecord.attributes.#{model_key}.#{@key}")
+ end
end
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