diff options
| author | teddywing | 2017-09-01 18:25:05 +0200 |
|---|---|---|
| committer | GitHub | 2017-09-01 18:25:05 +0200 |
| commit | ed64dc517bca5f775631d999aa2e60f78d4dae30 (patch) | |
| tree | 0acfa761cbbd39d9ec67a7e9e56eccb86950e1b5 | |
| parent | d127ed12158550f84ef1fb9c21d6360c86ac3642 (diff) | |
| parent | 44aeaeadfaf78ca1c43a6e182aaa67648f7e45f7 (diff) | |
| download | chouette-core-ed64dc517bca5f775631d999aa2e60f78d4dae30.tar.bz2 | |
Merge pull request #60 from af83/table-builder--allow-column-links-to-be-customised
Table builder allow column links to be customised
20 files changed, 155 insertions, 36 deletions
diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index f15019458..ec4d487c1 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -26,7 +26,10 @@ require 'table_builder_helper/url' # ), # TableBuilderHelper::Column.new( # key: :name, -# attribute: 'name' +# attribute: 'name', +# link_to: lambda do |company| +# referential_company_path(@referential, company) +# end # ), # TableBuilderHelper::Column.new( # key: :phone, @@ -43,7 +46,18 @@ require 'table_builder_helper/url' # ], # links: [:show, :edit], # cls: 'table has-search', -# overhead: [ {title: 'one', width: 1, cls: 'toto'}, {title: 'two <span class="test">Info</span>', width: 2, cls: 'default'} ] +# overhead: [ +# { +# title: 'one', +# width: 1, +# cls: 'toto' +# }, +# { +# title: 'two <span class="test">Info</span>', +# width: 2, +# cls: 'default' +# } +# ] # ) module TableBuilderHelper # TODO: rename this after migration from `table_builder` @@ -186,15 +200,12 @@ module TableBuilderHelper 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, - referential - ) + if column.linkable? + path = column.link_to(item) + link = link_to(value, path) if overhead.empty? - bcont << content_tag(:td, link_to(value, polymorph_url), title: 'Voir') + bcont << content_tag(:td, link, title: 'Voir') else i = columns.index(column) @@ -203,16 +214,16 @@ module TableBuilderHelper if (i > 0) && (overhead[i - 1][:width] > 1) clsArrayAlt = overhead[i - 1][:cls].split - bcont << content_tag(:td, link_to(value, polymorph_url), title: 'Voir', class: td_cls(clsArrayAlt)) + bcont << content_tag(:td, link, title: 'Voir', class: td_cls(clsArrayAlt)) else - bcont << content_tag(:td, link_to(value, polymorph_url), title: 'Voir') + bcont << content_tag(:td, link, title: 'Voir') end else clsArray = overhead[columns.index(column)][:cls].split - bcont << content_tag(:td, link_to(value, polymorph_url), title: 'Voir', class: td_cls(clsArray)) + bcont << content_tag(:td, link, title: 'Voir', class: td_cls(clsArray)) end end @@ -334,10 +345,6 @@ module TableBuilderHelper end end - def column_is_linkable?(column) - column.attribute == 'name' || column.attribute == 'comment' - end - def gear_menu_link(link) content_tag( :li, diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb index 800a8282e..b4c569882 100644 --- a/app/helpers/table_builder_helper/column.rb +++ b/app/helpers/table_builder_helper/column.rb @@ -2,7 +2,7 @@ module TableBuilderHelper class Column attr_reader :key, :name, :attribute, :sortable - def initialize(key: nil, name: '', attribute:, sortable: true) + def initialize(key: nil, name: '', attribute:, sortable: true, link_to: nil) if key.nil? && name.empty? raise ColumnMustHaveKeyOrNameError end @@ -11,6 +11,7 @@ module TableBuilderHelper @name = name @attribute = attribute @sortable = sortable + @link_to = link_to end def value(obj) @@ -29,6 +30,14 @@ module TableBuilderHelper I18n.t("activerecord.attributes.#{model_key}.#{@key}") end + + def linkable? + !@link_to.nil? + end + + def link_to(obj) + @link_to.call(obj) + end end diff --git a/app/views/api_keys/index.html.slim b/app/views/api_keys/index.html.slim index fc8d95c7a..9757b8955 100644 --- a/app/views/api_keys/index.html.slim +++ b/app/views/api_keys/index.html.slim @@ -12,7 +12,10 @@ [ \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |api_key| \ + organisation_api_key_path(api_key) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :token, \ diff --git a/app/views/calendars/index.html.slim b/app/views/calendars/index.html.slim index 757ade89b..1e38786b9 100644 --- a/app/views/calendars/index.html.slim +++ b/app/views/calendars/index.html.slim @@ -21,7 +21,10 @@ [ \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |calendar| \ + calendar_path(calendar) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :short_name, \ diff --git a/app/views/companies/index.html.slim b/app/views/companies/index.html.slim index 90d5e8c96..dad905c60 100644 --- a/app/views/companies/index.html.slim +++ b/app/views/companies/index.html.slim @@ -31,7 +31,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |company| \ + line_referential_company_path(current_referential, company) \ + end \ ) \ ], links: [:show, :edit], diff --git a/app/views/imports/index.html.slim b/app/views/imports/index.html.slim index 98a0f0108..6f76414a6 100644 --- a/app/views/imports/index.html.slim +++ b/app/views/imports/index.html.slim @@ -28,7 +28,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |import| \ + workbench_import_path(@workbench, import) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :creator, \ diff --git a/app/views/lines/index.html.slim b/app/views/lines/index.html.slim index e4a29b182..dda5afd44 100644 --- a/app/views/lines/index.html.slim +++ b/app/views/lines/index.html.slim @@ -29,7 +29,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |line| \ + line_referential_line_path(@line_referential, line) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :deactivated, \ diff --git a/app/views/networks/index.html.slim b/app/views/networks/index.html.slim index bd1f3d15a..235bdebda 100644 --- a/app/views/networks/index.html.slim +++ b/app/views/networks/index.html.slim @@ -31,7 +31,10 @@ ), TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |network| \ + line_referential_network_path(@line_referential, network) \ + end \ ), \ ], links: [:show], diff --git a/app/views/referential_companies/index.html.slim b/app/views/referential_companies/index.html.slim index 1946bbab5..e5b7ce24a 100644 --- a/app/views/referential_companies/index.html.slim +++ b/app/views/referential_companies/index.html.slim @@ -31,7 +31,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |company| \ + referential_company_path(@referential, company) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :phone, \ diff --git a/app/views/referential_lines/show.html.slim b/app/views/referential_lines/show.html.slim index f20b59d3e..cbce7a7d5 100644 --- a/app/views/referential_lines/show.html.slim +++ b/app/views/referential_lines/show.html.slim @@ -51,7 +51,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |route| \ + referential_line_route_path(@referential, @line, route) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :published_name, \ diff --git a/app/views/referential_networks/index.html.slim b/app/views/referential_networks/index.html.slim index 747143c94..ca67eca8b 100644 --- a/app/views/referential_networks/index.html.slim +++ b/app/views/referential_networks/index.html.slim @@ -31,7 +31,10 @@ ), TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |network| \ + referential_network_path(@referential, network) \ + end \ ) \ ], links: [:show], diff --git a/app/views/referentials/show.html.slim b/app/views/referentials/show.html.slim index 5229c3d7d..e1d89cba4 100644 --- a/app/views/referentials/show.html.slim +++ b/app/views/referentials/show.html.slim @@ -51,7 +51,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |line| \ + referential_line_path(@referential, line) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :deactivated, \ diff --git a/app/views/routes/show.html.slim b/app/views/routes/show.html.slim index 1e3fac723..a21b5ec8a 100644 --- a/app/views/routes/show.html.slim +++ b/app/views/routes/show.html.slim @@ -41,7 +41,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: Proc.new {|s| s.try(:stop_area).try(:name)} \ + attribute: Proc.new {|s| s.try(:stop_area).try(:name)}, \ + link_to: lambda do |stop_point| \ + referential_stop_area_path(@referential, stop_point.stop_area) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :deleted_at, \ diff --git a/app/views/routing_constraint_zones/index.html.slim b/app/views/routing_constraint_zones/index.html.slim index d7e15a2da..4e2534b6a 100644 --- a/app/views/routing_constraint_zones/index.html.slim +++ b/app/views/routing_constraint_zones/index.html.slim @@ -25,7 +25,14 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |routing_constraint_zone| \ + referential_line_routing_constraint_zone_path( \ + @referential, \ + @line, \ + routing_constraint_zone \ + ) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :stop_points_count, \ diff --git a/app/views/routing_constraint_zones/show.html.slim b/app/views/routing_constraint_zones/show.html.slim index b5aa199c6..dbd8464a0 100644 --- a/app/views/routing_constraint_zones/show.html.slim +++ b/app/views/routing_constraint_zones/show.html.slim @@ -30,7 +30,10 @@ [ \ TableBuilderHelper::Column.new( \ name: "Arrêts de l'itinéraire", \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |stop_point| \ + referential_stop_area_path(@referential, stop_point.stop_area) \ + end \ ), TableBuilderHelper::Column.new( \ name: "Arrêts inclus dans l'ITL", \ diff --git a/app/views/stop_areas/index.html.slim b/app/views/stop_areas/index.html.slim index 795c773ec..4c95761d2 100644 --- a/app/views/stop_areas/index.html.slim +++ b/app/views/stop_areas/index.html.slim @@ -25,7 +25,13 @@ ), \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |stop_area| \ + referential_stop_area_path( \ + @stop_area_referential, \ + stop_area \ + ) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :registration_number, \ diff --git a/app/views/time_tables/index.html.slim b/app/views/time_tables/index.html.slim index 05be460d4..1ea5256aa 100644 --- a/app/views/time_tables/index.html.slim +++ b/app/views/time_tables/index.html.slim @@ -27,7 +27,10 @@ ), \ TableBuilderHelper::Column.new( \ key: :comment, \ - attribute: 'comment' \ + attribute: 'comment', \ + link_to: lambda do |time_table| \ + referential_time_table_path(@referential, time_table) \ + end \ ), \ TableBuilderHelper::Column.new( \ name: "Période englobante", \ diff --git a/app/views/workbenches/show.html.slim b/app/views/workbenches/show.html.slim index 8d924b360..bb54f07cb 100644 --- a/app/views/workbenches/show.html.slim +++ b/app/views/workbenches/show.html.slim @@ -27,7 +27,10 @@ [ \ TableBuilderHelper::Column.new( \ key: :name, \ - attribute: 'name' \ + attribute: 'name', \ + link_to: lambda do |referential| \ + referential_path(referential) \ + end \ ), \ TableBuilderHelper::Column.new( \ key: :status, \ diff --git a/spec/helpers/table_builder_helper/column_spec.rb b/spec/helpers/table_builder_helper/column_spec.rb index 0f27703b2..e0bfd8a6a 100644 --- a/spec/helpers/table_builder_helper/column_spec.rb +++ b/spec/helpers/table_builder_helper/column_spec.rb @@ -20,4 +20,43 @@ describe TableBuilderHelper::Column do ).to eq('Numéro de téléphone') end end + + describe "#linkable?" do + it "returns true if :link_to is not nil" do + expect( + TableBuilderHelper::Column.new( + name: 'unused', + attribute: nil, + link_to: lambda do + train.kind + end + ).linkable? + ).to be true + end + + it "returns false if :link_to is nil" do + expect( + TableBuilderHelper::Column.new( + name: 'unused', + attribute: nil + ).linkable? + ).to be false + end + end + + describe "#link_to" do + it "calls the block passed in and returns the result" do + train = double('train', kind: 'TGV') + + expect( + TableBuilderHelper::Column.new( + name: 'unused', + attribute: nil, + link_to: lambda do |train| + train.kind + end + ).link_to(train) + ).to eq('TGV') + end + end end diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index c536a4c62..e17196a19 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -96,7 +96,10 @@ describe TableBuilderHelper, type: :helper do [ TableBuilderHelper::Column.new( key: :name, - attribute: 'name' + attribute: 'name', + link_to: lambda do |referential| + referential_path(referential) + end ), TableBuilderHelper::Column.new( key: :status, @@ -238,7 +241,10 @@ describe TableBuilderHelper, type: :helper do ), TableBuilderHelper::Column.new( key: :name, - attribute: 'name' + attribute: 'name', + link_to: lambda do |company| + referential_company_path(referential, company) + end ), TableBuilderHelper::Column.new( key: :phone, @@ -347,7 +353,10 @@ describe TableBuilderHelper, type: :helper do ), TableBuilderHelper::Column.new( key: :name, - attribute: 'name' + attribute: 'name', + link_to: lambda do |company| + referential_company_path(referential, company) + end ), TableBuilderHelper::Column.new( key: :phone, |
