Permalink
Browse files

Add custom serializers for xml and json.

  • Loading branch information...
1 parent a7fedbb commit cf26a04b356ad2161c4c6160260eb870a3de5328 @steveyken steveyken committed Dec 27, 2013
Showing with 27 additions and 6 deletions.
  1. +8 −0 app/models/users/user.rb
  2. +1 −1 config/routes.rb
  3. +14 −0 spec/models/users/user_spec.rb
  4. +4 −5 spec/routing/users_routing_spec.rb
View
@@ -147,6 +147,14 @@ def group_ids=(value)
super(value)
end
+ def to_json(options = nil)
+ [name].to_json
+ end
+
+ def to_xml(options = nil)
+ [name].to_xml
+ end
+
private
# Suspend newly created user if signup requires an approval.
View
@@ -137,7 +137,7 @@
end
end
- resources :users, :id => /\d+/ do
+ resources :users, :id => /\d+/, :except => [:index, :destroy] do
member do
get :avatar
get :password
@@ -209,4 +209,18 @@
@user.single_access_token.should == "token"
end
end
+
+ describe "serialization" do
+
+ let(:user) { FactoryGirl.build(:user) }
+
+ it "to json" do
+ expect(user.to_json).to eql([user.name].to_json)
+ end
+
+ it "to xml" do
+ expect(user.to_xml).to eql([user.name].to_xml)
+ end
+
+ end
end
@@ -8,8 +8,8 @@
describe UsersController do
describe "routing" do
- it "recognizes and generates #index" do
- { :get => "/users" }.should route_to(:controller => "users", :action => "index")
+ it "doesn't recognize #index" do
+ { :get => "/users" }.should_not be_routable
end
it "recognizes and generates #new as /signup" do
@@ -40,8 +40,8 @@
{ :put => "/opportunities/aaron" }.should_not be_routable
end
- it "recognizes and generates #destroy" do
- { :delete => "/users/1" }.should route_to(:controller => "users", :action => "destroy", :id => "1")
+ it "doesn't recognize #destroy" do
+ { :delete => "/users/1" }.should_not be_routable
end
it "doesn't recognize #destroy with non-numeric id" do
@@ -81,4 +81,3 @@
end
end
end
-

0 comments on commit cf26a04

Please sign in to comment.