Skip to content

Instantly share code, notes, and snippets.

@lareb
Created April 18, 2016 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lareb/16ef3093d3d4e21d58f3f8c9d8a4ac17 to your computer and use it in GitHub Desktop.
Save lareb/16ef3093d3d4e21d58f3f8c9d8a4ac17 to your computer and use it in GitHub Desktop.
class MyController
include One
def create
params = {first_name: "lareb", last_name: "nawab", email: "lareb.indore@gmail.com",
mobile: "8087036184", address_line_1: "2171 Parmar nagar 5", address_line_2: "Fatima Nagar",
city: "Pune", state: "Maharashtra", country: "India", zip: "411013"}
#this will return an array of validation message
validate(params)
end
end
module One
def validate(params)
cannot_empty = [:first_name, :last_name, :email]
email_validation = [:email]
zip_validation = [:zip]
error_messages = []
cannot_empty.each do |attr|
#attr has :first_name, :last_name, :email values
error_messages << cannot_be_empty(params[attr])
end
email_validation.each do |attr|
#attr has :email value
error_messages << get_email_validation(params[attr])
end
zip_validation.each do |attr|
#attr has :email value
error_messages << get_zip_validation(params[attr])
end
end
def cannot_be_empty(attr)
return "#{attr} can not be blank?" if attr.blank?
end
def get_email_validation(attr)
return "#{attr} can not be blank?" if attr.valid? #copy this valid function to regex email validation
end
def get_zip_validation(attr)
return "#{attr} can not be blank?" if attr.valid? #must be numeric and min 5 digit long
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment