My DropThot project will primarily be driven by a "like" system. Thots will be ordered by likes and by time-span, and comments on thots will also be ranked by likes. I know there are gems out there that implements this (e.g. act_as_votable), but for learning purposes I'm coding everything by hand.
I thought implementing likes would be very straight forward, but it's a little bit more complicated than I thought. In my case, I'm using a polymorphic association for my Like model because I need to implement likes for both thots and comments. I'm sure there are other ways to do it, such as a Likable class/model that the Thot and Comment models inherits from, but I think this is my preferred way of doing it.
Here's how my Like spec looks like so far:
describe Like do
let(:user) { FactoryGirl.create(:user) }
let(:other) { FactoryGirl.create(:user) }
let(:thot) { other.thots.build(content: "This is an interesting thot.") }
before { @like = user.likes.build(likable: thot) }
subject { @like }
it { should be_valid }
it { should respond_to :likable }
it { should respond_to :likable_type }
it { should respond_to :user }
it { should respond_to :user_id }
its(:user) { should == user }
its(:likable) { should == thot }
describe "liking your own thot" do
before do
own_thot = user.thots.build(content: "This is my own thot.")
@like = user.likes.build(likable: own_thot)
end
it { should_not be_valid }
end
end
I initially ran into a problem with FactoryGirl with creating multiple users (since users cannot have same username or email). A quick google helped solve this.
Instead of:
factory :user do
name "Alex"
email "alex@ucla.edu"
password "uclabruins"
password_confirmation "uclabruins"
end
Do:
factory :user do
sequence(:name) { |n| "Alex#{n}" }
sequence(:email) { |n| "alex#{n}@ucla.edu" }
password "uclabruins"
password_confirmation "uclabruins"
end
The sequence method in factories.rb makes it so that you can continue creating new users via FactoryGirl without running into validation problems.
This was just a quick blog post to help me outline my thinking process and to jog my brain for next steps; which will be to finish the like mechanism and the proper buttons. After that's done, it should open the doors to the rest of my app's functionality.
No comments:
Post a Comment