File Coverage

blib/lib/Interchange6/Schema/Result/Cart.pm
Criterion Covered Total %
statement 19 19 100.0
branch 5 6 83.3
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 29 30 96.6


line stmt bran cond sub pod time code
1 2     2   1142 use utf8;
  2         5  
  2         17  
2              
3             package Interchange6::Schema::Result::Cart;
4              
5             =head1 NAME
6              
7             Interchange6::Schema::Result::Cart
8              
9             =cut
10              
11 2     2   108 use Carp qw/croak/;
  2         6  
  2         151  
12 2         16 use Interchange6::Schema::Candy -components =>
13 2     2   21 [qw(InflateColumn::DateTime TimeStamp)];
  2         5  
14              
15             =head1 DESCRIPTION
16              
17             The Cart class (table) is used for storing shopping carts with products in the
18             cart held in the related L<Interchange6::Schema::Result::CartProduct> class.
19              
20             =head1 ACCESSORS
21              
22             =head2 carts_id
23              
24             Primary key.
25              
26             =cut
27              
28             primary_column carts_id => {
29             data_type => "integer",
30             is_auto_increment => 1,
31             };
32              
33             =head2 name
34              
35             The name of the cart. You might perhaps have a "main" cart that is used by
36             default for the current shopping session and also a "wishlist" cart. Other
37             uses might be "saved_items" so a user can save things for another time or
38             maybe on logout all cart items are moved to "previous_session" cart.
39              
40             =cut
41              
42             column name => {
43             data_type => "varchar",
44             default_value => "",
45             size => 255,
46             };
47              
48             =head2 users_id
49              
50             Foreign key constraint on L<Interchange6::Schema::Result::User/users_id>
51             via L</user> relationship.
52              
53             =cut
54              
55             column users_id => {
56             data_type => "integer",
57             is_nullable => 1,
58             };
59              
60             =head2 sessions_id
61              
62             Foreign key constraint on L<Interchange6::Schema::Result::Session/sessions_id>
63             via L</session> relationship. Is nullable.
64              
65             =cut
66              
67             column sessions_id => {
68             data_type => "varchar",
69             is_nullable => 1,
70             size => 255,
71             };
72              
73             =head2 created
74              
75             Date and time when this record was created returned as L<DateTime> object.
76             Value is auto-set on insert.
77              
78             =cut
79              
80             column created => {
81             data_type => "datetime",
82             set_on_create => 1,
83             };
84              
85             =head2 last_modified
86              
87             Date and time when this record was last modified returned as L<DateTime> object.
88             Value is auto-set on insert and update.
89              
90             =cut
91              
92             column last_modified => {
93             data_type => "datetime",
94             set_on_create => 1,
95             set_on_update => 1,
96             };
97              
98             =head1 UNIQUE CONSTRAINTS
99              
100             =head2 carts_name_sessions_id
101              
102             On ( name, sessions_id )
103              
104             =cut
105              
106             unique_constraint carts_name_sessions_id => [qw/ name sessions_id /];
107              
108             =head1 RELATIONS
109              
110             =head2 cart_products
111              
112             Type: has_many
113              
114             Related object: L<Interchange6::Schema::Result::CartProduct>
115              
116             =cut
117              
118             has_many
119             cart_products => "Interchange6::Schema::Result::CartProduct", "carts_id";
120              
121             =head2 session
122              
123             Type: belongs_to
124              
125             Related object: L<Interchange6::Schema::Result::Session>
126              
127             =cut
128              
129             belongs_to
130             session => "Interchange6::Schema::Result::Session",
131             { sessions_id => "sessions_id" },
132             {
133             is_deferrable => 1,
134             on_delete => "SET NULL",
135             join_type => "left"
136             };
137              
138             =head2 user
139              
140             Type: belongs_to
141              
142             Related object: L<Interchange6::Schema::Result::User>
143              
144             =cut
145              
146             belongs_to
147             user => "Interchange6::Schema::Result::User",
148             { users_id => "users_id" },
149             {
150             is_deferrable => 1,
151             on_delete => "CASCADE",
152             on_update => "CASCADE",
153             join_type => "left"
154             };
155              
156             =head1 METHODS
157              
158             =head2 clone($name)
159              
160             Return a clone of the with the new name. If a clone with the same name
161             and same session already exists, the clone is removed and recreated anew.
162              
163             =cut
164              
165             sub clone {
166 10     10 1 124151 my ($self, $name) = @_;
167 10 100       81 croak "Can't clone a cart without a name" unless $name;
168 7 50       198 croak "Can't clone using the same name" if $name eq $self->name;
169 7         136 my $schema = $self->result_source->schema;
170 7         105 my $guard = $schema->txn_scope_guard;
171 7 100       3534 if (defined $self->sessions_id) {
172 4         78 $schema->resultset('Cart')->search({
173             name => $name,
174             sessions_id => $self->sessions_id,
175             })->delete;
176             }
177             # the products are carried over by copy itself because it's an has_many
178             # https://metacpan.org/pod/DBIx::Class::Row#copy
179 7         9212 my $clone = $self->copy({ name => $name });
180 7         121563 $guard->commit;
181 7         1810 return $clone;
182             }
183              
184             1;