Created
June 12, 2012 15:50
-
-
Save saleandro/2918335 to your computer and use it in GitHub Desktop.
Describing how I use ActiveRecord models as Data Access objects.
This file contains hidden or 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
| module Models::Storage | |
| class Artist < ActiveRecord::Base | |
| validates_presence_of :name | |
| end | |
| end | |
| --- | |
| module Models | |
| class Artist | |
| def self.search(query) | |
| stored = storage.find_by_name(query) | |
| new(stored) | |
| end | |
| def self.allowed_attributes | |
| ['name'] | |
| end | |
| def initialize(stored) | |
| @stored = stored | |
| end | |
| def update(data) | |
| data = data.reject {|k, v| !self.class.allowed_attributes.include?(k.to_s) } | |
| return true if data.empty? | |
| @stored.update_attributes(data) | |
| end | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment