3 Reaction::Manual::Example - Simple Reaction example
7 This tutorial will guide you through the process of setting up and testing a
8 very basic CRUD application based on the database from
9 L<DBIx::Class::Manual::Example>.
11 You need at least a fairly basic understanding of L<DBIx::Class::Schema> for
12 this example to have value for you.
16 Install L<DBIx::Class> via CPAN.
18 Install Reaction from http://code2.0beta.co.uk/reaction/svn via SVN or SVK.
20 Set up the database as mentioned in L<DBIx::Class::Manual::Example>. Don't do
21 any of the DBIx::Class related stuff, only the SQLite database.
23 =head2 Create the application
25 catalyst.pl Test::Reaction
27 script/test_reaction_create.pl Model Test::Reaction DBIC::Schema Test::Reaction::DB
29 Also, remember to include Catalyst::Plugin::I18N in your plugin list, like
32 use Catalyst qw/-Debug ConfigLoader Static::Simple I18N/;
34 =head2 Set up DBIx::Class::Schema
36 In addition to the normal DBIC stuff, you need to moosify your DBIC classes.
38 Change directory back from db to the directory app:
43 Then, create the following DBIx::Class::Schema classes:
47 package Test::Reaction::DB;
49 use base 'DBIx::Class::Schema';
51 __PACKAGE__->load_classes;
57 package Test::Reaction::DB::Artist;
59 use base 'DBIx::Class';
62 has 'artistid' => ( isa => 'Int', is => 'ro', required => 1 );
63 has 'name' => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
70 __PACKAGE__->load_components(qw/PK::Auto Core/);
71 __PACKAGE__->table('artist');
72 __PACKAGE__->add_columns(qw/ artistid name /);
73 __PACKAGE__->set_primary_key('artistid');
74 __PACKAGE__->has_many( 'cds' => 'Test::Reaction::DB::Cd' );
80 package Test::Reaction::DB::Cd;
82 use base 'DBIx::Class';
85 has 'cdid' => ( isa => 'Int', is => 'ro', required => 1 );
87 ( isa => 'Test::Reaction::DB::Artist', is => 'rw', required => 1 );
88 has 'title' => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
95 __PACKAGE__->load_components(qw/PK::Auto Core/);
96 __PACKAGE__->table('cd');
97 __PACKAGE__->add_columns(qw/ cdid artist title/);
98 __PACKAGE__->set_primary_key('cdid');
99 __PACKAGE__->belongs_to( 'artist' => 'Test::Reaction::DB::Artist' );
100 __PACKAGE__->has_many( 'tracks' => 'Test::Reaction::DB::Track' );
106 package Test::Reaction::DB::Track;
108 use base 'DBIx::Class';
111 has 'trackid' => ( isa => 'Int', is => 'ro', required => 1 );
112 has 'cd' => ( isa => 'Test::Reaction::DB::Cd', is => 'rw', required => 1 );
113 has 'title' => ( isa => 'NonEmptySimpleStr', is => 'rw', required => 1 );
115 __PACKAGE__->load_components(qw/PK::Auto Core/);
116 __PACKAGE__->table('track');
117 __PACKAGE__->add_columns(qw/ trackid cd title/);
118 __PACKAGE__->set_primary_key('trackid');
119 __PACKAGE__->belongs_to( 'cd' => 'Test::Reaction::DB::Cd' );
123 =head3 Reaction attributes
125 See L<Reaction::Types::Core>
129 Reaction will use I<sub display_name> for displaying when there is a 1:Many or
130 Many:Many relation. It will return a suitable text representation.
134 =head3 Create Test::Reaction::Model::Action
136 Still in lib/Test/Reaction, create
140 package Test::Reaction::Model::Action;
144 use Test::Reaction::DB;
146 use aliased 'Reaction::InterfaceModel::Action::DBIC::ActionReflector';
148 my $r = ActionReflector->new;
150 $r->reflect_actions_for( 'Test::Reaction::DB::Artist' => __PACKAGE__ );
151 $r->reflect_actions_for( 'Test::Reaction::DB::Cd' => __PACKAGE__ );
152 $r->reflect_actions_for( 'Test::Reaction::DB::Track' => __PACKAGE__ );
158 Reaction controllers inherit from Reaction::UI::CRUDController, like this:
162 package Test::Reaction::Controller::Artist;
166 use base 'Reaction::UI::CRUDController';
170 model_base => 'Test::Reaction',
171 model_name => 'Artist',
172 action => { base => { Chained => '/base', PathPart => 'artist' } }
179 package Test::Reaction::Controller::Cd;
183 use base 'Reaction::UI::CRUDController';
187 model_base => 'Test::Reaction',
189 action => { base => { Chained => '/base', PathPart => 'cd' } }
196 package Test::Reaction::Controller::Track;
200 use base 'Reaction::UI::CRUDController';
204 model_base => 'Test::Reaction',
205 model_name => 'Track',
206 action => { base => { Chained => '/base', PathPart => 'track' } }
211 Finally, change Controller/Root.pm to
213 package Test::Reaction::Controller::Root;
217 use base 'Reaction::UI::RootController';
220 use aliased 'Reaction::UI::ViewPort';
221 use aliased 'Reaction::UI::ViewPort::ListView';
222 use aliased 'Reaction::UI::ViewPort::ActionForm';
224 __PACKAGE__->config->{namespace} = '';
226 sub base :Chained('/') :PathPart('') :CaptureArgs(0) {
229 $self->push_viewport(ViewPort, layout => 'xhtml');
232 sub root :Chained('base') :PathPart('') :Args(0) {
235 $self->push_viewport(ViewPort, layout => 'index');
242 View/XHTML.pm looks like this
244 package Test::Reaction::View::XHTML;
248 extends 'Reaction::UI::Renderer::XHTML';
252 This is all the perly stuff. Now return to the base Test-Reaction directory and
257 main_block = 'index';
261 %]<p><a href="[% ctx.uri_for('/artist') %]">artist</a></p>
262 <p><a href="[% ctx.uri_for('/cd') %]">cd</a></p>
263 <p><a href="[% ctx.uri_for('/track') %]">track</a></p>[%
271 Now all that remains is to tell Catalyst about the root and the model. Let
272 test_reaction.yml look like this:
278 window_title: 'Reaction Test App'
279 Model::Test::Reaction:
280 schema_class: 'Test::Reaction::DB'
282 - 'dbi:SQLite:dbname=database/example.db'
284 The finals step for this example is to link to Reaction's templates:
286 ln -s <path to reaction install directory>/root/base/ root/base
288 At last you're now ready to run the server
290 script/test_reaction_server.pl
298 See L<Reaction::Class> for authors.
302 See L<Reaction::Class> for the license.