Skip to content

Instantly share code, notes, and snippets.

@simsalabim
Last active December 28, 2015 07:49
Show Gist options
  • Select an option

  • Save simsalabim/7466815 to your computer and use it in GitHub Desktop.

Select an option

Save simsalabim/7466815 to your computer and use it in GitHub Desktop.
Making pickle find models deleted with paranoid.
# https://github.com/ianwhite/pickle
# https://github.com/goncalossilva/acts_as_paranoid
#
# Given you created a 'deleted' factory
# And then you want to match it in steps with #{capture_model} like followiwng:
#
# Given a deleted user "Turanga Leela" exists
# Then /^(?:|I )select #{capture_model} from the table$/ do |user|
# model!(user)
# end
#
# Then normally model!(user) will fail with an exception
# Overriding pickle to make model!(deleted_user) and find_model!(deleted_user) find the deleted user's factory
Before('@pickle_as_paranoid') do
module PickleAsParanoid
class ActiveRecord::Base
module PickleAdapter
def self.get_model(klass, id)
klass.respond_to?(:with_deleted) ? klass.with_deleted.find(id) : klass.find(id)
end
def self.find_first_model(klass, conditions)
klass.respond_to?(:with_deleted) ? klass.with_deleted.find(:first, conditions: conditions) : klass.find(:first, conditions: conditions)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment