For friends I ended up writing a find_by_sql helper method to get all
friends whether invitor or invitee:
def friends(state = 'active')
query = "SELECT users.* FROM users,friendships WHERE ((users.id =
friendships.invitee_id AND friendships.invitor_id = ?) OR (users.id =
friendships.invitor_id AND friendships.invitee_id = ?))"
if state == 'all'
User.find_by_sql([query, id, id])
else
query += " AND friendships.state = ?"
User.find_by_sql([query, id, id, state])
end
end
Annoyingly specific and brittle but I couldn't think of any other way
to avoid making two db calls and then having to filter out duplicates.
On Jan 7, 2:56 pm, atmorell <[email protected]> wrote:
> Kevin,
>
> How did you create the friends method? I got it working by hacking
> around, but I am wondering how you accomplished the same result.
>
> User model:
>
> has_many :friend_invites, :class_name => "Friendship", :foreign_key
> => :invitee_id, :dependent => :destroy
> has_many :friend_invitors, :class_name => "Friendship", :foreign_key
> => :invitor_id, :dependent => :destroy
>
> has_many :accepted_invites, :class_name =>
> "Friendship", :foreign_key => "invitee_id", :conditions => ['state
> = ?', "active"]
> has_many :accepted_invitors, :class_name =>
> "Friendship", :foreign_key => "invitee_id", :conditions => ['state
> = ?', "active"]
> def friends
> accepted_invites + accepted_invitors
> end
>
> Application:
>
> <def tag="invite-button">
> <% target = this %>
> <do with="&Friendship.new">
> <form controller="friendships" lifecycle="invite" unless="&target ==
> current_user or current_user.friends">
> <input type="hidden" name="friendship[invitee_id]" value="#
> {target.id}" />
> <submit label="Request as Friend" />
> </form>
> </do>
> </def>
>
> Works great by the way :)
>
> Owen,
> Ofcourse I will create a recipe when I got it working and tweaked. So
> far everything I got is in this thread :) You can invite other users
> with a button thanks to Kevin. Creating the other functions should be
> straight forward with the great examples.
>
> Best regards.
> Asbjørn Morell
>
> On Jan 7, 8:52 pm, Owen Dall <[email protected]> wrote:
>
> > Asbjorn,
>
> > Would you mind posting your final working code as comments to the recipe?
> > That may help others in the future.
>
> > Thanks,
>
> > Owen
>
> > On Thu, Jan 7, 2010 at 2:03 PM, kevinpfromnm <[email protected]> wrote:
> > > Just associations. Friendship belongs_to two users, invitee and
> > > invitor.
>
> > > has_many :friend_invites, :class_name => "Friendship", :foreign_key
> > > => :invitee_id, :dependent => :destroy
> > > has_many :friend_invitors, :class_name => "Friendship", :foreign_key
> > > => :invitor_id, :dependent => :destroy
>
> > > On Jan 7, 4:54 am, atmorell <[email protected]> wrote:
> > > > Hello Kevin,
>
> > > > Thank you so much for your example. This is so usefull. Could you
> > > > explain to me how you created the friends and friend_invites methods?
> > > > At the moment my user has no relation to the friendship module.
>
> > > > NoMethodError in UsersController#show
>
> > > > undefined method `friends' for #<User:0x4356430>
>
> > > > Best regards.
> > > > Asbjørn Morell
>
> > > > On Jan 6, 9:28 pm, kevinpfromnm <[email protected]> wrote:
>
> > > > > You have to tweak it more than just the putting in the invite-form...
> > > > > when I did it, I ended up making an invite button that was the form
> > > > > with the context (user) as a hidden id sent with. This is what I had
> > > > > in my friendship taglib for a particular app:
>
> > > > > <def tag="invite-button">
> > > > > <% target = this %>
> > > > > <do with="&Friendship.new">
> > > > > <form controller="friendships" lifecycle="invite" unless="&target ==
> > > > > current_user or current_user.friends('all').include?(target)">
> > > > > <input type="hidden" name="friendship[invitee_id]" value="#
> > > > > {target.id}" />
> > > > > <submit label="Request as Friend" />
> > > > > </form>
> > > > > </do>
> > > > > </def>
> > > > > <def tag="card" for="Friendship" attrs="parent">
> > > > > <% parent ||= @user %>
> > > > > <card merge>
> > > > > <body:>
> > > > > <view:invitee unless="&this.invitee == parent"
> > > />
> > > > > <view:invitor unless="&this.invitor == parent"
> > > />
> > > > > <transition-buttons />
> > > > > </body:>
> > > > > </card>
> > > > > </def>
> > > > > <def tag="friend-requests">
> > > > > <collection:friend_invites.pending>
> > > > > <empty-message:>No pending friend
> > > invites.</empty-message:>
> > > > > <card: parent="¤t_user" />
> > > > > </collection>
> > > > > </def>
> > > > > <def tag="requested-friends">
> > > > > <collection:friend_invitors.pending>
> > > > > <empty-message:>No friend requests
> > > pending.</empty-message:>
> > > > > <card: parent="¤t_user" />
> > > > > </collection>
> > > > > </def>
>
> > > > > On Jan 3, 11:05 am, atmorell <[email protected]> wrote:
>
> > > > > > Hello,
>
> > > > > > The lifecycle tutorial shows how to implement a friendship logic in
> > > > > > the model and controller. However it does not really cover how to
> > > glue
> > > > > > the gui/views together. When I go to /friendships/invite - hobo
> > > > > > presents me with a form with a drop down menu. How do I add a form
> > > > > > to
> > > > > > the user show-page with just one button (Invite) I guess that the
> > > > > > the
> > > > > > user viewed should be in a hidden field?
>
> > > > > > I tried adding the form like this:
>
> > > > > > <extend tag="show-page" for="User">
> > > > > > <old-show-page merge>
> > > > > > <append-content-body:>
> > > > > > <invite-form for="Friendship" />
> > > > > > </append-content-body:>
> > > > > > </old-show-page>
> > > > > > </extend>
>
> > > > > > Hobo ignores the invite-form hmmm I must be missing something.
>
> > > > > > Best regards
> > > > > > Asbjørn Morell
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "Hobo Users" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<hobousers%[email protected]>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/hobousers?hl=en.
>
> > --
> > Thanks,
>
> > Owen
>
> > Owen Dall
> > Barquin International
> > 410-991-0811
>
>
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en.