From 079db3bd8aae52e06fd3b79cdb5521eeddc600d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=B6hmer?= Date: Tue, 15 Nov 2022 17:52:54 +0100 Subject: [PATCH 1/5] Unify code of RS::DishIngredient, RS::RecipeIngredient in role --- .../Schema/Component/ResultSet/Ingredient.pm | 19 +++++++++++++++++++ .../Schema/ResultSet/DishIngredient.pm | 13 ++++--------- .../Schema/ResultSet/RecipeIngredient.pm | 10 ++++------ 3 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 lib/Coocook/Schema/Component/ResultSet/Ingredient.pm diff --git a/lib/Coocook/Schema/Component/ResultSet/Ingredient.pm b/lib/Coocook/Schema/Component/ResultSet/Ingredient.pm new file mode 100644 index 000000000..961198195 --- /dev/null +++ b/lib/Coocook/Schema/Component/ResultSet/Ingredient.pm @@ -0,0 +1,19 @@ +package Coocook::Schema::Component::ResultSet::Ingredient; + +# ABSTRACT: common methods for ResultSet::DishIngredient and ResultSet::RecipeIngredient + +use strict; +use warnings; + +use feature 'fc'; # Perl v5.16 + +=head1 METHODS + +=cut + +sub sorted_by_columns { 'position' } + +sub prepared { shift->search( { -bool => 'prepare' } ) } +sub not_prepared { shift->search( { -not_bool => 'prepare' } ) } + +1; diff --git a/lib/Coocook/Schema/ResultSet/DishIngredient.pm b/lib/Coocook/Schema/ResultSet/DishIngredient.pm index 67fa751b3..e9d27db01 100644 --- a/lib/Coocook/Schema/ResultSet/DishIngredient.pm +++ b/lib/Coocook/Schema/ResultSet/DishIngredient.pm @@ -5,18 +5,13 @@ use namespace::autoclean; extends 'Coocook::Schema::ResultSet'; -sub sorted_by_columns { 'position' } - -__PACKAGE__->load_components('+Coocook::Schema::Component::ResultSet::SortByName'); +__PACKAGE__->load_components( + '+Coocook::Schema::Component::ResultSet::Ingredient', + '+Coocook::Schema::Component::ResultSet::SortByName', +); __PACKAGE__->meta->make_immutable; -sub prepared { - my $self = shift; - - return $self->search( { -bool => $self->me('prepare') } ); -} - sub unassigned { my $self = shift; diff --git a/lib/Coocook/Schema/ResultSet/RecipeIngredient.pm b/lib/Coocook/Schema/ResultSet/RecipeIngredient.pm index abe1c2596..06098128c 100644 --- a/lib/Coocook/Schema/ResultSet/RecipeIngredient.pm +++ b/lib/Coocook/Schema/ResultSet/RecipeIngredient.pm @@ -5,13 +5,11 @@ use namespace::autoclean; extends 'Coocook::Schema::ResultSet'; -sub sorted_by_columns { 'position' } - -__PACKAGE__->load_components('+Coocook::Schema::Component::ResultSet::SortByName'); +__PACKAGE__->load_components( + '+Coocook::Schema::Component::ResultSet::Ingredient', + '+Coocook::Schema::Component::ResultSet::SortByName', +); __PACKAGE__->meta->make_immutable; -sub prepared { shift->search( { -bool => 'prepare' } ) } -sub not_prepared { shift->search( { -not_bool => 'prepare' } ) } - 1; -- GitLab From aa1881d7e3da54a13879e48ffb6427e1e871a3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=B6hmer?= Date: Tue, 15 Nov 2022 19:47:06 +0100 Subject: [PATCH 2/5] Add share icon to button with share link --- root/templates/recipe/edit.tt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/root/templates/recipe/edit.tt b/root/templates/recipe/edit.tt index 0c3665bc9..36d7a43da 100644 --- a/root/templates/recipe/edit.tt +++ b/root/templates/recipe/edit.tt @@ -14,7 +14,9 @@ IF import_url %]
[ [% IF public_url %] - + [% END; IF import_url %] -- GitLab From 8e76ff235bfe4f672d20436df51ffdbfcae3124c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=B6hmer?= Date: Tue, 15 Nov 2022 20:26:46 +0100 Subject: [PATCH 3/5] Add button to create a recipe from a dish Implements #179. --- lib/Coocook/Controller/Dish.pm | 5 +- lib/Coocook/Controller/Recipe.pm | 16 ++++++ .../Schema/Component/ResultSet/Ingredient.pm | 16 ++++++ lib/Coocook/Schema/ResultSet/Dish.pm | 14 +++--- lib/Coocook/Schema/ResultSet/Recipe.pm | 29 +++++++++++ root/templates/dish/edit.tt | 23 +++++---- t/schema_Recipe.t | 50 +++++++++++++++++++ 7 files changed, 134 insertions(+), 19 deletions(-) diff --git a/lib/Coocook/Controller/Dish.pm b/lib/Coocook/Controller/Dish.pm index 0caf15e63..8ad4b0409 100644 --- a/lib/Coocook/Controller/Dish.pm +++ b/lib/Coocook/Controller/Dish.pm @@ -88,8 +88,9 @@ sub edit : GET HEAD Chained('base') PathPart('') Args(0) RequiresCapability('vie articles => $ingredients->all_articles, units => $ingredients->all_units, prepare_meals => [ $prepare_meals->all ], - add_ingredient_url => $c->project_uri( '/dish/add', $dish->id ), - delete_url => $c->project_uri( '/dish/delete', $dish->id ), + add_ingredient_url => $c->project_uri( '/dish/add', $dish->id ), + delete_url => $c->project_uri( '/dish/delete', $dish->id ), + create_recipe_url => $c->project_uri( '/recipe/from_dish', $dish->id ), ); for my $ingredient ( @{ $c->stash->{ingredients} } ) { diff --git a/lib/Coocook/Controller/Recipe.pm b/lib/Coocook/Controller/Recipe.pm index 5a04e5ffc..690162944 100644 --- a/lib/Coocook/Controller/Recipe.pm +++ b/lib/Coocook/Controller/Recipe.pm @@ -170,6 +170,22 @@ sub create : POST Chained('submenu') Args(0) RequiresCapability('edit_project') } +sub from_dish : POST Chained('/project/base') PathPart('recipes/from_dish') Args(1) + RequiresCapability('edit_project') { + my ( $self, $c, $dish_id ) = @_; + + my $dish = $c->project->dishes->find($dish_id); + + if ( $c->project->find_related( recipes => { name => $dish->name } ) ) { + $c->messages->error("Recipe with that name does already exist"); + $c->detach( '/dish/redirect' => [ $dish->id ] ); + } + + my $recipe = $c->model('DB::Recipe')->from_dish($dish); + + $c->response->redirect( $c->project_uri( '/recipe/edit', $recipe->id ) ); +} + sub duplicate : POST Chained('base') Args(0) RequiresCapability('edit_project') { my ( $self, $c ) = @_; diff --git a/lib/Coocook/Schema/Component/ResultSet/Ingredient.pm b/lib/Coocook/Schema/Component/ResultSet/Ingredient.pm index 961198195..5534d8ae7 100644 --- a/lib/Coocook/Schema/Component/ResultSet/Ingredient.pm +++ b/lib/Coocook/Schema/Component/ResultSet/Ingredient.pm @@ -9,8 +9,24 @@ use feature 'fc'; # Perl v5.16 =head1 METHODS +=head2 copy_from_rs($resultset) + +Copy ingredients from another dish or recipe. +Can copy from dish to recipe or vice versa. + =cut +sub copy_from_rs { + my ( $self, $rs ) = @_; + + my @columns = qw( position prepare article_id unit_id value comment ); + + # TODO could even use arrayref inflator for more speed + $self->populate( [ $rs->search( undef, { columns => \@columns } )->hri->all ] ); + + return $self; +} + sub sorted_by_columns { 'position' } sub prepared { shift->search( { -bool => 'prepare' } ) } diff --git a/lib/Coocook/Schema/ResultSet/Dish.pm b/lib/Coocook/Schema/ResultSet/Dish.pm index 52c3ad97c..4d2eb7069 100644 --- a/lib/Coocook/Schema/ResultSet/Dish.pm +++ b/lib/Coocook/Schema/ResultSet/Dish.pm @@ -20,19 +20,17 @@ sub from_recipe { meal_id => $args{meal}, comment => $args{comment}, - name => $args{name} || $recipe->name, - description => $args{description} || $recipe->description, - preparation => $args{preparation} || $recipe->preparation, + map { $_ => $args{comment} || $recipe->get_column($_) } qw( + name + description + preparation + ) } ); $dish->set_tags( [ $recipe->tags->all ] ); - # copy ingredients - for my $ingredient ( $recipe->ingredients->all ) { - $dish->create_related( - ingredients => { map { $_ => $ingredient->$_ } qw } ); - } + $dish->ingredients->copy_from_rs( $recipe->ingredients ); # adjust values of dish ingredients to new servings if ( my $servings = $args{servings} ) { diff --git a/lib/Coocook/Schema/ResultSet/Recipe.pm b/lib/Coocook/Schema/ResultSet/Recipe.pm index 619eba1d7..93d192c11 100644 --- a/lib/Coocook/Schema/ResultSet/Recipe.pm +++ b/lib/Coocook/Schema/ResultSet/Recipe.pm @@ -9,6 +9,35 @@ __PACKAGE__->load_components('+Coocook::Schema::Component::ResultSet::SortByName __PACKAGE__->meta->make_immutable; +sub from_dish { + my ( $self, $dish, %args ) = @_; + + return $self->txn_do( + sub { + my $recipe = $self->create( + { + project_id => $dish->meal->project_id, + servings => $dish->servings, + map { $_ => $args{$_} || $dish->get_column($_) } + qw( + name + description + preparation + ) + } + ); + + $recipe->set_tags( [ $dish->tags->all ] ); + + $recipe->ingredients->copy_from_rs( $dish->ingredients ); + + # TODO set from_recipe on dish? #179 + + return $recipe; + } + ); +} + sub public { my $self = shift; diff --git a/root/templates/dish/edit.tt b/root/templates/dish/edit.tt index a32d652f5..cd5cceb9d 100644 --- a/root/templates/dish/edit.tt +++ b/root/templates/dish/edit.tt @@ -12,15 +12,20 @@ js.push('/lib/coocook-web-components/dist/ingredients-editor/ingredients-editor. [% END %] -
-
-
- -
-
- +
+
+
+ +
+ +
+ +
+
diff --git a/t/schema_Recipe.t b/t/schema_Recipe.t index 035f248de..2296c655e 100644 --- a/t/schema_Recipe.t +++ b/t/schema_Recipe.t @@ -17,4 +17,54 @@ subtest duplicate => sub { is $clone->ingredients->count => $recipe->ingredients->count, "number of ingredients equal"; }; +subtest from_dish => sub { + my $db = TestDB->new; + + my $dish = $db->resultset('Dish')->find(1); + + ok my $recipe = $db->resultset('Recipe')->from_dish($dish); + + is [ $recipe->ingredients->hri->all ] => bag { + item hash { + field position => 1; + field value => 500.0; # half as in recipe + field unit_id => 1; # grams + field article_id => 1; # flour + etc; + }; + item hash { field position => 2; etc }; + item hash { field position => 3; etc }; + }; +}; + +subtest from_recipe => sub { + my $db = TestDB->new; + + my $recipe = $db->resultset('Recipe')->find(1); + + ok my $dish = $db->resultset('Dish')->from_recipe( + $recipe, + meal => 1, + comment => __FILE__, + servings => 2, # half as recipe + ); + + is $dish->from_recipe_id => $recipe->id; + is $dish->servings => 2; + is $dish->comment => __FILE__; + + is [ $dish->ingredients->hri->all ] => bag { + item hash { + field position => 1; + field value => 0.25; # half as in recipe + field unit_id => 3; # liters + field article_id => 3; # water + etc; + }; + item hash { field position => 2; etc }; + item hash { field position => 3; etc }; + item hash { field position => 4; etc }; + }; +}; + done_testing; -- GitLab From 200b75a355d4261a5142f11b7a31eec96796ebc1 Mon Sep 17 00:00:00 2001 From: Timon Foerster Date: Wed, 16 Nov 2022 00:44:03 +0100 Subject: [PATCH 4/5] Add space between buttons --- root/templates/dish/edit.tt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/root/templates/dish/edit.tt b/root/templates/dish/edit.tt index cd5cceb9d..98d952712 100644 --- a/root/templates/dish/edit.tt +++ b/root/templates/dish/edit.tt @@ -13,7 +13,7 @@ js.push('/lib/coocook-web-components/dist/ingredients-editor/ingredients-editor.
-
+
-
-
- - - +
+
+
+ +
-
- -
-
+
+ +
+
-- GitLab