| ⌘T | go to file |
| ⌘⌃P | go to project |
| ⌘R | go to methods |
| ⌃G | go to line |
| ⌘KB | toggle side bar |
| ⌘⇧P | command prompt |
| require 'strong_parameters' | |
| class ActiveRecord::Base | |
| include ActiveModel::ForbiddenAttributesProtection | |
| end | |
| class ActionController::Base | |
| # Use this with CanCan's load_resource to permit a set of params before | |
| # it tries to build or update a resource with them. |
I have bemoaned the lack of a ViewModel in Rails many times, and I prefer using Presenters to simulate a ViewModel. But it turns out there is an object that does represent the state of the view in Rails apps. Its an instance of ActionView::Base that the controller calls view_context. We don't have much control of this object, but it seems like a logical place to put our view-specific behavior.
This code is an attempt at creating Presenters using modules. The module will be mixed into the view_context object. This is very similar to how a Decorator module will be mixed into a model object, only instead of being specific to the model is it specific to the view.
This means that testing your presenter is no different than testing any other module. So relying on dependencies such as other methods or instance variables can make testing difficult.
| require 'sinatra' | |
| require 'redis' | |
| require 'json' | |
| require 'date' | |
| class String | |
| def &(str) | |
| result = '' | |
| result.force_encoding("BINARY") |
| =Navigating= | |
| visit('/projects') | |
| visit(post_comments_path(post)) | |
| =Clicking links and buttons= | |
| click_link('id-of-link') | |
| click_link('Link Text') | |
| click_button('Save') | |
| click('Link Text') # Click either a link or a button | |
| click('Button Value') |
| Handlebars.registerHelper('equal', function(lvalue, rvalue, options) { | |
| if (arguments.length < 3) | |
| throw new Error("Handlebars Helper equal needs 2 parameters"); | |
| if( lvalue!=rvalue ) { | |
| return options.inverse(this); | |
| } else { | |
| return options.fn(this); | |
| } | |
| }); |
I'm a fan of MiniTest::Spec. It strikes a nice balance between the simplicity of TestUnit and the readable syntax of RSpec. When I first switched from RSpec to MiniTest::Spec, one thing I was worried I would miss was the ability to add matchers. (A note in terminology: "matchers" in MiniTest::Spec refer to something completely different than "matchers" in RSpec. I won't get into it, but from now on, let's use the proper term: "expectations").
Let's take a look in the code (I'm specifically referring to the gem, not the standard library that's built into Ruby 1.9):
# minitest/spec.rb
module MiniTest::Expectations| /** Reinflates a compressed signature string: | |
| resolution = a representation of the resolution in | |
| pixels of the canvas which this signature will be drawn | |
| e.g. {x:800,y:200} | |
| */ | |
| var inflateToJsonSignature = function (deflatedSig, resolution) { | |
| var components = [], | |
| modifier = 1, | |
| compressWithResolution = /^(?:\[(\d+)x(\d+)\])?([\w\W]*)/, | |
| parsedSigString = deflatedSig.match(compressWithResolution), |