Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug with auto-modeling associated objects on a model. Also, som…

…e syntax sugar for passing an asynchronous callback as the 2nd argument to #find.

git-svn-id: https://svn.thoughtbot.com/jester/trunk@111 d7758119-aa2c-0410-afcd-b700fbd0d0b3
  • Loading branch information...
commit cdfb8bb8bac51fe37b9fea8f2db4aa7e9ce1a9df 1 parent 44ab5a6
emill authored
Showing with 62 additions and 22 deletions.
  1. +23 −13 jester.js
  2. +39 −9 test/jester_test.html
View
36 jester.js
@@ -75,11 +75,10 @@ Object.extend(Jester.Resource, {
new_model["_" + opt] = options[opt];
// Establish custom URL helpers
- for (var url in options.urls) {
+ for (var url in options.urls)
eval('new_model._' + url + '_url = function(params) {return this._url_for("' + url + '", params);}');
- }
- if(options.checkNew)
+ if (options.checkNew)
this.buildAttributes(new_model, options.asynchronous);
return new_model;
@@ -169,6 +168,12 @@ Object.extend(Jester.Resource, {
},
find : function(id, params, callback) {
+ // allow a params hash to be omitted and a callback function given directly
+ if (!callback && typeof(params) == "function") {
+ callback = params;
+ params = null;
+ }
+
var findAllWork = bind(this, function(doc) {
if (!doc) return null;
@@ -212,7 +217,7 @@ Object.extend(Jester.Resource, {
},
build : function(attributes) {
- return new this(attributes)
+ return new this(attributes);
},
create : function(attributes, callback) {
@@ -222,12 +227,10 @@ Object.extend(Jester.Resource, {
return callback(base);
});
- if (callback)
- {
+ if (callback) {
return base.save(createWork);
}
- else
- {
+ else {
base.save();
return base;
}
@@ -256,8 +259,7 @@ Object.extend(Jester.Resource, {
return this.request(destroyWork, this._destroy_url(id), {method: "delete"}, callback);
},
- _interpolate: function(string, params)
- {
+ _interpolate: function(string, params) {
var result = string;
for(var val in params) {
var re = new RegExp(":" + val, "g");
@@ -362,7 +364,6 @@ Object.extend(Jester.Resource, {
i += 1;
}
-
// has_many
if (relation[singular] && typeof(relation[singular]) == "object" && i == 1) {
var value = [];
@@ -374,7 +375,12 @@ Object.extend(Jester.Resource, {
elements[plural][singular] = [elements[plural][singular]];
elements[plural][singular].each( bind(this, function(single) {
- if (typeof(eval(name)) == "undefined") Jester.Resource.model(name, {prefix: this._prefix, singular: singular, plural: plural});
+ // if the association hasn't been modeled, do a default modeling here
+ // hosted object's prefix and format are inherited, singular and plural are set
+ // from the XML
+ if (eval("typeof(" + name + ")") == "undefined") {
+ Jester.Resource.model(name, {prefix: this._prefix, singular: singular, plural: plural, format: this._format});
+ }
var base = eval(name + ".build(this._attributesFromTree(single))");
value.push(base);
}));
@@ -384,7 +390,11 @@ Object.extend(Jester.Resource, {
singular = attr;
var name = singular.capitalize();
- if (typeof(eval(name)) == "undefined") Jester.Resource.model(name, {prefix: this._prefix, singular: singular});
+ // if the association hasn't been modeled, do a default modeling here
+ // hosted object's prefix and format are inherited, singular is set from the XML
+ if (eval("typeof(" + name + ")") == "undefined") {
+ Jester.Resource.model(name, {prefix: this._prefix, singular: singular, format: this._format});
+ }
value = eval(name + ".build(this._attributesFromTree(value))");
}
}
View
48 test/jester_test.html
@@ -233,6 +233,12 @@
var eric = User.find(1, {}, {asynchronous: false});
assertEquals('eric', eric.first_name);
+ // test that I can omit the params hash completely if I pass a straight callback
+ User.find(1, function(eric) {
+ assertEquals('eric', eric.first_name);
+ assert_changed();
+ });
+
// test path prefixed find
Resource.model("Post", {prefix: "/users/:user_id"})
var post = Post.find(1, {user_id: 1})
@@ -341,23 +347,47 @@
/********************************************
************ ASSOCIATION TESTS *************
********************************************/
-
-function testRelations() {
+
+function testAssociations() {
+ // has_many
var eric = User.find(1);
+ assertEquals(1, eric._associations.length);
+ assertEquals("posts", eric._associations[0]);
+ assertEquals(Post, eric.posts[0].class);
assertEquals(2, eric.posts.length);
var john = User.find(2);
assertEquals(1, john.posts.length);
- assertEquals("has_many object should have correct name.", "Post", eric.posts[0].class.name);
- assertEquals("has_many object should have correct singular form.", "post", eric.posts[0].class._singular);
- assertEquals("has_many object should have correct plural form.", "posts", eric.posts[1].class._plural);
-
+ // auto-model associations if need be
+ var original_url = Post._show_url();
+ Post = undefined;
+ var eric = User.find(1);
+ assertEquals("posts", eric._associations[0]);
+ assertEquals(Post, eric.posts[0].class);
+ assertEquals(original_url, Post._show_url());
+ assertEquals("post", Post._singular);
+ assertEquals(User._format, Post._format);
+ assertEquals(User._prefix, Post._prefix);
+
+ // belongs_to/has_one
+ Resource.model("Post");
Internet[Post._show_url(1)] = {'post': postIncludeDetails};
var post = Post.find(1);
- assertEquals("has_one object should have correct name.", "User", post.user.class.name);
- assertEquals("has_one object should have correct singular form.", "user", post.user.class._singular);
- assertEquals("has_one object should have correct plural form.", "users", post.user.class._plural);
+ assertEquals(1, post._associations.length);
+ assertEquals("user", post._associations[0]);
+ assertEquals(User, post.user.class)
+
+ // auto-model belongs_to/has_one
+ var original_url = User._show_url();
+ User = undefined;
+ var post = Post.find(1);
+ assertEquals("user", post._associations[0]);
+ assertEquals(User, post.user.class);
+ assertEquals(original_url, User._show_url());
+ assertEquals("user", User._singular);
+ assertEquals(Post._format, User._format);
+ assertEquals(Post._prefix, User._prefix);
}
Please sign in to comment.
Something went wrong with that request. Please try again.