Skip to content

Instantly share code, notes, and snippets.

@Papillard
Created July 17, 2013 14:23
Show Gist options
  • Select an option

  • Save Papillard/6020987 to your computer and use it in GitHub Desktop.

Select an option

Save Papillard/6020987 to your computer and use it in GitHub Desktop.
include ActiveModel mixin to use ActiveRecord validations & helpers
# Including ActiveModel validations & conversion
class SupportRequest
include ActiveModel::Validations
include ActiveModel::Conversion
attr_accessor :name, :email, :problem
validates_presence_of :name, :email, :problem
def initialize(attributes={})
self.name = attributes[:name]
self.email = attributes[:email]
self.problem = attributes[:problem]
end
# SupportRequest never persisted in the DB
def persisted?
false
end
end
# "Stupid" ruby model without ActiveRecord functionality (e.g. validations, to_param/to_path conversion for helpers..)
class SupportRequest
attr_accessor :name, :email, :problem
def initialize(attributes={})
self.name = attributes[:name]
self.email = attributes[:email]
self.problem = attributes[:problem]
end
def valid? # Poor validation
if self.name.blank? || self.email.blank? || self.problem.blank?
return false
end
return true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment