Sublime RubyTest Shortcuts

Run entire Rspec File
CMD + SHIFT + T

Run specific test or block (where cursor is selected)
CMD + SHIFT + R

Encapsulation patterns in AngularJS

Providing public and private design patterns in Angular.js

The concept of Encapsulation is an important design pattern in most object-oriented programming designs. Determining which properties and methods should be accessible is a nice forcing function because it forces the programmer to think about how his or her could will be consumed.

If you’ve spent any time in the world of Javascript, you probably know that Javascript does not provide native support for defining public or private interfaces.

If you haven’t explored Javascript encapsulation in the past, encapsulation is most often used in Javascript Constructor functions which operate a bit like class definitions in Classical inheritance using the new Object() constructor pattern.

Encapsulation is typically achieved by defining local variables within the function object. These locals can only be referenced by other functions defined on the function object.

You can find a more detailed exposé of encapsulation in javascript here.

What about AngularJS?

Angular provides a number of ways to create javascript singleton objects that are available via dependency injection. Depending on who you talk to, it’s a bit of a mess. At the end of the day though, AngularJS is just javascript and we can build encapsulation into our Angular factories and services using the same types of patterns.

Factories

Services

Controllers

I find myself thinking a lot about what should be exposed publicly in my view controllers. As a beginner, it’s easy to just attach your functions and variables to $scope (or this if using `controller as` syntax) and call it a day. But do you really need your view to have access to all of those functions? In most cases you don’t. Instead, assign these private functions or values to a local variable within your controller function.

I often define a function that initializes the default values within the controller.

Making peace between server and client: Camelizing and de_camelizing Restangular data

Sometimes you find that the API that you’re interacting with via Angular $http requests uses the underscore convention rather than Javascript’s camelCase convention. In this case, our Ruby on Rails app spits out blobs like the second example and we ultimately want it re-serialized like the first example. It’s easy to create a confusing quagmire of under_score and camelCase functions and keys all over your project if you don’t implement a way to translate object keys.

We need some way to re-serialize this object’s keys to camelCase after a response is received. Similarly, requests made to the server should be translated to under_score like the server is expecting. Fortunately the Restangular library makes this quite easy to accomplish.

Restangular allows you to add response and/or request interceptors which allow you to modify your data before and after a $http request. Using the Humps library to camelize and deCamelize, it was simple to modify the requests and responses handled by Restangular. Simply add a config block and use the provided RestangularProvider to add a responseInterceptor and a requestInterceptor:

How to specify a custom model class for your ActiveModel::Serializer 0.8 Serializer

ActiveModel::Serializers gives you a lot of flexibility and modularity when it come to generating JSON for your Rails api. Serializer just work when the serializer is named after the model it references ( MyModelNameSerializer). You also knew you could easily extend a base ActiveModelSerializer using class inheritance MyExtendedSerializer < MyModelSerializer. Recently, however, I found myself building a serializer that couldn't be named by ActiveModel::Serializer convention and couldn't inherit from the base serializer for that class. So where does this gem find it's dependent class? It turns out that the method Serializer#model_class, defined on Serializer defines this behavior.

It was relatively simple to override this behavior: Just override this method in your serializer so that the class method model_class refers to the parent class.

ENV vars: A better way to pass arguments to rake tasks in Rails

Today, I happened upon a situation where I needed to pass an argument into my rake task. In the past I’ve passed an argument using an array-like syntax like like this post describes. While this works, I didn’t really like the syntax and it didn’t seem very natural in the command-line.

Calling a task like rake seeder:seed[100,someString] didn’t seem as readable as it could be.

I’ve found a solution that I like better that utilizes a method from Thoughtbot in their Paperclip gem.

Set an environment variable as part of your rake task command, then look for that ENV var during the task.

Then look for the env var in the task itself.

It’s simple and a lot more readable than passing in an array of arguments.

More >>