Classify and Constantize

Metaprogramming! Today I created a variable event that calls a serializer unique to the value of event. Along the way I learned about the Rails methods classify and constantize. classify takes a plural table name and returns a class name. > 'some_things'.classify => "SomeThing" constantize tries to find a constant with the name specified in the argument string. > "Module".constantize => Module > "Nothing".constantize NameError: uninitialized constant Nothing Here’s a sample of how I used these methods today (on the ‘Today I Learned’ app itself): ...

June 3, 2015

Find Or Create By With Block

The Rails find_or_create_by method is great for bringing something into existence only once. I like to use it for database seeds, so you get the basic objects you need to make your development site useable, but don’t keep creating them over and over again. One feature I recently learned is that this method takes a block. User.find_or_create_by(first_name: 'Jake') do |user| user.last_name = 'Worth' end This lets you find the object, and if it doesn’t exist, create it with whatever attributes you need.

May 30, 2015

Multipurpose Environmental Variables

I use HTTP Basic auth in non-production environments because it ships with Rails and is easy to implement. The first time I used it, I set it up like this: # app/controllers/application_controller.rb if ENV['http_basic_auth'] http_basic_authenticate_with name: ENV['username'], password: ENV['password'] end I used environmental variables so that no credentials were hard-coded, and so I could toggle it without deploying. Today I learned you can also implement it like this: # app/controllers/application_controller.rb if creds = ENV['basic_auth_credentials'] username, password = creds.split(':', 2) http_basic_authenticate_with name: username, password: password end This requires an environmental variable called basic_auth_credentials, set to <username>:<password>. I prefer this because it allows one variable to serve two purposes: it toggles the feature and also contains the information the feature needs to work. ...

May 26, 2015

Redirect a Path

To redirect a path in Rails: # config/routes.rb get "/authors" => redirect("/designers") This lets you take a difficult-to-type path name and expose it to your users as a simpler path name: # config/routes.rb get "/login" => redirect("/auth/google_oauth2")

May 11, 2015

Bypass Basic Auth

Rails comes with HTTP Basic authentication built in. Adding this to a controller is how it’s done: # app/controllers/application_controller.rb http_basic_authenticate_with name: 'name', password: 'password' When you visit a URL requiring authentication, you can bypass the authentication screen by adding your username and password to the beginning of the URL. For localhost, this would look like: name:password@localhost:3000

May 8, 2015

Parameters Filtering

Rails logs your server’s activity, which is useful for development and debugging. However, often the server handles sensitive information that should not be logged. A few examples are authentication credentials, personal data, and financial information. To prevent Rails from logging such data, add this to your application configuration: config.filter_parameters << :sensitive_param When the named parameter is handled by the server, it will be logged as [FILTERED] instead of its actual value. Add this configuration by environment if you want to keep the parameters unfiltered in development.

May 2, 2015

Modify Attribute on Set

In this example, we want to allow users to enter a twitter handle with or without an at-sign (’@’), just like Twitter, but save it without the at-sign. twitter_handle is already an attribute on the class. class User < ActiveRecord::Base def twitter_handle=(handle) write_attribute(:twitter_handle, handle.to_s.gsub(/^@/, '')) end end

May 2, 2015

Don’t miss my next essay

Hear from me immediately when I post: no ads, unsubscribe anytime.