Francesco De Rose wrote ...

> in my application I have the model User which is managed through AS.
> 
> For obvious reason, I want to prevent the administrator from deleting
> the last user. :-)
> 
> "Agile Development with Rails" suggests, in their example application
> (which of course does not use AS) the following code in the model:
> 
>   def after_destroy
>     raise "Can't destroy the last user"
>   end
> 
> This works in AS, as it effectively prevents the user to be deleted,
> but instead of that error message, I get "Internal Error" in the AS
> view.
> 
> What is the best way to achieve this under AS and to display an
> informative error message to the user? Thank you!

I don't know if this is the best way, but it is one way to do it.

In one of my projects the admin and current user accounts are protected
against deletion by defining the authorized_for_destroy? and
before_destroy methods in the user model (no after_destroy method is
defined):

  def authorized_for_destroy?
    self.login != "admin" && self.id != current_user.id
  end

  def before_destroy
    raise "Cannot delete the admin account." if self.login == "admin"
    raise "Cannot delete the current account." if self.id == current_user.id
  end

Additionally, the destroy method is wrapped in the controller to report
the exceptions:

  def destroy
    begin
      super
    rescue ActiveScaffold::RecordNotAllowed => e
      raise e
    rescue Exception => e
      flash[:warning] = e.message
      render :update do |page|
        page.replace_html active_scaffold_messages_id, :partial => 'messages'
      end
    end
  end

Adjust the authorized_for_destroy? and before_destroy methods for your
situation.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" 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/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to