Skip to content

Instantly share code, notes, and snippets.

@kinopyo
kinopyo / omniauth_macros.rb
Created November 4, 2011 05:44
Integration test with Omniauth. This example is using twitter, and assume you've installed rspec and capybara. Official document is here: https://github.com/intridea/omniauth/wiki/Integration-Testing
# in spec/support/omniauth_macros.rb
module OmniauthMacros
def mock_auth_hash
# The mock_auth configuration allows you to set per-provider (or default)
# authentication hashes to return during integration testing.
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser',
@kinopyo
kinopyo / example_spec_with_javascript_enalbed.rb
Created November 6, 2011 05:58
How to enable javascript in request(integration) test using rspec and capybara webkit. Also covered issue with database transaction.
# in your view erb file
<%= link_to_function "test js", '$(this).html("js works")' %>
# in test spec
it "supports js", :js => true do
visit tasks_path
click_link "test js"
page.should have_content("js works")
end
@kinopyo
kinopyo / gist:1342782
Created November 6, 2011 11:45
Check current login shell, change login shell command.
$ echo $SHELL
$ chsh -s /bin/tcsh
@kinopyo
kinopyo / compared_to_defined_method_with_oneline_condition.erb
Created November 7, 2011 02:28
Passing local optional variables to sub template views.
# this will work
<% unless defined?(:show_author) %>
<% show_author = true %>
<% end %>
# NOT work
<% show_author = true unless defined?(:show_author) %>
# this will work
<% show_author = true unless local_assigns.has_key? :show_author %>
@kinopyo
kinopyo / minitest.rb
Created November 8, 2011 02:32
Seems like 'turn' gem required minitest
# under /Users/username/.rvm/gems/ruby-1.9.2-p290/gems/turn-0.8.3/lib/turn/autorun/minitest.rb
require 'turn/autoload'
require 'minitest/unit'
require 'minitest/spec'
require 'turn/colorize'
require 'turn/controller'
require 'turn/runners/minirunner'
@kinopyo
kinopyo / gist:1350279
Created November 9, 2011 03:46
Rails asset(画像、CSSなど)hostのについての勉強メモです。

asset hostを指定

image_tag("rails.png")のhelper methodで生成するリンクはデフォルトでは同じホストのpublicフォルダを指しています。それを変更したい場合はconfig/environments/production.rbActionController::Base.asset_hostをいじります。

ActionController::Base.asset_host = "assets.example.com"
image_tag("rails.png")
# => <img alt="Rails" src="http://assets.example.com/images/rails.png?1230601161" />
@kinopyo
kinopyo / application.html.erb
Created November 9, 2011 11:00
Targeting the iPhone 4 Retina Display with CSS3 Media Queries. This way retina and non-retina will get their own images, and will save you some KB in non-retina device.
<%= stylesheet_link_tag "main.css", :media => "all" %>
<%= stylesheet_link_tag "retina.css", :media => "only screen and (-webkit-min-device-pixel-ratio: 2)" %>
@kinopyo
kinopyo / checkbox.coffee
Created November 22, 2011 08:47
jQuery handle checkbox check change event.
$("#some_checkbox").click (e) ->
if $(this).is(':checked')
# actions when checked
else
# actions when not checked
@kinopyo
kinopyo / escape_url.rb
Created December 8, 2011 02:36
Ruby: How to escape url
require 'cgi'
CGI.escape(url)
@kinopyo
kinopyo / gist:1547098
Created January 1, 2012 11:36
MongoMapper query for not null column.

Goal

"Using MongoMapper to find all users that :name column is not null", this is all I want.

Solution

To do so, use this query:

User.where(:name.ne => nil).all
User.all(:name.ne => nil)