Browse files

Initial commit: schema is working but need some love

  • Loading branch information...
0 parents commit 4e8ee4c7386e58617a3d2240b5d949c99f116df5 @diegok committed Mar 6, 2012
9 dist.ini
@@ -0,0 +1,9 @@
+name = Curs-Schema
+author = Diego Kuperman <diego@freekeylabs.com>
+license = Perl_5
+copyright_holder = Diego Kuperman
+copyright_year = 2012
+
+version = 1
+
+[@Basic]
19 lib/Curs/Schema.pm
@@ -0,0 +1,19 @@
+package Curs::Schema;
+use strict; use warnings;
+use parent 'DBIx::Class::Schema';
+
+our $VERSION = 1;
+
+__PACKAGE__->load_namespaces();
+__PACKAGE__->load_components( qw/Schema::Versioned/ );
+
+# add extra attributes to all tables on the schema!
+sub sqlt_deploy_hook {
+ my ($self, $sqlt_schema) = @_;
+
+ for my $sqlt_table ( $sqlt_schema->get_tables ) {
+ $sqlt_table->extra( mysql_charset => 'utf8' );
+ }
+}
+
+1;
46 lib/Curs/Schema/Result/Event.pm
@@ -0,0 +1,46 @@
+package Curs::Schema::Result::Event;
+use strict; use warnings;
+use parent 'DBIx::Class';
+use Text::Unidecode qw();
+
+__PACKAGE__->load_components(qw/ TimeStamp Core /);
+
+__PACKAGE__->table( 'event' );
+__PACKAGE__->add_columns(
+ id => { data_type => 'int', is_nullable => 0, is_auto_increment => 1 },
+ title => { data_type => 'varchar', is_nullable => 0, size => 255 },
+ description => { data_type => 'text', is_nullable => 1 },
+ created => { data_type => 'datetime', set_on_create => 1, },
+ updated => { data_type => 'datetime', set_on_create => 1, set_on_update => 1, },
+ start => { data_type => 'datetime', is_nullable => 1 },
+ end => { data_type => 'datetime', is_nullable => 1 },
+ creator_id => { data_type => 'int', is_nullable => 0 },
+);
+
+__PACKAGE__->set_primary_key( 'id' );
+__PACKAGE__->has_many( 'rel_attendees', 'Curs::Schema::Result::EventUser', { 'foreign.event_id' => 'self.id' }, { cascade_delete => 1 });
+__PACKAGE__->many_to_many( 'attendees', 'rel_attendees', 'user' );
+
+=head2 sqlt_deploy_hook
+ Create indexes on deploy
+=cut
+sub sqlt_deploy_hook {
+ my ($self, $sqlt_table) = @_;
+
+ $sqlt_table->add_index( name => 'creation_idx', fields => [qw/ created /] );
+ $sqlt_table->add_index( name => 'updated_idx', fields => [qw/ updated /] );
+ $sqlt_table->add_index( name => 'dateframe_idx', fields => [qw/ start end /] );
+}
+
+#TODO: generate slug (add slug field with unique index)
+sub insert {
+ my $self = shift;
+ $self->next::method(@_);
+}
+
+sub update {
+ my $self = shift;
+ $self->next::method(@_);
+}
+
+1;
23 lib/Curs/Schema/Result/EventUser.pm
@@ -0,0 +1,23 @@
+package Curs::Schema::Result::EventUser;
+use strict; use warnings;
+use parent qw( DBIx::Class );
+
+__PACKAGE__->load_components(qw/ TimeStamp Core /);
+
+__PACKAGE__->table( 'event_user' );
+__PACKAGE__->add_columns(
+ "event_id",
+ { data_type => 'INT', default_value => 0, is_nullable => 0 },
+ "user_id",
+ { data_type => 'INT', default_value => 0, is_nullable => 0 },
+ created => { data_type => 'datetime', set_on_create => 1, },
+);
+
+__PACKAGE__->set_primary_key( 'event_id', 'user_id' );
+__PACKAGE__->resultset_attributes({ order_by => [ 'created' ] });
+
+__PACKAGE__->belongs_to( 'user', 'Curs::Schema::Result::User', { 'foreign.id' => 'self.user_id' });
+__PACKAGE__->belongs_to( 'event', 'Curs::Schema::Result::Event', { 'foreign.id' => 'self.event_id' });
+
+
+1;
22 lib/Curs/Schema/Result/Role.pm
@@ -0,0 +1,22 @@
+package Curs::Schema::Result::Role;
+use strict;
+use warnings;
+use parent qw( DBIx::Class );
+use utf8;
+
+__PACKAGE__->load_components('Core');
+__PACKAGE__->table( 'role' );
+
+__PACKAGE__->add_columns(
+ 'id',
+ { data_type => 'INT', is_nullable => 0, is_auto_increment => 1 },
+ 'name',
+ { data_type => 'VARCHAR', default_value => "", is_nullable => 0, size => 20 },
+);
+
+__PACKAGE__->set_primary_key( 'id' );
+__PACKAGE__->add_unique_constraint( 'name', ['name'] );
+__PACKAGE__->has_many( 'user_roles', 'Curs::Schema::Result::UserRole', { 'foreign.role_id' => 'self.id' } );
+__PACKAGE__->many_to_many( 'users', 'user_roles', 'user' );
+
+1;
60 lib/Curs/Schema/Result/User.pm
@@ -0,0 +1,60 @@
+package Curs::Schema::Result::User;
+use strict;
+use warnings;
+
+use parent qw( DBIx::Class );
+use utf8;
+
+__PACKAGE__->load_components(qw/ EncodedColumn Core /);
+
+__PACKAGE__->table( 'user' );
+__PACKAGE__->add_columns(
+ 'id',
+ { data_type => 'INT', is_nullable => 0, is_auto_increment => 1 },
+ 'email',
+ { data_type => 'VARCHAR', default_value => "", is_nullable => 0, size => 255 },
+ 'password',
+ {
+ data_type => 'VARCHAR',
+ default_value => '',
+ is_nullable => 0,
+ size => 40 + 10,
+ encode_column => 1,
+ encode_class => 'Digest',
+ encode_args => { algorithm => 'SHA-1', format => 'hex', salt_length => 10 },
+ encode_check_method => 'check_password',
+ },
+ 'name',
+ {
+ data_type => 'VARCHAR',
+ default_value => "",
+ is_nullable => 1,
+ size => 64,
+ },
+);
+
+__PACKAGE__->set_primary_key( 'id' );
+__PACKAGE__->add_unique_constraint( 'email', ['email'] );
+
+__PACKAGE__->has_many( 'user_roles', 'Curs::Schema::Result::UserRole', { 'foreign.user_id' => 'self.id' } );
+__PACKAGE__->many_to_many( 'roles', 'user_roles', 'role' );
+
+=head2 update_roles
+ Set the user's roles
+ Arguments: list of role names ('admin', 'editor', etc)
+=cut
+sub update_roles {
+ my( $self, @roles ) = @_;
+
+ my $rs = $self->result_source->schema->resultset('Role');
+ my %roles = map { $_ => 1 } @roles;
+
+ my @objs;
+ if (keys %roles) {
+ @objs = $rs->search({ name => { 'IN' , [ keys %roles ] } });
+ }
+ $self->set_roles( \@objs );
+ return 1; # true value so we can test for success
+}
+
+1;
19 lib/Curs/Schema/Result/UserRole.pm
@@ -0,0 +1,19 @@
+package Curs::Schema::Result::UserRole;
+use parent 'DBIx::Class';
+
+__PACKAGE__->load_components('Core');
+
+__PACKAGE__->table( 'user_role' );
+__PACKAGE__->add_columns(
+ "user_id",
+ { data_type => 'INT', default_value => 0, is_nullable => 0 },
+ "role_id",
+ { data_type => 'INT', default_value => 0, is_nullable => 0 },
+);
+
+__PACKAGE__->set_primary_key( 'user_id', 'role_id' );
+
+__PACKAGE__->belongs_to( 'user', 'Curs::Schema::Result::User', { 'foreign.id' => 'self.role_id' });
+__PACKAGE__->belongs_to( 'role', 'Curs::Schema::Result::Role', { 'foreign.id' => 'self.role_id' });
+
+1;
16 t/lib/TestSchema.pm
@@ -0,0 +1,16 @@
+package # hide from cpan!
+ TestSchema;
+
+use strict;
+use warnings;
+
+use DBICx::TestDatabase;
+
+sub new {
+ $ENV{DBIC_NO_VERSION_CHECK}++;
+ my $schema = DBICx::TestDatabase->new( 'Curs::Schema' );
+ return $schema;
+}
+
+1;
+
32 t/schema.t
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+use Test::More 'no_plan';
+
+use lib 't/lib';
+BEGIN { use_ok 'TestSchema' }
+
+ok ( my $schema = TestSchema->new(), 'Create a schema object' );
+isa_ok ( $schema, 'Curs::Schema');
+
+ok ( my $admin = $schema->resultset('User')->create({
+ email => 'admin@curs.perl.cat',
+ password => 'admin',
+ name => 'Super user for barcelona.pm'
+ }), 'Create user' );
+ok ( $admin->add_to_roles({ name => 'user' }), 'Give role user to user :-)' );
+ok ( $admin->add_to_roles({ name => 'admin' }), 'Give role admin to user' );
+
+ok ( my $user = $schema->resultset('User')->create({
+ email => 'user@curs.perl.cat',
+ password => 'user',
+ name => 'Default user'
+ }), 'Create user' );
+ok ( $user->add_to_roles({ name => 'user' }), 'Give role user to user :-)' );
+
+ok ( my $event = $schema->resultset('Event')->create({
+ title => 'Curs avançat de Perl',
+ description => 'Something nice to do on a saturday!',
+ creator_id => $admin->id,
+ start => DateTime->new( year => 2012, month => 3, day => 10, hour => 9 ),
+ end => DateTime->new( year => 2012, month => 3, day => 10, hour => 9, minute => 15 ),
+ }), 'Create event');

0 comments on commit 4e8ee4c

Please sign in to comment.