Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooreniemi/4bada621c9a2f066e3b9 to your computer and use it in GitHub Desktop.
Save mooreniemi/4bada621c9a2f066e3b9 to your computer and use it in GitHub Desktop.
activerecord pattern example 1
def update
@model = Model.find(params[:id]) # ActiveRecord's find method being used here
@model.update_attributes(params) # ActiveRecord's update_attributes method being used here
if @model.save # ActiveRecord's save method being used here
respond_with(@model)
else
respond_with(@model.errors)
end
end
repo = UserRepository.new # NOT in the inheritance chain for user
user = repo.find(id: 1) # the returned object has no idea how to find itself or others
admins = repo.find(role: 'admin')
repo.update(user, role: 'admin')
before(:each) do
expect(Model).to receive(:find).and_return(build(:model))
expect(Model).to receive(:update_attributes).and_return(build(:model))
expect(Model).to receive(:save).and_return(true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment