ruby-china/ruby-china — pull request #340 by suffering (2014-08-19 13:25:44+00:00)
Title: topic with favorite/follow status

通过api获取topic时, 为其添加`has_favorited`与`has_followed`两条属性. 用于判断当前用户是否已经`喜欢`或`收藏`此topic.

若用户未登陆其`has_favorited`与`has_followed`皆为`false`.

若用户已登陆, 则判断当前用户是否喜欢/收藏此topic.

测试代码已通过, 如下:
```ruby
it "should get favorite/follow status with current_user" do
t = Factory(:topic, :title => "Have I favorited this topic?")
get "/api/v2/topics/#{t.id}.json"
expect(response.status).to eq(200)
json = JSON.parse(response.body)
puts json["has_followed"]
expect(json["has_followed"]).to eq(false)
expect(json["has_favorited"]).to eq(false)
u = Factory(:user).tap { |u| u.update_private_token }
#like it
post "/api/v2/topics/#{t.id}/favorite.json", :token => u.private_token

get "/api/v2/topics/#{t.id}.json", :token => u.private_token
expect(response.status).to eq(200)
json = JSON.parse(response.body)
expect(json["has_favorited"]).to eq(true)
expect(json["has_followed"]).to eq(false)

#unlike it
post "/api/v2/topics/#{t.id}/favorite.json", :token => u.private_token, :type => 'unfavorite'

#follow it
post "/api/v2/topics/#{t.id}/follow.json", :token => u.private_token

get "/api/v2/topics/#{t.id}.json", :token => u.private_token
expect(response.status).to eq(200)
json = JSON.parse(response.body)
expect(json["has_favorited"]).to eq(false)
expect(json['has_followed']).to eq(true)

get "/api/v2/topics/#{t.id}.json"
expect(response.status).to eq(200)
json = JSON.parse(response.body)
expect(json["has_favorited"]).to eq(false)
expect(json['has_followed']).to eq(false)
end

it "should work when id record found" do
get "/api/topics/-1.json"
expect(response.status).to eq(404)
end
end
```