From 9574d8b8351fd59eeada05a1ba71893a82b86989 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 14:47:29 +0200 Subject: TableBuilderHelper::Column: Add `link_to` argument This parameter will be used as the `href` to link the column value somewhere. We give it a lambda because this makes it easier to call any method on the row object. This means the accessor needs to take the object as an argument, like in the `#value` method, because we don't have a better way to handle that (it can't be done at initialisation time because at that point we don't have row objects, we have a collection). --- app/helpers/table_builder_helper/column.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb index 800a8282e..ef3c0b629 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,10 @@ module TableBuilderHelper I18n.t("activerecord.attributes.#{model_key}.#{@key}") end + + def link_to(obj) + @link_to.call(obj) + end end -- cgit v1.2.3 From 7f85e8bbb15ec4063dad8b080e61d690b77dead7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 14:51:07 +0200 Subject: TableBuilderHelper::Column: Add `#linkable?` method This tells the markup assembler whether or not this column should be wrapped in a link (``). It intends to serve the same purpose as `TableBuilderHelper#column_is_linkable`, but at the column level. The idea is to remove that method when these links only operate by column and not by pre-defined values like before/now. --- app/helpers/table_builder_helper/column.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app') diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb index ef3c0b629..b4c569882 100644 --- a/app/helpers/table_builder_helper/column.rb +++ b/app/helpers/table_builder_helper/column.rb @@ -31,6 +31,10 @@ 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 -- cgit v1.2.3 From 9470a51cfc5fb3fd4d5fde2fb4e12caf3eaaa8eb Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 15:14:45 +0200 Subject: TableBuilderHelper: Test out column-specific `#link_to` Testing out the new `TableBuilderHelper::Column#link_to` method to provide the link needed to go to the row object's #show page. Seems to work in this context, which is promising! --- app/helpers/table_builder_helper.rb | 6 ++++++ app/views/workbenches/show.html.slim | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index f15019458..61812a427 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -186,6 +186,12 @@ module TableBuilderHelper columns.each do |column| value = column.value(item) + if column.linkable? + path = column.link_to(item) + bcont << content_tag(:td, link_to(value, path), title: 'Voir') + next # TODO: temporary for testing, fix all the stuff below + end + if column_is_linkable?(column) # Build a link to the `item` polymorph_url = URL.polymorphic_url_parts( 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, \ -- cgit v1.2.3 From b546fc6ef6d406a041f5a26f5855a02b7c8d1f2c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 15:34:45 +0200 Subject: TableBuilderHelper#tbody: Use new `Column#link_to` for all column links Instead of always linking to the `name` and `comment` attributes, allow any column to link to any thing by calling the new `TableBuilderHelper::Column#link_to` method. Because `linkable?`-ness is now determined at the column level, we can get rid of the `#column_is_linkable?` method here. The code is still pretty much "en désordre" as a result of the `overhead` code tornado, but we'll have to deal with that later. (Oh goodness I hope we get to deal with it.) --- app/helpers/table_builder_helper.rb | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) (limited to 'app') diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index 61812a427..94a7a62e6 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -188,19 +188,10 @@ module TableBuilderHelper if column.linkable? path = column.link_to(item) - bcont << content_tag(:td, link_to(value, path), title: 'Voir') - next # TODO: temporary for testing, fix all the stuff below - end - - if column_is_linkable?(column) - # Build a link to the `item` - polymorph_url = URL.polymorphic_url_parts( - item, - referential - ) + 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) @@ -209,16 +200,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 @@ -340,10 +331,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, -- cgit v1.2.3 From 28db706443a912e8355e4c48488dc40c403e7f76 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 16:04:59 +0200 Subject: TableBuilderHelper::Column#link_to: Allow arbitrary number of arguments This enables us to pass multiple objects into the lambda, so that we can for example build a URL using a Rails helper using multiple objects. Example: column.link_to(referential, item) lambda do |referential, item| some_path(referential, item) end --- app/helpers/table_builder_helper/column.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb index b4c569882..5834cbc4e 100644 --- a/app/helpers/table_builder_helper/column.rb +++ b/app/helpers/table_builder_helper/column.rb @@ -35,8 +35,8 @@ module TableBuilderHelper !@link_to.nil? end - def link_to(obj) - @link_to.call(obj) + def link_to(*objs) + @link_to.call(*objs) end end -- cgit v1.2.3 From e115d2cf86c285f66af37aa8ca66d63eb4144294 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 16:47:10 +0200 Subject: table_builder_2: Use `Column#link_to` in all table builder calls Rewrite table builder calls to work with the new `TableBuilderHelper::Column#link_to` mechanism for defining the linked column and what it points to. This takes the link out of the `TableBuilderHelper`, and puts it into the hands of the view. It gives us more flexibility with regards to what can be defined as a link in our tables and what those links point to. This will be used in a more meaningful way in the `imports#show` page to list `NetexImport`s and provide links to both the referential associated with a Netex import and the imported file status page from a table row. --- app/views/api_keys/index.html.slim | 5 ++++- app/views/calendars/index.html.slim | 5 ++++- app/views/companies/index.html.slim | 5 ++++- app/views/imports/index.html.slim | 5 ++++- app/views/lines/index.html.slim | 5 ++++- app/views/networks/index.html.slim | 5 ++++- app/views/referential_companies/index.html.slim | 5 ++++- app/views/referential_lines/show.html.slim | 5 ++++- app/views/referential_networks/index.html.slim | 5 ++++- app/views/referentials/show.html.slim | 5 ++++- app/views/routes/show.html.slim | 5 ++++- app/views/routing_constraint_zones/index.html.slim | 9 ++++++++- app/views/routing_constraint_zones/show.html.slim | 5 ++++- app/views/stop_areas/index.html.slim | 8 +++++++- app/views/time_tables/index.html.slim | 5 ++++- 15 files changed, 67 insertions(+), 15 deletions(-) (limited to 'app') 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", \ -- cgit v1.2.3 From df64a6dcbc38cefe41c74bcb1d2d166d13711b23 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 16:51:02 +0200 Subject: Revert "TableBuilderHelper::Column#link_to: Allow arbitrary number of arguments" This reverts commit 28db706443a912e8355e4c48488dc40c403e7f76. Turns out we didn't need to be able to pass an arbitrary number of arguments to the lambda after all. The URL helper objects necessary in addition to the first argument to the lambda can be retrieved from the view context directly instead of passing them into the block as parameters (which would actually make things more difficult, because the block is called in the `TableBuilderHelper`, thus outside the scope of the view). --- app/helpers/table_builder_helper/column.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb index 5834cbc4e..b4c569882 100644 --- a/app/helpers/table_builder_helper/column.rb +++ b/app/helpers/table_builder_helper/column.rb @@ -35,8 +35,8 @@ module TableBuilderHelper !@link_to.nil? end - def link_to(*objs) - @link_to.call(*objs) + def link_to(obj) + @link_to.call(obj) end end -- cgit v1.2.3 From e278efb2eb66cd5bbe9072a81b86efe17dfbd251 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 18:06:22 +0200 Subject: TableBuilderHelper: Add `link_to` to header documentation Add the new `link_to` argument to the example to show how it's used. --- app/helpers/table_builder_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index 94a7a62e6..df37cf55f 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, -- cgit v1.2.3 From 44aeaeadfaf78ca1c43a6e182aaa67648f7e45f7 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 1 Sep 2017 18:08:58 +0200 Subject: TableBuilderHelper: Fix formatting of doc example --- app/helpers/table_builder_helper.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index df37cf55f..ec4d487c1 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -46,7 +46,18 @@ require 'table_builder_helper/url' # ], # links: [:show, :edit], # cls: 'table has-search', -# overhead: [ {title: 'one', width: 1, cls: 'toto'}, {title: 'two Info', width: 2, cls: 'default'} ] +# overhead: [ +# { +# title: 'one', +# width: 1, +# cls: 'toto' +# }, +# { +# title: 'two Info', +# width: 2, +# cls: 'default' +# } +# ] # ) module TableBuilderHelper # TODO: rename this after migration from `table_builder` -- cgit v1.2.3