Validate Uniques in Activerecord
# For a Model where we want to have an unique combo of two fields
# Example: slug/category_id
# In Model class
validates :slug, uniqueness: { scope: :category_id, case_sensitive: false }
# In Migration
add_index :posts, [:slug, :category_id], unique: true
Activerecord Email Validator
# Validator
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || "is not a valid e-mail address")
end
end
end
# In model...
validates :email, email: true
CORS ( Cross-Origin Resource Sharing)
# Hey: This is just a base work, for security reasons make sure you understand what it does.
#
before '/api/*' do
# CORS Headers
headers_list = {
'Access-Control-Allow-Origin' => "*",
'Access-Control-Allow-Headers' => 'Authorization,Accepts,Content-Type,X-CSRF-Token,X-Requested-With',
'Access-Control-Allow-Methods' => 'GET,OPTIONS'
}
headers headers_list
content_type :json
#halt 401, api_repsonse(false, 'Not authorized') unless valid_apikey?()
end
# CORS
options "/api/*" do
response.headers["Allow"] = "HEAD,GET,PUT,DELETE,OPTIONS"
# Needed for AngularJS (?)
response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept"
halt 200
end