On Sep 13, 2005, at 2:46 PM, Jamis Buck wrote:
>
> On Sep 13, 2005, at 12:42 PM, Kimball Larsen wrote:
>
>> So, in trying to do everything the rails way, I am convinced there
>> must be a better way to do what I''m trying to accomplish with
this
>> line:
>>
>> if @session["user"] != nil and
@session["user"].role != nil and
>> @session["user"].role.name = "admin"
>
> I''d do:
>
> if @session["user"] && @session["user"].role
&&
> @session["user"].role.name == "admin"
>
> But there may be an even more concise way.
>
> - Jamis
You could probably say:
if (session[:user].role.name == ''admin'' rescue false)
Which is "horizontally short" (a big feature in _some_ frameworks), if
not a little smelly.
It seems to me that the role.name == ''admin'' part could be
moved into
the user object such
that you''re left with:
if session[:user] && session[:user].is_admin?
If it''s applicable to your situation, you may also verify
session[:user] in a before_filter
(as is the case with authentication) which could knock out the first
check there.
--
Scott Barron
Lunchbox Software
http://lunchboxsoftware.com
http://lunchroom.lunchboxsoftware.com