Conditional config.actions.exclude

112 views
Skip to first unread message

nwpa

unread,
May 20, 2007, 1:56:51 PM5/20/07
to ActiveScaffold : Ruby on Rails plugin
I've calculated a variable @n in books_controller.rb and would like to
be able to do something like this:

active_scaffold :book do |config|
config.actions.exclude :create if @n > 5
end

or maybe: config.actions.exclude << :create if @n > 5

Is there a way to apply a test to config.actions.exclude?

Thanks.

Daniel E

unread,
May 21, 2007, 8:29:02 AM5/21/07
to ActiveScaffold : Ruby on Rails plugin

nwpa

unread,
May 21, 2007, 9:38:30 PM5/21/07
to ActiveScaffold : Ruby on Rails plugin
On May 21, 8:29 am, Daniel E <deinspan...@gmail.com> wrote:
> This should get you up and running:http://activescaffold.com/tutorials/per-request-configuration

Thanks for the tip; I had overlooked this. However, a little
experimentation has shown that per-request-configuration works fine on
columns, eg:
active_scaffold_config.list.columns.exclude :title
but unfortunately has no effect on actions, eg:
active_scaffold_config.actions.exclude :create

The code executes without errors but doesn't give the result I'm
after. Relevant parts of my controller are:
- - - - -
include AuthenticatedSystem
before_filter :login_required
before_filter :update_table_config

active_scaffold :library_config do |config|
config.columns =
[:library_name, :barcode_prefix, :starting_barcode, :login_name]
config.create.columns.exclude :login_name
config.actions = [:list, :show, :create, :update, :delete]
config.label = "Library Information and Data"
end

def update_table_config
n = LibraryConfig.count(:conditions => ['login_name = (?)',
[current_user.login]])
if n>0

active_scaffold_config.actions.exclude :create
# < - - - this has no effect

active_scaffold_config.list.columns.exclude :barcode_prefix # <
- - - but this does
else
active_scaffold_config.actions.add :create
end
end
- - - - -

What I want to do is prevent a user who already has a row in the
database from creating a second one. I can enforce this with a
validation but would rather the :create action not be available in the
first place.

nwpa

unread,
May 21, 2007, 9:59:16 PM5/21/07
to ActiveScaffold : Ruby on Rails plugin
On May 21, 9:38 pm, nwpa <ben.bullo...@gmail.com> wrote:
> On May 21, 8:29 am, Daniel E <deinspan...@gmail.com> wrote:
>
> > This should get you up and running:http://activescaffold.com/tutorials/per-request-configuration
>
> Thanks for the tip; I had overlooked this. However, a little
> experimentation has shown that per-request-configuration works fine on
> columns, eg:
> active_scaffold_config.list.columns.exclude :title
> but unfortunately has no effect on actions, eg:
> active_scaffold_config.actions.exclude :create

OK, I posted a little too quickly on this because it does work after
all, but not quite the way I expected. When
active_scaffold_config.actions.exclude :create is applied, the "Create
new" link still appears, but clicking it gives "Request Failed (code
500, Internal Error)". This gets the job done, but I would like to
override this error message with a custom one if possible. Any way to
do that?

Thanks.

Lance Ivy

unread,
May 22, 2007, 12:31:02 PM5/22/07
to actives...@googlegroups.com
Yep, try defining this method on your controller:

class BooksController
  active_scaffold :books

  protected

  def create_authorized?
    LibraryConfig.count(:conditions => ['login_name = (?)', [current_user.login]]) == 0
  end
end

nwpa

unread,
May 22, 2007, 4:39:49 PM5/22/07
to ActiveScaffold : Ruby on Rails plugin
On May 22, 12:31 pm, "Lance Ivy" <l...@cainlevy.net> wrote:
> Yep, try defining this method on your controller:
>
> class BooksController
> active_scaffold :books
>
> protected
>
> def create_authorized?
> LibraryConfig.count(:conditions => ['login_name = (?)',
> [current_user.login]]) == 0
> end
> end

That's what I was looking for! Thanks very much.

oliver

unread,
May 22, 2007, 7:36:11 PM5/22/07
to ActiveScaffold : Ruby on Rails plugin
nwpa,

I'm experiencing the same problem where actions configured per-request
have no effect, only in my case it's persisting. I'm trying

active_scaffold_config.actions =
[:list, :show, :update, :delete, :field_search, :nested, :subform]

and

active_scaffold_config.actions.add :update, :delete

neither work

is there anything you did that made it work?

oliver

unread,
May 24, 2007, 9:45:18 PM5/24/07
to ActiveScaffold : Ruby on Rails plugin
anyone? I still can't get defining actions per request working...

Lance Ivy

unread,
May 24, 2007, 11:25:35 PM5/24/07
to actives...@googlegroups.com
Hi Oliver, did you see my earlier message (May 22) about using the #{action}_authorized? methods?

oliver

unread,
May 25, 2007, 10:48:46 AM5/25/07
to ActiveScaffold : Ruby on Rails plugin
Hi Lance,

Yes, but I didn't think it applied to my case...

to do

if current_user.is_superuser


active_scaffold_config.actions =
[:list, :show, :update, :delete, :field_search, :nested, :subform]

else
active_scaffold_config.actions =
[:list, :show, :field_search, :nested]
end

I would have to define security methods for each of them?

On May 25, 12:25 am, "Lance Ivy" <l...@cainlevy.net> wrote:
> Hi Oliver, did you see my earlier message (May 22) about using the
> #{action}_authorized? methods?
>

Lance Ivy

unread,
May 25, 2007, 10:56:41 AM5/25/07
to actives...@googlegroups.com
Just the ones that change - :update, :delete. Not sure about :subform ...

Lance Ivy

unread,
May 25, 2007, 10:58:31 AM5/25/07
to actives...@googlegroups.com
Oh, and there's nothing wrong with doing stuff like this to keep your code clean:

def delete_authorized?
  current_user.is_superuser?
end

alias update_authorized? delete_authorized?

oliver

unread,
May 25, 2007, 11:02:51 AM5/25/07
to ActiveScaffold : Ruby on Rails plugin
ok, I think I get it now... bettter to use the security methods than
to use the per request config

On May 25, 11:58 am, "Lance Ivy" <l...@cainlevy.net> wrote:
> Oh, and there's nothing wrong with doing stuff like this to keep your code
> clean:
>
> def delete_authorized?
> current_user.is_superuser?
> end
>
> alias update_authorized? delete_authorized?
>

> On 5/25/07, Lance Ivy <l...@cainlevy.net> wrote:
>
>
>
> > Just the ones that change - :update, :delete. Not sure about :subform ...
>

Reply all
Reply to author
Forward
0 new messages