From 734de24950c6ae490d4781799c8a6d805944e6aa Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 14 Jun 2017 11:14:16 +0200 Subject: Workbenches#show: Use common referential action_links in table gear menu Get our `ReferentialDecorator` working for a collection of `Referential`s. We decorate the `ActiveRecord::Relation` of referentials that gets passed to the table builder. This enables us to call the `#action_links` method on those referentials, getting a list of links that should appear in the gear menu. From that list, we can generate the proper HTML to stick into the menu. This was my first test of reusing the `#action_links` for both the header buttons in Referentials#show and the list in Workbenches#show. Ran into some trouble. The issue was that decorating the collection with Draper's defaults doesn't delegate methods on the collection object (in other words, `ActiveRecord::Relation`). We call methods on the collection in our code, and doing so on our new decorated collection caused errors. As a first step to get around the first batch of errors, I changed the `_filters.html.slim` template to call `#human_attribute_name` on `Referential` model directly, instead of on the Relation. There was some additional weirdness that Luc tipped me off to as we have a `Chouette::ActiveRecord` class that defines its own `#human_attribute_name` method. Switching the callee to the model fixed this error. Subsequently, I ran into a problem in the `TableBuilderHelper`. In that code, we call `collection.model` to determine the class of objects in our table. Now that I think about it, maybe we should just pass this information when calling `table_builder`. Let's do that because it would be better. Anyway, to enable that `ActiveRecord::Relation#model` method to be called, I created a Draper `CollectionDecorator` that delegates the `#model` method. This allows us to call `#model` on the decorated collection of referentials (or other objects). Finally, I ran into another delegate problem for our pagination method calls. Fortunately, Draper provides some code for a `CollectionDecorator` for pagination. I copied this and used it in conjunction with my custom `ModelDecorator` to properly delegate all necessary collection methods for a collection of Referentials. Stuck my `PaginatingDecorator` and `ModelDecorator` right in the `referential_decorator.rb` file for now just for testing until I got things working. Needed to import this file in the controller where we decorate the collection. Not entirely sure why but my guess is that it's because the loader connects classes to file names and we have several different classes in that file. Maybe I'm totally wrong on that. Anyway, will be moving those classes to separate files later. In the controller, decorate our `Referential` collection with the `ModelDecorator` (which inherits from `PaginatingDecorator`, giving us those delegated methods), and ensure the objects inside the collection get decorated with our original `ReferentialDecorator`. In `TableBuilderHelper#build_links`, comment out all the existing link building code and instead map over the `#action_links` provided by the decorator. Currently this should only work for `Referential` collections, but the idea is to use that pattern for all table objects. NOTE: We should add a doc comment to the table builder that tells people that they need a decorator for their model in order for it to work. Refs #3479 --- app/controllers/workbenches_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'app/controllers/workbenches_controller.rb') diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index ccd55965b..56f9136b3 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -1,3 +1,6 @@ +# TODO: Try not importing +require 'referential_decorator' + class WorkbenchesController < BreadcrumbController before_action :query_params, only: [:show] @@ -14,6 +17,10 @@ class WorkbenchesController < BreadcrumbController q_for_result = scope.ransack(params[:q].merge(archived_at_not_null: nil, archived_at_null: nil)) @wbench_refs = sort_result(q_for_result.result).paginate(page: params[:page], per_page: 30) + @wbench_refs = ModelDecorator.decorate( + @wbench_refs, + with: ReferentialDecorator + ) @q = scope.ransack(params[:q]) show! do -- cgit v1.2.3 From fc74a500d9c672d855d199eee715efd398058527 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 14 Jun 2017 19:03:02 +0200 Subject: WorkbenchesController: Remove `referential_decorator` import This is no longer necessary. I had put it in because I was getting an error, but it turns out that error was probably a result of me redefining `PaginatingDecorator` inside "referential_decorator.rb". Now that we're using the `PaginatingDecorator` that Jean-Paul created originally, we no longer have a problem. Refs #3479 --- app/controllers/workbenches_controller.rb | 3 --- 1 file changed, 3 deletions(-) (limited to 'app/controllers/workbenches_controller.rb') diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index 56f9136b3..1447c27de 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -1,6 +1,3 @@ -# TODO: Try not importing -require 'referential_decorator' - class WorkbenchesController < BreadcrumbController before_action :query_params, only: [:show] -- cgit v1.2.3 From e74ee2e805b78666caa78b50df708cf25d3150ea Mon Sep 17 00:00:00 2001 From: Xinhui Date: Fri, 23 Jun 2017 11:59:19 +0200 Subject: Fix workbench#show filter by associated lines with sorting refs #3826 --- app/controllers/workbenches_controller.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'app/controllers/workbenches_controller.rb') diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index 1447c27de..b178a7fe2 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -11,8 +11,12 @@ class WorkbenchesController < BreadcrumbController scope = ransack_status(scope) # Ignore archived_at_not_null/archived_at_null managed by ransack_status scope - q_for_result = - scope.ransack(params[:q].merge(archived_at_not_null: nil, archived_at_null: nil)) + # We clone params[:q] so we can delete fake ransack filter arguments before calling search method, + # which will allow us to preserve params[:q] for sorting + ransack_params = params[:q].merge(archived_at_not_null: nil, archived_at_null: nil).clone + ransack_params.delete('associated_lines_id_eq') + + q_for_result = scope.ransack(ransack_params) @wbench_refs = sort_result(q_for_result.result).paginate(page: params[:page], per_page: 30) @wbench_refs = ModelDecorator.decorate( @wbench_refs, @@ -63,7 +67,6 @@ class WorkbenchesController < BreadcrumbController def ransack_associated_lines scope if params[:q] && params[:q]['associated_lines_id_eq'] scope = scope.include_metadatas_lines([params[:q]['associated_lines_id_eq']]) - params[:q].delete('associated_lines_id_eq') end scope end -- cgit v1.2.3 From 8e7eb2e722ad4615f4b34253dd2fcaa6226887a9 Mon Sep 17 00:00:00 2001 From: Xinhui Date: Fri, 23 Jun 2017 17:31:21 +0200 Subject: Fix workbench#show sort with period filter Refs #3826 --- app/controllers/workbenches_controller.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'app/controllers/workbenches_controller.rb') diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index b178a7fe2..83762d83f 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -16,14 +16,13 @@ class WorkbenchesController < BreadcrumbController ransack_params = params[:q].merge(archived_at_not_null: nil, archived_at_null: nil).clone ransack_params.delete('associated_lines_id_eq') - q_for_result = scope.ransack(ransack_params) - @wbench_refs = sort_result(q_for_result.result).paginate(page: params[:page], per_page: 30) + @q = scope.ransack(ransack_params) + @wbench_refs = sort_result(@q.result).paginate(page: params[:page], per_page: 30) @wbench_refs = ModelDecorator.decorate( @wbench_refs, with: ReferentialDecorator ) - @q = scope.ransack(params[:q]) show! do build_breadcrumb :show end -- cgit v1.2.3 From b260c832c8e129dbeacfe065d01bd0732dd80701 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 28 Jun 2017 17:19:50 +0200 Subject: Hotfix fixes: #3793@1h --- app/controllers/workbenches_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/controllers/workbenches_controller.rb') diff --git a/app/controllers/workbenches_controller.rb b/app/controllers/workbenches_controller.rb index 83762d83f..d03841356 100644 --- a/app/controllers/workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -40,7 +40,7 @@ class WorkbenchesController < BreadcrumbController private def resource - @workbench = Workbench.find params[:id] + @workbench = current_organisation.workbenches.find params[:id] end def sort_result collection -- cgit v1.2.3