Skip to content
Browse files

fix: now DBIx::Simple::Class->dbix, app->dbix and $c->dbix are the sa…

…me instance
  • Loading branch information...
1 parent dee55dd commit f708c91903850fc8699f196678506a7886882245 @kberov committed Aug 4, 2012
Showing with 38 additions and 119 deletions.
  1. +7 −5 lib/Mojolicious/Plugin/DSC.pm
  2. +24 −21 t/advanced.t
  3. +4 −90 t/lib/My.pm
  4. +3 −3 t/lib/My/Groups.pm
View
12 lib/Mojolicious/Plugin/DSC.pm
@@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Plugin';
use DBIx::Simple::Class;
-our $VERSION = '0.03';
+our $VERSION = '0.04';
#some known good defaults
my %COMMON_ATTRIBUTES = (
@@ -19,7 +19,8 @@ my $DRIVER_ATTRIBUTES = {
};
my $MEx = 'Mojo::Exception';
-has config =>sub{{}};
+has config => sub { {} };
+
sub register {
my ($self, $app, $config) = @_;
@@ -73,11 +74,12 @@ sub register {
$config->{onconnect_do} = [$config->{onconnect_do}];
}
for my $sql (@{$config->{onconnect_do}}) {
- $dbix->dbh->do($sql) if $sql;
+ $dbix->dbh->do($sql);
}
- DBIx::Simple::Class->dbix($dbix); #do not forget
$config->{dbix_helper} ||= 'dbix';
- $app->helper($config->{dbix_helper}, $dbix); #add helper dbix
+ $app->attr($config->{dbix_helper}, sub {$dbix});
+ $app->helper($config->{dbix_helper}, $app->dbix); #add helper dbix
+ DBIx::Simple::Class->dbix($app->dbix); #do not forget
if ($config->{namespace} && @{$config->{load_classes}}) {
View
45 t/advanced.t
@@ -4,46 +4,49 @@ use Mojo::Base -strict;
BEGIN {
$ENV{MOJO_NO_IPV6} = 1;
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
+ use lib qw(t/lib);
}
-use Test::More tests => 5;
+use Test::More tests => 6;
package main;
+
use Mojolicious::Lite;
use Test::Mojo;
use Data::Dumper;
-use lib qw(t/lib);
-
-isa_ok(plugin('DSC', {
- database => ':memory:',
- DEBUG => 0,
- namespace => 'My',
- load_classes => ['Groups'],
- dbh_attributes => {},
- driver => 'SQLite',
- onconnect_do =>[],
- dbix_helper =>'dbix',
- host =>'localhost',
- dsn => 'dbi:SQLite:database=:memory:;host=localhost'
- }),'Mojolicious::Plugin::DSC');
+my $config = {
+ database => ':memory:',
+ DEBUG => 0,
+ namespace => 'My',
+ load_classes => ['Groups'],
+ dbh_attributes => {},
+ driver => 'SQLite',
+ onconnect_do => [],
+ dbix_helper => 'dbix',
+ host => 'localhost',
+ dsn => 'dbi:SQLite:database=:memory:;host=localhost'
+};
+
+isa_ok(plugin('DSC', $config), 'Mojolicious::Plugin::DSC');
my $my_groups_table = <<"T";
-CREATE TABLE "my groups"(
+CREATE TABLE my_groups(
id INTEGER PRIMARY KEY AUTOINCREMENT,
"group" VARCHAR(12),
"is' enabled" INT DEFAULT 0
)
T
-app->dbix->query($my_groups_table);
-#my $group = My::Groups->new(group=>'Mojo',is_enabled=>1);
-#isa_ok($group,'My::Groups');
+ok(app->dbix->query($my_groups_table), 'app->dbix works');
+
+ok(app->dbix->query('INSERT INTO my_groups ("group") VALUES(?)', 'pojo'),
+ 'app->dbix->query works');
get '/' => sub {
my $self = shift;
- $self->render_text('Hello Mojo!');
+ $self->render_text('Hello ' . My::Groups->find(1)->group . '!');
};
my $t = Test::Mojo->new;
-$t->get_ok('/')->status_is(200)->content_is('Hello Mojo!');
+$t->get_ok('/')->status_is(200)->content_is('Hello pojo!');
View
94 t/lib/My.pm
@@ -1,97 +1,11 @@
-use 5.010;
-use strict;
-use warnings;
-use utf8;
-
-use DBIx::Simple::Class;
{
package My; #our schema
+ use 5.010;
+ use strict;
+ use warnings;
+ use utf8;
use base qw(DBIx::Simple::Class);
sub namespace {__PACKAGE__}
}
-
-{
-
- package My::User;
- use base qw(My);
-
- sub TABLE {'users'}
- sub COLUMNS { [qw(id group_id login_name login_password disabled)] }
- sub WHERE { {disabled => 1} }
-
- #See Params::Check
- my $_CHECKS = {
- id => {allow => qr/^\d+$/x},
- group_id => {allow => qr/^\d+$/x, default => 1},
- disabled => {
- default => 1,
- allow => sub {
- return $_[0] =~ /^[01]$/x;
- }
- },
- login_name => {allow => qr/^\p{IsAlnum}{4,12}$/x},
- login_password => {
- required => 1,
- allow => sub { $_[0] =~ /^[\w\W]{8,20}$/x; }
- }
-
- #...
- };
- sub CHECKS {$_CHECKS}
-
- sub id {
- my ($self, $value) = @_;
- if (defined $value) { #setting value
- $self->{data}{id} = $self->_check(id => $value);
-
- #make it chainable
- return $self;
- }
- $self->{data}{id} //= $self->CHECKS->{id}{default}; #getting value
- }
-}
-{
-
- package My::Group;
- use base qw(My);
-
- use constant TABLE => 'groups';
- use constant COLUMNS => [qw(id group_name foo-bar data)];
- use constant WHERE => {};
-
- #See Params::Check
- use constant CHECKS => {};
-}
-
-{
-
- package My::Collision;
- use base qw(My);
-
- use constant TABLE => 'collision';
- use constant COLUMNS => [qw(id data)];
- use constant WHERE => {};
- use constant ALIASES => {data => 'column_data'};
-
- #CHECKS are on columns
- use constant CHECKS => {
- id => {allow => qr/^\d+$/x},
- data => {default => '',} #that's ok
- };
-}
-
-{
-
- package My::SiteUser;
- use base qw(My::User);
- my $_CHECKS = My::User->CHECKS;
- $_CHECKS->{group_id}{default} = 3;
- sub CHECKS {$_CHECKS}
- sub WHERE { {disabled => 0, group_id => $_CHECKS->{group_id}{default}} }
-
- #merge with parent $SQL
- __PACKAGE__->SQL(GUEST_USER => 'SELECT * FROM users WHERE login_name = \'guest\'');
-}
-
1;
View
6 t/lib/My/Groups.pm
@@ -1,10 +1,10 @@
+package My::Groups;
use strict;
use warnings;
use utf8;
-
-package My::Groups;
+use My;
use base qw (My);
-sub TABLE {'my groups'} #problem
+sub TABLE {'my_groups'} #problem
sub COLUMNS { ['id', 'group', 'is\' enabled'] } #problem
sub ALIASES {

0 comments on commit f708c91

Please sign in to comment.
Something went wrong with that request. Please try again.