Skip to content

Instantly share code, notes, and snippets.

@dhoss
Forked from anonymous/gist:1419435
Created December 1, 2011 20:05
Show Gist options
  • Save dhoss/1419440 to your computer and use it in GitHub Desktop.
Save dhoss/1419440 to your computer and use it in GitHub Desktop.
package SuiteSetup::Schema::Result::Account;
use SuiteSetup::Schema::Candy
-components => [qw(
InflateColumn::DateTime
TimeStamp
Helper::Row::ToJSON
)];
use Data::Dumper;
primary_column accountid => {
data_type => 'int',
is_nullable => 0,
is_auto_increment => 1,
is_serializable => 1,
extra => { unsigned => 1 },
};
column reseller => {
data_type => 'int',
is_nullable => 0,
extra => { unsigned => 1 },
};
column status => {
data_type => 'enum',
is_nullable => 0,
values => [qw( active suspended cancelled )],
extra => { list => [qw( active suspended cancelled )], },
};
column created_at => {
data_type => 'datetime',
is_nullable => 0,
set_on_create => 1,
is_serializable => 1,
};
column updated_at => {
data_type => 'datetime',
is_nullable => 1,
set_on_create => 1,
set_on_update => 1,
is_serializable => 1,
};
column contact => {
data_type => 'int',
is_nullable => 0,
extra => { unsigned => 1 },
};
has_many 'users' => 'SuiteSetup::Schema::Result::User', 'userid';
has_many 'contacts' => 'SuiteSetup::Schema::Result::Contact', 'account';
belongs_to 'reseller' => 'SuiteSetup::Schema::Result::Reseller', 'reseller';
=head2 TO_JSON
Override the Helper::Row::ToJSON method to take care of DateTime inflation, etc.
=cut
sub TO_JSON {
my $self = shift;
my $hash = {
%{ $self->next::method },
created_at => $self->created_at . "",
updated_at => $self->updated_at . "",
};
return $hash;
}
1;
package SuiteSetup::Schema::Result::Contact;
use Moose;
use MooseX::NonMoose;
use SuiteSetup::Schema::Candy -components => [
qw(
InflateColumn::DateTime
TimeStamp
Helper::Row::ToJSON
)
];
with 'SuiteSetup::HasContainer';
use sugar;
primary_column contactid => {
data_type => 'int',
is_nullable => 0,
is_auto_increment => 1,
extra => { unsigned => 1 },
};
column type => {
data_type => 'varchar',
size => 200,
is_nullable => 0,
};
column account => {
data_type => 'int',
is_nullable => 0,
extra => { unsigned => 1 },
};
column name => {
data_type => 'varchar',
size => 200,
is_nullable => 0,
};
column email => {
data_type => 'int',
is_nullable => 1,
extra => { unsigned => 1 }
};
column tags => {
data_type => 'int',
is_nullable => 1,
extra => { unsigned => 1 },
};
belongs_to 'account' => 'SuiteSetup::Schema::Result::Account', 'account';
has_many 'tags' => 'SuiteSetup::Schema::Result::ContactProperty', 'contact';
has_many 'email_addresses' => 'SuiteSetup::Schema::Result::EmailAddress', 'contact';
1;
DBIx::Class::Relationship::Base::create_related(): Unable to perform storage-dependent operations with a detached result source (source '_unnamed_' is not associated with a schema). at t/schema.t line 21
test:
package SuiteSetup::Schema::Result::Contact;
use Moose;
use MooseX::NonMoose;
use SuiteSetup::Schema::Candy -components => [
qw(
InflateColumn::DateTime
TimeStamp
Helper::Row::ToJSON
)
];
with 'SuiteSetup::HasContainer';
use sugar;
primary_column contactid => {
data_type => 'int',
is_nullable => 0,
is_auto_increment => 1,
extra => { unsigned => 1 },
};
column type => {
data_type => 'varchar',
size => 200,
is_nullable => 0,
};
column account => {
data_type => 'int',
is_nullable => 0,
extra => { unsigned => 1 },
};
column name => {
data_type => 'varchar',
size => 200,
is_nullable => 0,
};
column email => {
data_type => 'int',
is_nullable => 1,
extra => { unsigned => 1 }
};
column tags => {
data_type => 'int',
is_nullable => 1,
extra => { unsigned => 1 },
};
belongs_to 'account' => 'SuiteSetup::Schema::Result::Account', 'account';
has_many 'tags' => 'SuiteSetup::Schema::Result::ContactProperty', 'contact';
has_many 'email_addresses' => 'SuiteSetup::Schema::Result::EmailAddress', 'contact';
1;
use Test::More;
use strict;
use warnings;
use Test::DBIx::Class qw(:resultsets);
fixtures_ok 'basic';
ok my $reseller = Reseller->find({ name => 'herschel' })
=> "found a reseller";
is_fields $reseller, { name => 'herschel', details => 'derp' }
=> "expected fields for a reseller";
$reseller->create_related('accounts', {
status => 'active'
});
ok $_->status eq 'active', "account status should be active"
for $reseller->accounts;
ok my $account = $reseller->accounts->first;
ok my $contact = $account->create_related('contacts',
{
type => 'test',
name => 'tester mctestington',
}),
=> 'created a contact';
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment