Rails on Maui

Programming in Paradise

Enums and Queries in Rails 4.1, and Understanding Ruby

Sometimes when you get puzzled by what Rails is doing, you really just need to understand what Ruby is doing.

For example, given this simple code to get an attribute value:

# return value of some_attribute and foobar
def some_attribute_foobar
  "#{some_attribute} and foobar"

Beginners are often stumped by why this code does not set an attribute value:

# change the value of some_attribute to foobar
def change_some_attribute
  # why doesn't the next line set the some_attribute value to "foobar"?
  some_attribute = "foobar"

What’s going on?

In the first method, some_attribute is actually a method call which gets the attribute value of the record. This works in Rails ActiveRecord due to the Ruby feature of method_missing which allows some code to run when a method is called that does not exist.

In the second method, a local variable called some_attribute is getting assigned. There is no call to method_missing, as this is a variable assignment!

The correct code should have been:

# change the value of some_attribute to foobar
def change_some_attribute
  self.some_attribute = "foobar"

Adding a JS LIbrary to a Ruby on Rails Project When Using Webpack

What’s it like to add a JavaScript library when Webpack is integrated into your Ruby on Rails environment, per my article: Fast Rich Client Rails Development With Webpack and the ES6 Transpiler?

It’s super easy! But what if you want some of your the legacy JavaScript or CoffeeScript code in your Rails app to access the Webpack added library?

Here’s a real world example. Suppose you want your JavaScript code to round numbers to decimal places. Math.round() only rounds decimal numbers to the nearest integer. A code sample on that page is provided to show you how round numbers off to some number of decimal places.

A quick Google for JavaScript libraries finds npm package compute-roundn. A look at the github repository for compute-io/roundn reveals clean code and it has some tests.

Fast Rich Client Rails Development With Webpack and the ES6 Transpiler

There has to be a better way to incorporate the JavaScript ecosystem into Rails.

Have you:

  1. Wondered if there’s a better way to utilize modern JavaScript client frameworks the context of an existing Ruby on Rails project?
  2. Gotten confused about how to integrate JavaScript libraries and examples that are packaged up into proper “modules”?
  3. Discovered the drawbacks of having all applications JavaScript littering the global name-space.
  4. Heard about ES6 (aka Harmony), the next version of JavaScript and how the cool kids in Silicon Valley (Facebook, Instagram, Square, etc.) are using ES6 syntax?

How would you like to achieve, within a Rails project:

  1. The ability to prototype a rich UI, seeing changes in JS and CSS/Sass code almost instantly after hitting save, without the page reloading.
  2. First class citizenship for utilizing the Node ecosystem, by specifying dependencies in package.json, running npm install, and then simply requiring modules in JavaScript files.
  3. Seamless integration of Node based JavaScript assets for the Rails Asset Pipeline, thus not circumventing the asset pipeline, but co-existing with it and leveraging it.
  4. The ability to plug the node client side ecosystem into an existing Rails project seamlessly.
  5. Utilization of many JavaScript tools, such as the React JSX tranpiler and ES6 transpiler.

This article will show you how you can utilize Webpack in your Rails development process to achieve these goals!

React on Rails Tutorial

In response to a recent client request for a richer browser side UI experience, I took a fresh look at all the recent advances in JavaScript rich client frameworks. The React library stood out as unique, innovative, and impressive.

The main reasons that I like React are:

  1. It’s a better abstraction than MVC!
  2. React keeps track of what needs to change in the DOM with its virtual DOM model.
  3. All the view rendering code can assume that nothing changes during the rendering process as components recursively call render(). This makes reasoning about the rendering code much simpler.
  4. The simpler conceptual model of always rendering the entire UI from a given state is akin to the server side rendering of HTML pages, that Rails programmers are more familiar with.
  5. The documentation is very good, and it’s got significant traction.

Given that React is just about the View part of the client UI, or more specifically, view components, it seems especially suitable for integration into the Rails ecosystem to help build better rich JavaScript UIs. The React website contains a simple tutorial utilizing Node for the backend. Suppose you want to use Rails for the backend?

This following instructions walk you through the steps to build the original simple tutorial with a Rails 4.2 backend utilizing the react-rails gem. With the Rails scaffold generator, very little Rails coding is required. You can try the end result of the completed tutorial on Heroku, and the code on Github.

Golden Gate Ruby Conference (GoGaRuCo) Pictures 2014

I took lots of great pictures at the 2014 Golden Gate Ruby Conference this year.

Overall, the conference was awesome. All the speakers seemed incredibly well prepared.

In case you haven’t heard, this was the last GoGaRuCo conference. Why? I heard that the costs for the facility are going up, especially the costs for catering. I also suspect that other new conferences, such as Ember Conf, are competing for attention. And certainly it’s been a huge undertaking for the conference organizers.

I’ve been toying around with creating a Rails on Maui Conference, and I’ve just created a forum for just this sort of discussion.

Should we have a Maui Rails Conference? Let’s discuss the possibility of such a conference here. I’d need at least several committed co-organizers in order for this to become a reality. A possible date would be next September, 2015, given that GoGaRuCo will no longer take place.

I’d propose having a smaller, less formal conference for the first year. I’ve got a very reasonably priced venue in mind that could take up to 100 participants.

Ideas? Want to help?

I’ve broken the pictures up into smaller sets of the best pictures which I’ve placed in Facebook albums. Then I’ve got the complete sets of images posted to Flickr.

If you need any full resolution, non-watermarked images, please get in touch with me.