Last active
August 29, 2015 14:10
-
-
Save mooreniemi/4bada621c9a2f066e3b9 to your computer and use it in GitHub Desktop.
activerecord pattern example 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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