Skip to content

Instantly share code, notes, and snippets.

@kerrizor
Created March 21, 2014 00:37
Show Gist options
  • Save kerrizor/9677113 to your computer and use it in GitHub Desktop.
Save kerrizor/9677113 to your computer and use it in GitHub Desktop.
ShippingModel < AR::Base
before_validation :validate_address
after_save :set_asp_sr_id
def initialize(opts)
super
@validator = opts[:validator] || AddressValidator
end
private
def validate_address
@validator.validate_address address
end
end
class AddressValidator
def self.validate_address address, validator = UPSValidator.new
validator.get_suggestions address
end
end
@stim371
Copy link

stim371 commented May 9, 2014

This type of thing came up for me again where I essentially want to inject in a fake class the an API wrapper that:

  • returns stock sample data in development/test
  • can raise an exception in test

How would I blanket inject it in development mode? I understand that it shouldn't have environment-aware stuff in the model (Rails.env.development?), but that kind of switch has to go somewhere, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment