lib/classy_resources.rb in giraffesoft-classy_resources-0.2.1 vs lib/classy_resources.rb in giraffesoft-classy_resources-0.3.0
- old
+ new
@@ -10,31 +10,14 @@
def define_resource(*options)
ResourceBuilder.new(self, *options)
end
- def load_collection(resource, parent = nil)
- parent.nil? ? load_shallow_collection(resource) : load_nested_collection(resource, parent)
+ def collection_url_for(resource, format)
+ "/#{resource}.#{format}"
end
- def create_object(resource, object_params, parent = nil)
- parent.nil? ? create_shallow_object(resource, object_params) : create_nested_object(resource, object_params, parent)
- end
-
- def load_parent_object(parent)
- load_object(parent, params[parent_id_name(parent)])
- end
-
- def parent_id_name(parent)
- :"#{parent.to_s.singularize}_id"
- end
-
- def collection_url_for(resource, format, parent = nil)
- parent = parent.nil? ? "" : "/#{parent}/:#{parent_id_name(parent)}"
- [parent, "/#{resource}.#{format}"].join
- end
-
def object_route_url(resource, format)
"/#{resource}/:id.#{format}"
end
def object_url_for(resource, format, object)
@@ -73,26 +56,31 @@
end
end
protected
def define_collection_get(resource, format)
- parent = options[:parent]
- get collection_url_for(resource, format, parent) do
+ get collection_url_for(resource, format) do
set_content_type(format)
- serialize(load_collection(resource, parent), format)
+ serialize(load_collection(resource), format)
end
end
def define_collection_post(resource, format)
- parent = options[:parent]
- post collection_url_for(resource, format, parent) do
+ post collection_url_for(resource, format) do
set_content_type(format)
- object = create_object(resource, params[resource.to_s.singularize] || {}, parent)
+ object = build_object(resource, params[resource.to_s.singularize] || {})
- response['location'] = object_url_for(resource, format, object)
- response.status = 201
- serialize(object, format)
+ if object.valid?
+ object.save
+
+ response['location'] = object_url_for(resource, format, object)
+ response.status = 201
+ serialize(object, format)
+ else
+ response.status = 422
+ serialize(object.errors, format)
+ end
end
end
def define_member_get(resource, format)
get object_route_url(resource, format) do
@@ -105,10 +93,17 @@
def define_member_put(resource, format)
put object_route_url(resource, format) do
set_content_type(format)
object = load_object(resource, params[:id])
update_object(object, params[resource.to_s.singularize])
- serialize(object, format)
+
+ if object.valid?
+ object.save
+ serialize(object, format)
+ else
+ response.status = 422
+ serialize(object.errors, format)
+ end
end
end
def define_member_delete(resource, format)
delete object_route_url(resource, format) do