danott.website / archive / 2015

Multiple calls to ReactDOM.render can be a super powerful tool. Given a state-ful component Counter

// Initial render into document.body
React.render(<Counter count=0 />, document.body)
// => { state: { count: 0 }, props: { count: 0 } }

// Something internal happens that changes state to 50
// => { state: { count: 50 }, props: { count: 0 } }

// Re-render without unmounting into document.body
React.render(<Counter count=100 />, document.body)
// => { state: { count: 50 }, props: { count: 100 } }

In many use-cases, flux is introduced to distinguish between props and state. If props are changing on the server, this is a nice light-weight way re-render everything, while maintaining some client-side state. (Super helpful in rails in my usage)

While playing with webpack in a Rails environment, there are still some variables I want to expose to the window. I came up with this 2-liner to treat exports from the top most bundle as global variables!

export { Whatever, You, Want }
Object.assign(window, exports)

When using elm make, the output is determined by the extension.

elm make Main.elm --output main.html # generates html
elm make Main.elm --output main.js # generates js

Put it in a Rakefile. Why incur the overhead of ruby my_script, when you can just type rake? It takes a little more time to write, but being able to return to a small folder and type rake -T by convention makes you faster in the long run.

The Effective Emailer (guykawasaki.com)

If you can’t say it in plain text, you don’t have anything worth saying

Yes Lord, amen.

You are not allowed to complain about what you are not willing to confront. I saw that in a tweet a while ago, and it is a good word.

Prefixing methods that will change what is on output to the screen with render_ sounds verbose, but it has been helpful as I’ve started to use it more. Another React convention that is informing other areas of development.

Avoid these traps when nesting Ruby modules (blog.honeybadger.io)

It turns out that the “inheritance” of a parent’s constants by the child isn’t due to the parent/child relationship. It’s lexical. That means that it’s based on the structure of your code, not on the structure of the objects your code is building.

Vim and Ctags (andrew.stwrt.ca)

A wet-the-appetite introduction to using ctags with vim.

Using CORS (html5rocks.com)

An explanation of CORS that you’ll actually use.

CORS is real. The part that really tripped me up, is the preflight OPTIONS request.

You can handle this options request in Rails with a nice routing constraint.

match '*', to: 'cors#handle', via: %i(options)

Then set your Access-Control-[Whatever] headers accordingly, and you’re in business.

There are a lot of ways that payment can fail. Thankfully, Stripe provides ways to trigger almost every conceivable failure. And they put the credit card values that will trigger these failures into a handy doc.

It’s important to remember, some of these failures will occur when attempting to get a Stripe token. Others will fail when attempting to use the Stripe token.

When using Bugsnag within a Rails app, you must use the before_bugsnag_notify macro within your ApplicationController (or similar) to add custom info. I tried to be clever, using the Bugsnag.before_notify_callbacks manually in an initializer. This didn’t work. My assumption is that Bugsnag.before_notify_callbacks.clear is called somewhere internally in the Railtie.

It pays to read the Bugsnag ruby docs.

Ruby's case statement (blog.honeybadger.io)

Ruby’s case statement uses the === operator behind the scenes. This can be used (or abused) for conciseness and/or cleverness.

In Rails’ Mailer Previews, you can append .text to the URL to view the text version by default. Way better than reloading, and having to select text from the dropdown!

Rails.application.routes.url_helpers can be used to access your URL helpers from anywhere.

What if the user was a function? (youtube.com)

Andre Staltz ask this question at JSConf. Exploring that question introduces some very interesting ideas about functional programming, and how we can model user/computer interactions.

In JavaScript, document.activeElement gives you the active element. How did I never know this?

A new way to understand your Rails app’s performance (justinweiss.com)

The takeaway is to use rack-mini-profiler to find hot spots in your rails code.

<<-SOMETHING.strip_heredoc in Ruby, to strip the heredoc, and align to the current level of indentation. Doc

When working in Ruby, JSON.parse(payload, symbolize_names: true) will automaticaly symbolize the names.

There is no magic for Rails’ enum in fixtures. If you want to use the string value (which is the entire point of using the enum), you need to ERB it up. Something like

michael:
  name: Michael Bluth
  occupation: <%= Person.occupations[:actor] %> # because 'actor' won't do it

You can use %i[some symbols] in ruby to generate [:some, :symbols]