If you execute IRB with the command line flag –noprompt you will enter in to an IRB session with none of the extra characters showing up on the left side of the terminal. This is great if you want to experiment with code and then use the mouse to copy & paste from it. Like, for example, in a code blog like the one you’re reading…
(Thanks to 6ftdan for this cool tip.)
Monday, February 22, 2016
Monday, February 15, 2016
Rspec tricks for DRY mocked specs
These “tricks” are straight from the docs. One will help DRY up your specs, the other will help you mock your controller for controller tests.
First, define your shared context:
Then, include it in your spec files:
Shared Contexts
Shared context allows for extracting certain setup steps and sharing them across several different sets of tests.First, define your shared context:
RSpec.shared_context "user setup" do
let(:user) { User.new(name: 'Chuck Norris') }
before { allow(user).to receive(:some_method) }
end
Then, include it in your spec files:
describe UsersController do
include_context 'user setup'
# Tests go here...
end
Anonymous Controllers
Anonymous controllers are a way to specify certain behavior for the controller you are trying to test, or more likely for a parent controller. The thing I found it most useful for, and not coincidentally the thing that the docs says it is useful for, is testing global error handling.
describe UsersController do
controller do
def index
raise AccessDenied
end
end
subject { get :index }
describe "handling AccessDenied exceptions" do
it "redirects to the /401.html page" do
expect(subject).to redirect_to("/401.html")
end
end
end
Monday, February 8, 2016
Converting unix timestamps in Ruby
Unix timestamps are common to see in other languages, and also common to see in API responses. Converting a unix timestamp to DateTime in Ruby is dead simple:
Let’s say your unix timestamp includes milliseconds:
Notice how both DateTime strings are the same? DateTime converted to string drops any millisecond data, but keep it as a DateTime object and you can run
unix_timestamp = Time.now.to_i
# => 1453667949
DateTime.strptime(unix_timestamp.to_s, "%s").to_s
# => "2016-01-24T20:39:09+00:00"
Let’s say your unix timestamp includes milliseconds:
unix_timestamp = (Time.now.to_f * 1000).to_i
# => 1453667949151
DateTime.strptime(unix_timestamp.to_s, "%Q").to_s
# => "2016-01-24T20:39:09+00:00"
DateTime.strptime
will allow you convert just about any formatted time string, see the docs for examples.Notice how both DateTime strings are the same? DateTime converted to string drops any millisecond data, but keep it as a DateTime object and you can run
strftime
and display it in any format you like:
DateTime.strptime(unix_timestamp.to_s, "%Q").strftime('%D %T:%L')
# => "01/24/16 20:39:09:151"
Monday, February 1, 2016
Don't wrap specific CSS elements
Another quickie quickie CSS tricky:
Let’s say you’re trying to make a site responsive to many different screen sizes, like you have to these days. You have a block of text that needs to wrap, but you also have a word or number you don’t want to get wrapped. The best example for this I can think of is a phone number – you don’t want it display weird like this:
Wrap that word or number or whatever in a
Let’s say you’re trying to make a site responsive to many different screen sizes, like you have to these days. You have a block of text that needs to wrap, but you also have a word or number you don’t want to get wrapped. The best example for this I can think of is a phone number – you don’t want it display weird like this:
Call Jenny today! 800-867-
5309
Wrap that word or number or whatever in a
span
and style it with "white-space: nowrap;"
. Then, that particular element will wrap as one piece: Call Jenny today!
800-867-5309
Subscribe to:
Posts (Atom)