I wanted to create a Rails Concern which can accept arguments from the parent and/or it’s inherited sub-classes.
Most examples I came across online were simple Concerns which didn’t take any parameters or arguments. So here’s how I went about creating one. Let’s call it Arguable for the sake of this blog post.
Implementation:
moduleArguableextendActiveSupport::ConcernmoduleClassMethodsattr_reader:arguable_optsprivatedefarguable(opts={})@arguable_opts=optsendenddefdo_somethingopts=get_arguable_optsexcluded_attrs=opts[:except]||[]included_attrs=opts[:only]||[]custom_attrs=opts[:custom]||{}#Do something with the attrs hereputsoptsenddefget_arguable_optsifself.class.arguable_opts.blank?&&self.class.superclass.arguable_opts.present?opts=self.class.superclass.arguable_optselsifself.class.arguable_opts.present?&&self.class.superclass.arguable_opts.present?opts=self.class.superclass.arguable_opts.merge(self.class.arguable_opts)elseopts=self.class.arguable_optsendopts||{}endend
PGError: ERROR: encoding UTF8 does not match locale en_US
DETAIL: The chosen LC_CTYPE setting requires encoding LATIN1.
You are here probably because you encountered an error like the one above. After a bit of searching I found and tried various different methods from random articles and gists. Although none worked for me as well as what I’m about to document. Which is why I’m blogging about this for future reference.
Although before we proceed, a word of caution - Backup your database. This has NOT been tried in production so use at your own risk!
2) Create a custom rule to validate URLs based on the RegEx by Diego Perini found here:
ko.validation.rules['url']={validator:function(val,required){if(!val){return!required}val=val.replace(/^\s+|\s+$/,'');//Strip whitespace//Regex by Diego Perini from: http://mathiasbynens.be/demo/url-regexreturnval.match(/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i);},message:'This field has to be a valid URL'};ko.validation.registerExtenders();
3) You are all set. You can now use it either one of these ways:
If your web app is sending emails, you will need to have these three DNS records configured properly to ensure reliable email delivery and avoid ending up in spam/junk folders:
PTR - It’s a pointer to a canonical name. Unlike a CNAME, DNS processing does NOT proceed but it just resolves an IP address to a fully-qualified domain name (FQDN). So just the name is returned. It’s also known as a Reverse DNS Record.
The most common use is for implementing reverse DNS lookups to check if the server name is actually associated with the IP address from where the connection was initiated. Here’s what it would look like:
SPF - Sender Policy Framework (SPF) is an email validation system designed to prevent email spam by detecting email spoofing, a common vulnerability, by verifying sender IP addresses.
Microsoft has a great 4 step wizard to guide you through creating a SPF record.
If you have a Google Apps domain and you are using a 3rd party email delivery service such as SendGrid, here’s what it would typically look like:
"v=spf1 a mx include:_spf.google.com include:sendgrid.net ~all"
without Google Apps:
"v=spf1 a mx include:sendgrid.net ~all"
DKIM - DomainKeys Identified Mail (DKIM) is a method for associating a domain name with an email message, thereby allowing a person, role, or organization to claim some responsibility for the message. The association is set up by means of a digital signature which can be validated by recipients.
This is usually set by the email delivery service you are using. Checkout their account settings to make sure its turned on.