[Jifty-commit] r4761 - in jifty/branches/jquery: .
lib/Jifty/Plugin/SQLQueries t/TestApp-Plugin-REST/t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sat Dec 22 05:19:37 EST 2007
Author: clkao
Date: Sat Dec 22 05:19:31 2007
New Revision: 4761
Modified:
jifty/branches/jquery/ (props changed)
jifty/branches/jquery/lib/Jifty/Plugin/REST/Dispatcher.pm
jifty/branches/jquery/lib/Jifty/Plugin/SQLQueries/View.pm
jifty/branches/jquery/t/TestApp-Plugin-REST/t/02-basic-use.t
Log:
merge down for jquery branch.
Modified: jifty/branches/jquery/lib/Jifty/Plugin/REST/Dispatcher.pm
==============================================================================
--- jifty/branches/jquery/lib/Jifty/Plugin/REST/Dispatcher.pm (original)
+++ jifty/branches/jquery/lib/Jifty/Plugin/REST/Dispatcher.pm Sat Dec 22 05:19:31 2007
@@ -35,6 +35,8 @@
on PUT '/=/model/*/*/*' => \&replace_item;
on DELETE '/=/model/*/*/*' => \&delete_item;
+on GET '/=/search/*/**' => \&search_items;
+
on GET '/=/action/*' => \&list_action_params;
on GET '/=/action' => \&list_actions;
on POST '/=/action/*' => \&run_action;
@@ -56,19 +58,22 @@
print qq{
Accessing resources:
-on GET /=/model list models
-on GET /=/model/<model> list model columns
-on GET /=/model/<model>/<column> list model items
-on GET /=/model/<model>/<column>/<key> show item
-on GET /=/model/<model>/<column>/<key>/<field> show item field
-
-on POST /=/model/<model> create item
-on PUT /=/model/<model>/<column>/<key> update item
-on DELETE /=/model/<model>/<column>/<key> delete item
-
-on GET /=/action list actions
-on GET /=/action/<action> list action params
-on POST /=/action/<action> run action
+on GET /=/model list models
+on GET /=/model/<model> list model columns
+on GET /=/model/<model>/<column> list model items
+on GET /=/model/<model>/<column>/<key> show item
+on GET /=/model/<model>/<column>/<key>/<field> show item field
+
+on POST /=/model/<model> create item
+on PUT /=/model/<model>/<column>/<key> update item
+on DELETE /=/model/<model>/<column>/<key> delete item
+
+on GET /=/search/<model>/<c1>/<v1>/<c2>/<v2>/... search items
+on GET /=/search/<model>/<c1>/<v1>/.../<field> show matching items' field
+
+on GET /=/action list actions
+on GET /=/action/<action> list action params
+on POST /=/action/<action> run action
Resources are available in a variety of formats:
@@ -436,6 +441,73 @@
map {$_->name} $rec->columns});
}
+=head2 search_items $model, [c1, v1, c2, v2, ...] [, $field]
+
+Loads up all models of type C<$model> that match the given columns and values.
+If the column and value list has an odd count, then the last item is taken to
+be the output column. Otherwise, all items will be returned.
+
+Will throw a 404 if there were no matches, or C<$field> was invalid.
+
+=cut
+
+sub search_items {
+ my ($model, $fragment) = (model($1), $2);
+ my @pieces = grep {length} split '/', $fragment;
+ my @orig = @pieces;
+
+ # if they provided an odd number of pieces, the last is the output column
+ my $field;
+ if (@pieces % 2 == 1) {
+ $field = pop @pieces;
+ }
+
+ # limit to the key => value pairs they gave us
+ my $collection = eval { $model->collection_class->new }
+ or abort(404);
+
+ # no pieces? they must be asking for everything
+ if (!@pieces) {
+ $collection->unlimit;
+ }
+
+ while (@pieces) {
+ my $column = shift @pieces;
+ my $value = shift @pieces;
+
+ $collection->limit(column => $column, value => $value);
+ }
+
+ $collection->count or abort(404);
+
+ # output
+ if (defined $field) {
+ my $item = $collection->first;
+
+ # make sure $field exists and is a real column
+ $item->can($field) or abort(404);
+ $item->column($field) or abort(404);
+
+ my @values;
+
+ # collect the values for $field
+ do {
+ push @values, $item->$field;
+ } while $item = $collection->next;
+
+ outs(
+ ['search', $model, @orig],
+ \@values,
+ );
+ }
+ else {
+ outs(
+ ['search', $model, @orig],
+ $collection->jifty_serialize_format,
+ );
+ }
+}
+
=head2 create_item
Implemented by redispatching to a CreateModel action.
Modified: jifty/branches/jquery/lib/Jifty/Plugin/SQLQueries/View.pm
==============================================================================
--- jifty/branches/jquery/lib/Jifty/Plugin/SQLQueries/View.pm (original)
+++ jifty/branches/jquery/lib/Jifty/Plugin/SQLQueries/View.pm Sat Dec 22 05:19:31 2007
@@ -108,9 +108,28 @@
ol {
li { $_ } for @$bindings;
}
- h5 { "Stack trace:" }
- pre {
- $misc->{SQLQueryPlugin};
+
+ my $more = Jifty->web->serial;
+ div {
+ attr {
+ class => "extra",
+ style => "display: none;",
+ id => $more,
+ };
+ h5 { "Stack trace:" }
+ pre {
+ $misc->{SQLQueryPlugin};
+ }
+ }
+
+ div {
+ a {
+ attr {
+ href => "#",
+ onclick => "Effect.toggle(\$('$more'),'blind'); this.innerHTML = this.innerHTML == 'more...' ? 'less...' : 'more...'; return false;",
+ };
+ "more..."
+ }
}
};
Modified: jifty/branches/jquery/t/TestApp-Plugin-REST/t/02-basic-use.t
==============================================================================
--- jifty/branches/jquery/t/TestApp-Plugin-REST/t/02-basic-use.t (original)
+++ jifty/branches/jquery/t/TestApp-Plugin-REST/t/02-basic-use.t Sat Dec 22 05:19:31 2007
@@ -13,7 +13,7 @@
use lib 't/lib';
use Jifty::SubTest;
-use Jifty::Test tests => 70;
+use Jifty::Test tests => 78;
use Jifty::Test::WWW::Mechanize;
my $server = Jifty::Test->make_server;
@@ -81,6 +81,24 @@
# on PUT '/=/model/*/*/*' => \&replace_item;
# on DELETE '/=/model/*/*/*' => \&delete_item;
+# on GET '/=/search/*/**' => \&search_items;
+$mech->get_ok('/=/search/user/id/1.yml');
+my $content = get_content();
+is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef }]);
+
+$mech->get_ok('/=/search/user/id/1/name/test.yml');
+$content = get_content();
+is_deeply($content, [{ name => 'test', email => 'test at example.com', id => 1, tasty => undef }]);
+
+$mech->get_ok('/=/search/user/id/1/name/test/email.yml');
+$content = get_content();
+is_deeply($content, ['test at example.com']);
+
+$mech->get('/=/search/Usery/id/1.yml');
+is($mech->status,'404');
+
+$mech->get('/=/search/user/id/1/name/foo.yml');
+is($mech->status,'404');
# on GET '/=/action' => \&list_actions;
More information about the Jifty-commit
mailing list