Skip to content

Instantly share code, notes, and snippets.

@mowat27
Created August 7, 2012 15:15
Show Gist options
  • Save mowat27/3286275 to your computer and use it in GitHub Desktop.
Save mowat27/3286275 to your computer and use it in GitHub Desktop.
Rails ResourceModel
# 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