jQuery and cross domain AJAX requests
When making cross-domain AJAX requests with jQuery (using CORS or xdomain or similar), you will run into issues with HTTP headers:
- jQuery will not set the
X-Requested-With
header. On your server, requests will not look like AJAX requests (request.xhr?
will befalse
). - jquery-ujs will not set CSRF headers.
This is by design and improves secu...
Howto respond html or json in the same controller action with Rails 2
Code snippet tested with Rails 2.3
def index
# ...
if request.xhr?
html = render_to_string(:partial => "list", :layout => false)
respond_to do |format|
format.html { render :text => html }
format.json { render :json => {:html => html, ... } }
end
end
end
Note: Perhaps you ran into ActionView::MissingTemplate
error and this card might help. If you call render_to_string
within the format.json
block, Rails will only look for an index.json
template, but not for an `index.erb...
jpillora/xdomain · GitHub
Automagically makes XHR requests work cross-domain by tunneling all communications through an IFRAME on your page.
Note sure if it's a wise idea, but it's a thing now.
Defining Ruby strings with % (percent) notation
The attached post shows some alternative ways to define Strings in Ruby using the percent notation. This can be useful when you'd like to use quotes ("
) or single-quotes ('
) in your strings:
%(any alpha-numeric)
%[char can be]
%%used as%
%!delimiter\!! # escape '!' literal
%( (pa(re(nt)he)sis) ) #=> "(pa(re(nt)he)sis)"
%[ [square bracket] ] #=> "[square bracket]"
%{ {curly bracket} } #=> "{curly bracket}"
%< <pointy bracket> > #=> "<pointy bracket>"
%< \<this...
Ruby lets you redefine backticks
This actually works:
class Klass
def initialize
`hi world`
end
def `(message)
puts "Called with backticks: #{message}"
end
end
Klass.new # Prints "Called with backticks: hi world"
Hat tip to @jcoglan.
Spreewald 1.0.0 released
Spreewald now has a spreewald
binary that lists all available steps, optionally filtering them. Example:
$> spreewald
# All Spreewald steps
Given I am on ...
... long list
$> spreewald check
# All Spreewald steps containing 'check'
When I check "..."
When I uncheck "..."
Then the "..." checkbox( within ...)? should be checked
Then the "..." checkbox( within ...)? should not be checked
Then the radio button "..." should( not)? be (checked|selected)
Use byebug on Ruby 2+
The debugger
gem does not seem to be properly working on Ruby 2. Use byebug
instead!
Byebug is a simple to use, feature rich debugger for Ruby 2. It uses the new TracePoint API for execution control and the new Debug Inspector API for call stack navigation, so it doesn't depend on internal core sources. It's developed as a C extension, so it's fast. And it has a full test suite so it's reliable. Note that byebug works only for ruby 2.0.0 or newer. For...
RSpec 1.x matcher for delegations
The attached RSpec matcher allows for comfortably testing delegation.
Examples
describe Post do
it { should delegate(:name).to(:author).with_prefix } # post.author_name
it { should delegate(:month).to(:created_at) }
it { should delegate(:year).to(:created_at) }
end
Credits go to txus. See the attached link for an RSpec 2+ version.
Google Material Design with AngularJS
The material design project for Angular is a complementary effort to the Polymer project's paper elements collection. Our goal is to provide a set of AngularJS-native UI elements that implement the material design system.
Google Material Design as Polymer components
The Paper elements are a set of UI elements that implement the material design system.
Sass function to set a color to an absolute hue
The adjust-hue
function of Sass allows you to change a color's hue, but only relative to its current hue.
adjust-hue(#ffff00, 120)
// => #00ffff
adjust-hue(#444400, 120)
// => #004444
As you can see, we moved our yellow by 120° to cyan. \
But what if you want to move any color to a hue of 120° which is a nice shiny green?
Take this function:
@function set-hue($color, $target-hue)
$current-hue: hue($color)
$degree...
Skype 4.3 for Linux fixes group chats
Skype has been updated to 4.3 on Linux. This fixes group chat issues with non-linux clients.
If you have previously installed skype via ubuntu packages, you need to remove those fist via
sudo apt-get remove skype skype-bin
Note
Try to install the 32 bit version. In serveral cases this was the way that worked out.
Grab the installer here:
terminator keyboard shortcuts
When connecting to multiple (i.e. > 4) servers to dive into logfiles or do security updates, terminator is what you want.
There are several keyboard shortcuts available:
- Ctrl-Shift-E: Split the view vertically.
- Ctrl-Shift-O: Split the view horizontally.
- Ctrl-Shift-P: Focus be active on the previous view.
- Ctrl-Shift-N: Focus be active on the next view.
- Ctrl-Shift-W: Close the view where the focus is on.
- Ctrl-Shift-Q: Exit terminator.
- Ctrl-Shift-X: Enlarge active window...
Rails Assets
Automatically builds gems from Bower packages (currently 1700 gems available). Packaged Javascript files are then automatically available in your asset pipeline manifests.
Why we're not using it
At makandra we made a choice to use bower-rails instead. While we believe Rubygems/Bundler to be superior to Javascript package managers, we wanted to use something with enough community momentum behind it that it won't go away in 10 years...
SudoSlider: a jQuery slider
SudoSlider is a simple yet powerful content slider that makes no (or very few) assumptions about your markup and is very customizable.
You can basically embed any HTML into the slides, so you can mix images, videos, texts, and other stuff.
Check out the demos.
Please note:
- There is a ton to configure. Check the demos and read the docs.
- It does not bring styles for prev/next links etc, so you need to style controls yourself (which I consider to b...
Working around OpenSSL::SSL::SSLErrors
If your requests blow up in Ruby or CURL, the server you're connecting to might only support requests with older SSL/TLS versions.
You might get an error like: OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=unknown state
SSL Server Test
This SSL Server Test can help finding out which SSL/TLS versions the server can handle.
Ruby
In Ruby, you can teach Net::HTTP
to use a specific SSL/TLS version.
uri = URI.parse(url)
ssl_options = {
use_ssl: true,
ssl_version...
whenever: Make runner commands use bundle exec
In whenever you can schedule Ruby code directly like so:
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
Combined with the best practice to hide background tasks behind a single static methods you can test, this is probably preferable to defining additional Rake tasks.
Unfortunately when whenever register a runner command, it doesn't use bundle exec
in the resulting crontab. This gets you errors like this:
`gem_original_require': no suc...
Material Design - Google design guidelines
Impressive set of design guidelines from Google.
shoulda-matcher methods not found in Rails 4.1
So you're getting an error message like the following, although your Gemfile lists shoulda-matchers
and it has always worked:
NoMethodError:
undefined method `allow_value' for #<RSpec::ExampleGroups::Person::Age:0x007feb239fa6a8>
This is due to Rails 4.1 (specifically, Spring) revealing a weak point of shoulda-matchers
-- jonleighton explains why.
Solution
The solution is to follow [the gem's installation guide](https://github.com/thoughtbot/sh...
Telling Spring to watch certain directories/files
If you have some file or directory that should trigger a Spring reboot, tell Spring e.g. in config/spring.rb
:
Spring.watch 'file.rb'
Spring.watch 'lib/templates'
However, Spring will silently drop paths that do not exist at the time calling #watch
. Unless you restart Spring (thereby reloading the watch
commands), it won't even watch them once they do exist.
Make sure the watchable paths exist before telling Spring to watch, e.g. with
FileUtils.touch 'file.rb'
FileUtils.mkdir_p 'lib/templates'
Spring.wa...
Device sizing and network throttling are coming to Chrome DevTools
See screenshot here.
This is great news because network throttling is very painful in Linux.
The features are already in Chrome Canary, so expect them to come to your Chrome sources soon.
How to test bundled applications using Aruba and Cucumber
Aruba is an extension to Cucumber that helps integration-testing command line tools.
When your tests involve a Rails test application, your tool's Bundler environment will shadow that of the test application. To fix this, just call unset_bundler_env_vars
in a Cucumber Before block.
Previously suggested solution
Put the snippet below into your tool's features/support/env.rb
-- now any command run through Aruba (e.g. via #run_simple
) will have a clean Bundler envir...
Cucumber step to manually trigger javascript events in Selenium scenarios (using jQuery)
When you cannot make Selenium trigger events you rely on (e.g. a "change" event when filling in a form field), trigger it yourself using this step:
When /^I manually trigger a (".*?") event on (".*?")$/ do |event, selector|
page.execute_script("jQuery(#{selector}).trigger(#{event})")
end
Note that this only triggers events that were registered through jQuery. Events registered through CSS or the native Javascript registry will not trigger.
Tearing Down Capybara Tests of AJAX Pages
An all-in-approach to fix the problem of pending AJAX requests dying in the browser when the server ends a test or switches scenarios.
We were able to work around this issue in most projects by doing this instead:
After '@javascript' do
step 'I wait for the page to load'
end