[Hobo Users] New transition - block user

24 views
Skip to first unread message

Scorpio

unread,
Mar 10, 2012, 11:49:39 PM3/10/12
to Hobo Users
Hello,
I want to add to the admin subsite index a block button at the index
but nomatter what I try I get an error of some sort.

This is what I've added to the user lifecycle

state :blocked

and after auto generated transitions

transition :block, { :inactive => :blocked }, :available_to
=> :all,
:params => [:given_id]
transition :block, { :active => :blocked }, :available_to
=> :all,
:params => [:given_id ]
transition :unblock, { :blocked => :inactive }, :available_to
=> :all,
:params => [:given_id]

to the users controller under admin



def block(given_id)

user = User.where(:id=>given_id)
user.state = 'blocked'
flash[:now] ="TEST"
end

def unblock(given_id)
user = User.where(:id=>given_id)
user.state = 'inactive'
end
end

finally the view

<collection:>
<a><name/></a><%=this.state%><%if this.state=='blocked'%>
<transition-button transition="unblock"
params="&{:given_id=>this.id}" /> <%else%> <transition-button
transition="block"/> <%end%>
</collection:>

The goal:

Ability for the admin and him alone to block/unblock users using the
above buttons

The problems

At this point I click the button and I get a redirect but nothing
happens as if nothing ran.

If i remove the params from the transitions I get a no route matches
admin/users error and with the current setup that happens when I use
the user switcher to switch to a user without admin permission (as if
it wanted to block the acting user aka admin?! )

I know this is screwed up bad and Iknow I need to pass a param of the
user id to the transition and set state to "blocked" on a user with
this id I just dunno how exactly.

Also I've modified the user controller in the admin dir to class
Admin::UsersController < Admin::AdminSiteController as the auto
generated one inherited from application controller and that allowed
non admin users in to parts of the subsite.

Btw. On this collection I've lost the nice default hobo formatting and
now I've got a bare link and a system-themed button. How do I bring
back the nice index look ?

Lifecycles where always magical to me. Yes, I read the lifecycle
section in beta 6 book till my eyes bled. Any and all help is much
appreciated. Thanks in advance.

Bryan Larsen

unread,
Mar 11, 2012, 12:48:05 AM3/11/12
to hobo...@googlegroups.com
>        transition :block, { :inactive => :blocked }, :available_to
> => :all,
>                   :params => [:given_id]

params should map to attributes or columns on your model. Behind the
scenes Hobo will do something like user.given_id = params[:given_id].
In this case I don't think you need any params as the model is
already found to get to the transition

>
> to the users controller under admin
>
>  def block(given_id)
>
>    user = User.where(:id=>given_id)
>    user.state = 'blocked'
>    flash[:now] ="TEST"
>  end

It's the do_block action you care about. Block is the action that
renders the block form. But Hobo should be creating a sufficient
block and do_block method for you, as long as you have auto_actions
:all or auto_actions :lifecycle in your controller. You do have one
of those, don't you?

You also aren't saving the user, so no changes are being persisted to
the database.

if you need to add code to your action you can do something like this:

def do_block
flash[:now] ="TEST"
do_transition_action :block
end

Bryan

Scorpio

unread,
Mar 11, 2012, 7:48:08 AM3/11/12
to Hobo Users
My mistake on the save. Still I need some way to pass an ID of the
user to be blocked because it's the admin that's doing the clicking :)
ban wave incoming :)

Bryan Larsen

unread,
Mar 11, 2012, 11:39:44 AM3/11/12
to hobo...@googlegroups.com
Sure, current_user is set to the admin, but the context is the user
being banned, so the transition should still work. After all most of
the time when people use lifecycles they are on objects other than
users, so current_user is obviously irrelevant then.

Bryan

> --
> You received this message because you are subscribed to the Google Groups "Hobo Users" group.
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
>

kevinpfromnm

unread,
Mar 11, 2012, 2:39:39 PM3/11/12
to hobo...@googlegroups.com
One other addition is you probably don't want this available_to all users.  :available_to => "acting_user if acting_user.administrator?"

also, if you want this solely in the admin subsite you probably want :subsite => :admin as well.

Scorpio

unread,
Mar 11, 2012, 6:59:50 PM3/11/12
to Hobo Users
I went along with all your suggestions added acting, user and subsite
params. I also deleted the custom actions hoping the context would fix
it. I deleted the param from the button as well. Still getting

Routing Error

No route matches "/admin/users"

after the redirect same as before. Any ideas ?

Matt Jones

unread,
Mar 12, 2012, 10:38:22 AM3/12/12
to hobo...@googlegroups.com

On Mar 11, 2012, at 6:59 PM, Scorpio wrote:

> I went along with all your suggestions added acting, user and subsite
> params. I also deleted the custom actions hoping the context would fix
> it. I deleted the param from the button as well. Still getting
>
> Routing Error
>
> No route matches "/admin/users"
>
> after the redirect same as before. Any ideas ?

You'll need to specify the redirect path directly, since the default isn't working for you:

class Admin::UsersController < ApplicationController

auto_actions :all

def do_block
do_transition_action :block do
flash[:notice] = 'BANHAMMERED"
redirect_to :someplace
end

end

--Matt Jones

Scorpio

unread,
Mar 13, 2012, 12:05:06 AM3/13/12
to Hobo Users
For the sake if completion:

slight changes to the above:

:subsite => 'admin' not :admin

and

def do_block
do_transition_action :block do
flash[:notice] = 'BANHAMMERED'
redirect_to :back, :status => 303
end

Worked great. Matt said that the 303 is some sort of request bugfix in
1.3.0. Don't ask me :) I'm not that good
Reply all
Reply to author
Forward
0 new messages