Skip to content

Commit

Permalink
Add custom serializers for xml and json.
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyken committed Dec 27, 2013
1 parent a740bf5 commit c3de3a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
8 changes: 8 additions & 0 deletions app/models/users/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,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.
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/models/users/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 4 additions & 5 deletions spec/routing/users_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -81,4 +81,3 @@
end
end
end

0 comments on commit c3de3a2

Please sign in to comment.