Pimp my code? Using self.new in class methods, brittle desi

It’s way past midnight and I’m about to go to sleep. I’m having
doubts about my code now that I’m trying to test as I go.

Now my model follows the pattern of the LineItem model in the rails
book.

Basically the User has one Group and this one Group has many Friends
(polymorphic two-way as “befriender” and “befriended”–users).

from Friend.rb (here the “befriender” and “befriended” that are passed
into the model are User objects)

def self.befriend_user(befriender, befriended)
unless befriender.is_friends_with(befriended)
friend = self.new
friend.befriender = befriender
friend.befriended = befriended
befriender.group.friends << friend
friend
else
return
end
end

Should I be breaking this up into smaller methods? Is there a better
way to do it? Is my design brittle or convoluted?

How would I write a Unit Test for the above?

Really stupid questions: why do I need the “friend” right before the
else?

I know these are a lot of questions but I really can’t find any good
information out there about good design patterns for an integrated
rails MVC/TDD approach so a few pointers would be appreciated.

Cheers,

Sam

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs