Sunday, January 5, 2014

Re: [Rails] Problems testing a destroy failure

On Jan 5, 2014, at 2:06 PM, Clem Rock wrote:

> Hello,
> I'm trying to test an active record object destroy failure but I'm
> having problems creating a failure situation. I have a before_filter
> method called 'require_user_payment_info' which validates the
> @payment_info object before the delete method is called so I can't
> create a 'bad' @payment_info object before the delete method is called.
>
> Here's the require_user_payment_info method:
>
> [code]
> def require_user_payment_info
> @payment_info = credit_card_model.slave.find_by_user_guid(user_guid)
> if !@payment_info || @payment_info.user_guid != user_guid
> redirect_to(:controller => 'store', :action => 'index') and return
> false
> else
> if((@payment_info.card_expires_year.to_i < Date.today.year) ||
> ((@payment_info.card_expires_month.to_i < Date.today.month) &&
> (@payment_info.card_expires_year.to_i == Date.today.year)))
> @payment_info.card_account_public = "" #clear this out so the
> user is forced to re-enter the credit card number
> @payment_info.valid?
> flash.now[:error] = t('ui_flash_errors.card_expired_error')
> end
> end
> end
> [/code]
>
>
> And the actual delete method:
>
> [code]
> def delete

Shouldn't this line be 'def destroy'?

Delete is the method that is called without callbacks to actually remove the record. Destroy calls up the before_destroy handlers and checks the validations.

Here's one ripped from a working application:

#group.rb
before_destroy :group_has_children?
...
private
def group_has_children?
errors.add(:base, "Cannot delete a group with members") unless groupings.count == 0
errors.blank?
end

There's probably a better way to do this, but this was how I settled on it. I know there's a way to do this with validations, too. Probably just this:

validate :group_has_children?, :on => :destroy

def group_has_children?
errors.add(:base, "Cannot delete a group with members") unless groupings.count == 0
end

Walter

> # required to be called via a delete request
> redirect_to :action => 'edit' and return if !request.delete?
> if @payment_info.destroy
> flash[:notice] = "Delete SUCCESSFUL"
> redirect_to :action => 'new'
> else
> flash[:error] = "Delete failed"
> redirect_to :action => 'edit'
> end
> [/code]
>
> Any ideas?
>
> Thanks!
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2106a78b86d97e1320d111ec5f05223e%40ruby-forum.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3F9589C8-F9DF-401F-BDD7-9EA01CDED474%40wdstudio.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate