Programming
7 Reasons Why You Should Treat Your Custom Application Like an Employee
0This is a cross-post from PanopticDev.com
Custom software can be an expensive investment, but if you design it to do a job and invest in it like an employee, you’ll be able to track results better and budget for it accordingly. So when a potential client comes to us looking to have a custom application built, one of the first things we want to know is, what’s its job? Whether it’s going to be a mobile app, a website, an API, an embedded system, a device driver, or whatever, every custom application ultimately works for that client, just like their employees.
Setting up a Rails 5 app from edge
6Update: Many people reported issues with installing the proper version of Rack, so I’ve updated step 3 to account for that.
I just returned from RailsConf 2015 and wanted to checkout some of the new features of Rails 5 that @dhh highlighted in his keynote. Here’s how I got up and running on edge rails in a few quick steps.
1. Create the project directory
1 2 3 | cd ~/Projects/Test mkdir rails-5-edge-test cd rails-5-edge-test |
2. Setup your environment
Rails 5 requires Ruby >= 2.2.2. I’m using RVM in this case, but you can use whatever Ruby environment manager that works for you.
1 | rvm use 2.2.2@rails-5-edge-test --create |
3. Install edge Rails so we can generate a new project
1 2 3 4 5 6 7 8 9 | touch Gemfile cat > Gemfile <<END_CONF source "https://rubygems.org" ruby '2.2.2' gem 'rails', :git => 'git://github.com/rails/rails.git' gem 'rack', :git => 'git://github.com/rack/rack.git' gem 'arel', :git => 'git://github.com/rails/arel.git' END_CONF bundle |
Note that we included both Arel and Rack from source as well. At the time of writing, ActiveRecord requires Arel >= 7.0.0.alpha which isn’t available on RubyGems.org yet, and ActionPack requires Rack >= 2.0 which also isn’t released yet, so these two lines are required to resolve that dependency.
4. Generate the rails app
1 | bundle exec rails new . --dev --force |
The --force
flag will allow Rails to overwrite our Gemfile, and the --dev
flag tells Rails to point to the edge version of Rails that we just bundled.
5. Profit!
1 | bin/rails server |
Bash shortcuts to run Rails commands through Spring, Bundler, or ye olde scripts
0Just about a year ago to date I posted some useful Bash shortcuts for Ruby on Rails commands. These are mix of aliases and functions that detect what version of Rails is present and routes commands which ever way that version expects, i.e. ./script/console
versus rails console
. Now that Rails 4.1 ships with Spring by default, there is yet one more way commands could be routed – through the spring binstubs. I’ve recently updated my shortcuts to account for this. Now my commands will use the binstubs if they are present, or fallback to the other version-dependent methods.
H/T to makandracards for helping me figure this out.
Cache busting AJAX requests and redirects
0AJAX get
and head
requests are cached by default. This can cause problems when AJAX requests use the same URLs as a user would use during normal site navigation. jQuery can disable caching on a global or per-request basis. When doing so, jQuery adds a _={timestamp}
parameter to the query string which should make each request unique. However, there are cases where an AJAX request results in a redirect which jQuery will happily follow. When that happens the _={timestamp}
isn’t automatically appended to the new request, so the final response can still end up being cached by the browser even when you’ve told jQuery not to cache it. You can get around this in 2 ways:
- Make sure all redirects persist the
_={timestamp}
parameter, if present - Add the proper header response parameters to prevent the browser from caching the request.
Here’s an easy way to implement the 2nd solution in Rails:
Dynamic enum fields in nested association forms in Rails Admin
4We’ve had a few projects at Panoptic Development recently that made use of the RailsAdmin gem. Out of the box, RA satisfied about 95% of what we needed to do to complete each project, but there were a few edge requirements that we either had to make do without or find workarounds for. Don’t get me wrong, RA is a wonderful utility for quickly spinning up an administrative area for your Rails app. It’s loaded with really slick bells-and-whistles, and it has an incredibly flexible DSL for configuration. However, there are a few things it just doesn’t seem to be able to handle as-is and for which I’ve searched high and low for solutions for the last 9 months. I’m happy to report that I finally cracked one of these problems, and it wasn’t even all that complicated in the end.