Created
July 17, 2013 14:23
-
-
Save Papillard/6020987 to your computer and use it in GitHub Desktop.
include ActiveModel mixin to use ActiveRecord validations & helpers
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
| # 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 |
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
| # "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