Created
August 7, 2012 15:15
-
-
Save mowat27/3286275 to your computer and use it in GitHub Desktop.
Rails ResourceModel
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
# Extracted from Matt Yoho's (https://github.com/mattyoho) talk at Scottish Ruby Conference 2012 | |
# Exploiting the Resource Idiom - https://speakerdeck.com/u/mattyoho/p/exploiting-the-resource-idiom | |
# | |
module ResourceModel | |
def self.inherited(inheritor) | |
inheritor.class_eval do | |
include ActiveModel::Naming | |
include ActiveModel::Validations | |
include ActiveModel::Translation | |
end | |
end | |
attr_reader :attributes, :errors | |
def initialize(attributes = {}) | |
@attributes = attributes | |
@errors = ActiveModel::Errors.new(self) | |
end | |
def to_key; nil end | |
def to_param; nil end | |
def to_partial_path; "" end | |
def valid? | |
errors.empty? | |
end | |
def persisted?; false end | |
def read_attribute_for_validation(key) | |
@attributes[key] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment