Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
Merge branch 'split-projects'
Browse files Browse the repository at this point in the history
  • Loading branch information
dboehmer committed Sep 15, 2017
2 parents bff5727 + 67a00be commit a2994a7
Show file tree
Hide file tree
Showing 91 changed files with 7,569 additions and 654 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
language: perl
perl:
- "5.24"
2 changes: 2 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ my %WriteMakefileArgs = (
"Catalyst::Controller" => 0,
"Catalyst::Model" => 0,
"Catalyst::Model::DBIC::Schema" => 0,
"Catalyst::Plugin::Authentication" => 0,
"Catalyst::Plugin::ConfigLoader" => 0,
"Catalyst::Plugin::Session::State::Cookie" => 0,
"Catalyst::Plugin::Session::Store::FastMmap" => 0,
Expand Down Expand Up @@ -82,6 +83,7 @@ my %FallbackPrereqs = (
"Catalyst::Controller" => 0,
"Catalyst::Model" => 0,
"Catalyst::Model::DBIC::Schema" => 0,
"Catalyst::Plugin::Authentication" => 0,
"Catalyst::Plugin::ConfigLoader" => 0,
"Catalyst::Plugin::Session::State::Cookie" => 0,
"Catalyst::Plugin::Session::Store::FastMmap" => 0,
Expand Down
3 changes: 2 additions & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ exclude_filename = Makefile.PL
[PodWeaver]

[Prereqs]
perl = v5.8.0
perl = v5.16.0 ; needed for feature 'fc'
Catalyst::Action::RenderView = 0
Catalyst::Plugin::Authentication = 0
Catalyst::Plugin::ConfigLoader = 0
Catalyst::Plugin::Static::Simple = 0
Catalyst::Plugin::Session::State::Cookie = 0
Expand Down
37 changes: 37 additions & 0 deletions lib/Coocook.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use Catalyst qw/
Session
Session::Store::FastMmap
Session::State::Cookie
Authentication
StackTrace
Static::Simple
/;
Expand All @@ -46,15 +47,51 @@ $ENV{CATALYST_DEBUG}
__PACKAGE__->config(
name => 'Coocook',

project_deletion_confirmation => "I really want to loose my project",

# Disable deprecated behavior needed by old applications
disable_component_resolution_regex_fallback => 1,
enable_catalyst_header => 1, # Send X-Catalyst header

'Plugin::Authentication' => {
default => {
credential => {
class => 'Password',
password_field => 'password',
password_type => 'clear',
},
store => {
class => 'Minimal',
users => {
coocook => { password => "coocook" }
},
},
}
},

'View::TT' => {
INCLUDE_PATH => __PACKAGE__->path_to(qw< root templates >),
},
);

# custom helper
# TODO maybe move to designated helper module?
sub project_uri {
my $c = shift;
my $action = shift;

my $project = $c->stash->{project} || die;

return $c->uri_for_action( $action, [ $project->url_name, @_ ] );
}

# another helper
sub project {
my $c = shift;

$c->stash->{project};
}

# Start the application
__PACKAGE__->setup();

Expand Down
55 changes: 33 additions & 22 deletions lib/Coocook/Controller/Article.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Catalyst Controller.
=cut

sub auto : Private {
sub project_data : Private {
my ( $self, $c ) = @_;

$c->stash(
Expand All @@ -30,7 +30,7 @@ sub auto : Private {
default_preorder_workdays => 3,
);

my $quantities = $c->model('DB::Quantity');
my $quantities = $c->project->quantities;
$quantities = $quantities->search(
undef,
{
Expand All @@ -40,29 +40,38 @@ sub auto : Private {
);

$c->stash(
shop_sections => [ $c->model('DB::ShopSection')->sorted->all ],
shop_sections => [ $c->project->shop_sections->sorted->all ],
quantities => [ $quantities->all ],
);
}

sub index : Path('/articles') : Args(0) {
sub index : GET Chained('/project/base') PathPart('articles') Args(0) {
my ( $self, $c ) = @_;

my $articles = $c->model('DB::Article')->sorted;
my $articles = $c->project->articles->sorted->search( undef, { prefetch => 'shop_section' } );

$c->stash( articles => $articles );
$c->forward('project_data');
$c->stash( articles => [ $articles->all ] );
}

sub edit : GET Path Args(1) {
sub base : Chained('/project/base') PathPart('article') CaptureArgs(1) {
my ( $self, $c, $id ) = @_;

my $article = $c->model('DB::Article')->find($id)
or die "Can't find article"; # TODO serious error message
$c->stash( article => $c->project->articles->find($id) ); # TODO error handling
}

sub edit : GET Chained('base') PathPart('') Args(0) {
my ( $self, $c ) = @_;

$c->forward('project_data');

my $article = $c->stash->{article}
or die "Can't find article"; # TODO serious error message

# collect related recipes linked to dishes and independent dishes
my $dishes = $article->dishes;
$dishes = $dishes->search( undef, { order_by => $dishes->me('name'), prefetch => 'meal' } );
my $recipes = $article->recipes->search( undef, { order_by => 'name' } );
my $recipes = $article->recipes->sorted;

my @dishes;
my @recipes = map +{ recipe => $_, dishes => [] }, $recipes->all; # sorted hashrefs
Expand All @@ -86,7 +95,7 @@ sub edit : GET Path Args(1) {
);
}

sub create : Local : POST {
sub create : POST Chained('/project/base') PathPart('articles/create') Args(0) {
my ( $self, $c, $id ) = @_;

my $units = $c->model('DB::Unit')->search(
Expand Down Expand Up @@ -131,25 +140,27 @@ sub create : Local : POST {
$c->detach('redirect');
}

sub delete : Local : Args(1) : POST {
my ( $self, $c, $id ) = @_;
$c->model('DB::Article')->find($id)->delete;
sub delete : POST Chained('base') Args(0) {
my ( $self, $c ) = @_;

$c->stash->{article}->delete();
$c->detach('redirect');
}

sub update : POST Path Args(1) {
my ( $self, $c, $id ) = @_;
sub update : POST Chained('base') Args(0) {
my ( $self, $c ) = @_;

my $units = $c->model('DB::Unit')->search(
my $units = $c->project->units->search(
{
id => { -in => [ $c->req->param('units') ] },
}
);

my $tags =
$c->model('DB::Tag')->from_names( scalar $c->req->param('tags') );
my $tags = $c->project->tags->from_names( scalar $c->req->param('tags') );

my $shop_section = $c->project->shop_sections->find( scalar $c->req->param('shop_section') );

my $article = $c->model('DB::Article')->find($id);
my $article = $c->stash->{article};

$c->model('DB')->schema->txn_do(
sub {
Expand All @@ -159,7 +170,7 @@ sub update : POST Path Args(1) {
{
name => scalar $c->req->param('name'),
comment => scalar $c->req->param('comment'),
shop_section => scalar $c->req->param('shop_section'),
shop_section => $shop_section ? $shop_section->id : undef,
}
);
if ( scalar $c->req->param('shelf_life') ) {
Expand Down Expand Up @@ -193,7 +204,7 @@ sub update : POST Path Args(1) {
sub redirect : Private {
my ( $self, $c ) = @_;

$c->response->redirect( $c->uri_for_action( $self->action_for('index') ) );
$c->response->redirect( $c->project_uri( $self->action_for('index') ) );
}

=encoding utf8
Expand Down
82 changes: 45 additions & 37 deletions lib/Coocook/Controller/Dish.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ Catalyst Controller.
=cut

sub edit : Path Args(1) {
sub base : Chained('/project/base') PathPart('dish') CaptureArgs(1) {
my ( $self, $c, $id ) = @_;

my $dish = $c->model('DB::Dish')->search( undef, { prefetch => 'meal' } )->find($id);
# TODO error handling
$c->stash( dish => $c->project->dishes->search( undef, { prefetch => 'meal' } )->find($id) );
}

sub edit : GET Chained('base') PathPart('') Args(0) {
my ( $self, $c ) = @_;

my $dish = $c->stash->{dish};

# candidate meals for preparing this dish: same day or earlier
my $meals = $c->model('DB::Meal');
my $meals = $c->project->meals;
my $prepare_meals = $meals->search(
{
id => { '!=' => $dish->meal->id },
Expand All @@ -37,28 +44,29 @@ sub edit : Path Args(1) {
$c->stash(
dish => $dish,
ingredients => [ $dish->ingredients_ordered->all ],
articles => [ $c->model('DB::Article')->all ],
units => [ $c->model('DB::Unit')->sorted->all ],
articles => [ $c->project->articles->all ],
units => [ $c->project->units->sorted->all ],
prepare_meals => [ $prepare_meals->all ],
);
}

sub delete : Local Args(1) POST {
sub delete : Local Args(1) POST { # TODO fix
my ( $self, $c, $id ) = @_;

my $dish = $c->model('DB::Dish')->find($id);
my $dish = $c->project->dishes->find($id);

$dish->delete;

$c->response->redirect( $c->uri_for_action( '/meal/edit', $dish->get_column('meal') ) );
$c->response->redirect( $c->project_uri( '/meal/edit', $dish->get_column('meal') ) );
}

sub create : Local Args(0) POST {
sub create : POST Chained('/project/base') PathPart('dishes/create') Args(0) {
my ( $self, $c ) = @_;

my $dish = $c->model('DB::Dish')->create(
{
meal => scalar $c->req->param('meal'),
my $meal = $c->project->meals->find( scalar scalar $c->req->param('meal') );

my $dish = $meal->create_related(
dishes => {
servings => scalar $c->req->param('servings'),
name => scalar $c->req->param('name'),
description => scalar $c->req->param('description') // "",
Expand All @@ -68,16 +76,16 @@ sub create : Local Args(0) POST {
}
);

$c->response->redirect( $c->uri_for_action( '/dish/edit', $dish->id ) );
$c->response->redirect( $c->project_uri( '/dish/edit', $dish->id ) );
}

sub from_recipe : Local Args(0) POST {
sub from_recipe : POST Chained('/project/base') PathPart('dishes/from_recipe') Args(0) {
my ( $self, $c ) = @_;

my $meal = $c->model('DB::Meal')->find( scalar $c->req->param('meal') );
my $recipe = $c->model('DB::Recipe')->find( scalar $c->req->param('recipe') );
my $meal = $c->project->meals->find( scalar $c->req->param('meal') );
my $recipe = $c->project->recipes->find( scalar $c->req->param('recipe') );

$c->model('DB::Dish')->from_recipe(
my $dish = $c->model('DB::Dish')->from_recipe(
$recipe,
(
meal => $meal->id,
Expand All @@ -86,25 +94,26 @@ sub from_recipe : Local Args(0) POST {
)
);

$c->response->redirect( $c->uri_for_action( '/project/edit', $meal->get_column('project') ) );
$c->response->redirect( $c->project_uri( '/dish/edit', $dish->id ) );
}

sub recalculate : Local Args(1) POST {
my ( $self, $c, $id ) = @_;
sub recalculate : POST Chained('base') Args(0) {
my ( $self, $c ) = @_;

my $dish = $c->model('DB::Dish')->find($id);
my $dish = $c->stash->{dish};

$dish->recalculate( scalar $c->req->param('servings') );

$c->detach( redirect => [$id] );
$c->detach( redirect => [ $dish->id, '#ingredients' ] );
}

sub add : Local Args(1) POST {
my ( $self, $c, $id ) = @_;
sub add : POST Chained('base') Args(0) {
my ( $self, $c ) = @_;

$c->model('DB::DishIngredient')->create(
{
dish => $id,
my $dish = $c->stash->{dish};

$dish->create_related(
ingredients => {
article => scalar $c->req->param('article'),
value => scalar $c->req->param('value'),
unit => scalar $c->req->param('unit'),
Expand All @@ -113,13 +122,13 @@ sub add : Local Args(1) POST {
}
);

$c->detach( redirect => [$id] );
$c->detach( redirect => [ $dish->id, '#ingredients' ] );
}

sub update : Local Args(1) POST {
my ( $self, $c, $id ) = @_;
sub update : POST Chained('base') Args(0) {
my ( $self, $c ) = @_;

my $dish = $c->model('DB::Dish')->find($id);
my $dish = $c->stash->{dish};

$c->model('DB')->schema->txn_do(
sub {
Expand All @@ -135,7 +144,7 @@ sub update : Local Args(1) POST {
}
);

my $tags = $c->model('DB::Tag')->from_names( scalar $c->req->param('tags') );
my $tags = $c->project->tags->from_names( scalar $c->req->param('tags') );
$dish->set_tags( [ $tags->all ] );

for my $ingredient ( $dish->ingredients->all ) {
Expand All @@ -160,13 +169,13 @@ sub update : Local Args(1) POST {
}
);

$c->detach( redirect => [$id] );
$c->detach( redirect => [ $dish->id, '#ingredients' ] );
}

sub reposition : POST Local Args(1) {
sub reposition : POST Chained('/project/base') PathPart('dish_ingredient/reposition') Args(1) {
my ( $self, $c, $id ) = @_;

my $ingredient = $c->model('DB::DishIngredient')->find($id);
my $ingredient = $c->project->dishes->ingredients->find($id);

if ( $c->req->param('up') ) {
$ingredient->move_previous();
Expand All @@ -184,8 +193,7 @@ sub reposition : POST Local Args(1) {
sub redirect : Private {
my ( $self, $c, $id, $fragment ) = @_;

$c->response->redirect(
$c->uri_for_action( $self->action_for('edit'), $id ) . ( $fragment // '' ) );
$c->response->redirect( $c->project_uri( $self->action_for('edit'), $id ) . ( $fragment // '' ) );
}

=encoding utf8
Expand Down
Loading

0 comments on commit a2994a7

Please sign in to comment.