do not use try in UserSerializer for fields coming from UserProfile

This commit is contained in:
Andrew Bezzub 2014-06-07 12:52:51 -07:00
parent 7db31adf35
commit 0a42901c40
4 changed files with 14 additions and 5 deletions

View File

@ -110,14 +110,14 @@ class UserSerializer < BasicUserSerializer
end end
def location def location
object.user_profile.try(:location) object.user_profile.location
end end
def include_location? def include_location?
location.present? location.present?
end end
def website def website
object.user_profile.try(:website) object.user_profile.website
end end
def include_website? def include_website?
website.present? website.present?

View File

@ -75,4 +75,4 @@ Fabricator(:elder, from: :user) do
username { sequence(:username) { |i| "elder#{i}" } } username { sequence(:username) { |i| "elder#{i}" } }
email { sequence(:email) { |i| "elder#{i}@elderfun.com" } } email { sequence(:email) { |i| "elder#{i}@elderfun.com" } }
trust_level TrustLevel.levels[:elder] trust_level TrustLevel.levels[:elder]
end end

View File

@ -0,0 +1,2 @@
Fabricator(:user_profile) do
end

View File

@ -4,7 +4,7 @@ require_dependency 'user'
describe UserSerializer do describe UserSerializer do
context "with a user" do context "with a user" do
let(:user) { Fabricate.build(:user) } let(:user) { Fabricate.build(:user, user_profile: Fabricate.build(:user_profile) ) }
let(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) } let(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) }
let(:json) { serializer.as_json } let(:json) { serializer.as_json }
@ -32,7 +32,14 @@ describe UserSerializer do
end end
end end
context "with filled out website" do
before do
user.user_profile.website = 'http://example.com'
end
it "has a website" do
expect(json[:website]).to eq 'http://example.com'
end
end
end end
end end