Skip to content

Instantly share code, notes, and snippets.

@JeffCohen
Last active August 29, 2015 13:56
Show Gist options
  • Save JeffCohen/9284987 to your computer and use it in GitHub Desktop.
Save JeffCohen/9284987 to your computer and use it in GitHub Desktop.
The ez gem

The EZ gem

The ez gem is intended to help non-programmers during their first few weeks of learning Rails.

Currently only the happiest of all paths have been tested, and as far as I know, it only works on my machine.

  # Add it to your Gemfile
  
  gem 'ez', group: 'development'

1. Automatic Database Migrations and Model Classes

When first starting a project, students tend to change their schema very frequently, and managing migrations tends to be a large stumbling block.

Now, students can simply edit a file named db/models.yml to postpone learning the migration and model generators. Students describe their entire scheme in this file, and can run rake ez:tables anytime to update their database.

The rake task does its best to detect new model definitions, deleted models, new columns, and deleted columns, preserving data as much as it reasonably can.

The first time you run rake ez:tables it will generate an initial version of the file with comments and display a message that the student should go edit the file.

Please note that it's not possible to detect renamed columns. Instead the column is deleted and new one is added.

Allowing rapid evolution of the domain model is the paramount goal. Preventing data loss is NOT a priority here. Students are encouraged to learn how to write a good seeds file instead.

Here's an example db/models.yml file:

Movie:
  - title: string
  - year: integer
  - plot: text
  - poster_url: string
  - director_id: integer

Director:
  - name: string
  - photo_url: string
  

2. Controller-Less Routes!

The goal here is to provide an easier learning curve for routes. Once students have learned how to develop static HTML pages in the /public folder, they can use embedded Ruby if they move their view to the app/views machinery. In Rails that's not possible without also creating a controller and learning the full routing syntax.

Once this gem is installed, a new route syntax becomes supported:

  get '/my-url' => 'folder/filename`

and the ancient, unused legacy syntax becomes intuitive again:

  get '/folder/file'

The payoff is that neither of these routes require a controller class to be defined. Instead, the student needs to only create a view subfolder and an .html (or .html.erb) file.

Once the student sees the need for decoupling the view from certain logic, the Controller concept can be introduced, and the route syntax can be updated.

3. Consuming JSON APIs

Sometimes for a quick win, mashing up data into a Rails view is motivating. The typical open-uri-plus-json-parse dance as been encapsulated into a new method that's added to the JSON module:

  data = JSON.from_api("url goes here")

The given url will be properly URI-encoded. The data variable will be a Ruby array or hash, and the student can get directly to spelunking through the data.

  <% data = JSON.from_api("http://api.openweathermap.org/data/2.5/weather?q=Chicago,IL&units=imperial") %>
  <% temp = data["main"]["temp"].to_i %>
  <h1>It is <%= temp %> degrees outside!"</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment