Aug 31, 2011

Capybara and the problem with window.opener

I have encountered a really strange problem. When Capybara opens a new window with <a href=".." target="..">.

The new window cannot call window.opener in its Javascript code.

The problem occurs because Capybara starts the first browser with http://127.0.0.1:57214. But my <a href> points to http://localhost:57214.

They points to different domain names. That's why Firefox does not allow reference between windows.

It is amazing how the problem is not really relevant to Capybara.

Anyway, the way to solve it is to change default domain name of Capybara.



Put the above lines somewhere in spec_helper.rb

Capybara not to turn into a blank page after testing

It's a little bit of hacking. You have to go to this file.

C:\Ruby192\lib\ruby\gems\1.9.1\gems\capybara-1.0.0\lib\capybara\selenium\driver.rb

in function reset!, please comment the line @browser.navigate_to('about:blank')

-----

And sometimes when running tests without Spork, Capybara automatically closes browser after the testing is done.

If you want to avoid that, just comment the lines in the same file. The line that calls "quit"

Aug 30, 2011

linecache19 errors on missing vm_core.h (Windows 7)

The problem of missing vm_core.h. It should reside in %RUBY_PATH%\include\ruby-1.9.1\ruby-1.9.2-p180.

The cause is that you didn't install Visual Studio C++. Therefore, the C source code for Ruby is not installed as well.

Just install Visual Studio C++. And everything will be fine.

Aug 27, 2011

Test GitHub:gist

JRuby cannot find bundle binary file when do bundle exec

When I run:



It tells me that it cannot find command because the spork gem is not installed. But I already installed it under vendor/gems.

To bypass that, please do:


This will install binaries of gems under RAILS_ROOT/bin.
And you can run:

Batch file that forward arguments

You can use %* for all arguments.

Or refer to %1 %2 ... %9 for the first 9 arguments.

Aug 25, 2011

Clear cache of faster_require gem on Windows

faster_require

On Windows, we cannot run Gem binary file. Therefore, we cannot clear cache as it is told in the instruction.

Nevertheless, faster_require keeps cache in /your_home_dir/.ruby_faster_require_cache

Just delete it.

Install gem from GitHub

Download the package first. It should contain .gemspec file.

Then do this:


gem build GEMNAME.gemspec
gem install gemname-version.gem

Capybara + RSpec to test Facebook connect

In order to set up Capybara with Facebook connect, we need to set up a few things:

1. You need to specify a server port for Capybara and set localhost:port in your Facebook App setting. Otherwise Facebook won't redirect user back to your URL (security reason)

In order to do that, just drop this line in spec_helper.rb:


Capybara.server_port = 57124


2. You need to properly handle Facebook login and allow page. Here is the method that I use:


def facebook_connect(username,password)

if current_path == '/login.php'
within_window "Log In | Facebook" do
fill_in 'email', :with => username
fill_in 'pass', :with => password
click_button "Log In"
end
end

if current_path == '/connect/uiserver.php'
click_button "อนุญาต" rescue ""
click_button "Allow" rescue ""
end
end


That's it.

Aug 24, 2011

Invalid multibyte char (US-ASCII) with Ruby

This is dark magic. The trick to fix is to put this line at the beginning of every file.


# encoding: utf-8

Mongoid Criteria does not work as expected

You have to really be careful because the results are not yet pull

For example, if you invoke .length, it will instead return the length of the criteria array.

Therefore, please always do this:


result = Kratoo.only(:title).skip(100).entries

skip() in MongoDB is not good

We should design the UI in a way that it uses Range-based paging.

For example, view by date.

Aug 23, 2011

Capybara in the house

Just include the gem 'capybara' in your Gemfile.

Then drop these lines in spec_helper.rb


require 'rspec/rails' # find this line

require 'capybara/rspec'
require 'capybara/rails'

Capybara.default_driver = :selenium


And it works like a charm

Aug 19, 2011

Ruby super metaprogramming

I'm posting as a reference. What it does is that it will turn every method in a class into a class method and all of them will be one-level curriable.


class Validator

@@__being_checked_method = nil

def self.method_added(method_name)

puts "#{method_name} added to #{self}"

return if method_name == :validate
return if method_name.to_s.match(/^validation__proxy__/)
return if @@__being_checked_method == method_name

@@__being_checked_method = method_name

puts method_name
new_name = "validation__proxy__#{method_name}".to_sym

alias_method new_name, method_name

define_singleton_method(method_name) { |*args|
puts args.length
puts args.inspect

l = lambda(&(self.new.method(new_name))).curry

args.each { |a|
l = l.curry[a]
}

return l
}

end

class << self
def validate(lamb_func)
puts "hello"
end
end

end

class KratooValidator < Validator

def max_length(length,value)
puts "#{length} #{value}"
end

validate max_length(5)

end


puts KratooValidator.new.validation__proxy__max_length(5,10)
puts KratooValidator.max_length(5).call(12)

Aug 17, 2011

embeds_many is not an Array

You cannot delete element with a usual mean. You have to use destroy_all

Mongoid cannot have same type of embeds_many

If you have multiple embeds_many which are of the same class, every array you save will be on the first field.


embeds_many :agree_names, :as => :notificator, :class_name => "BaseNotificator"
embeds_many :disagree_names, :as => :notificator, :class_name => "BaseNotificator"


In order to prevent this, just change :as to be different. And do not forget to change embedded_in.


embeds_many :agree_names, :as => :agree_notificator, :class_name => "BaseNotificator"
embeds_many :disagree_names, :as => :disagree_notificator, :class_name => "BaseNotificator"


And in the embedded class, do this:


embedded_in :agree_notificator, :polymorphic => true
embedded_in :disagree_notificator, :polymorphic => true

Wait until MongoDB write the last change

We use getLastError() to wait for it (only the current connection).

But if you would like to wait for the next commit across all connections, you would have to add :fsync=>true.

Aug 13, 2011

Moving from DelayedJob to Resque

In order to run Resque on Windows, we require a little bit of tweak in command.

This is to run resque's workers:

rake environment resque:work QUEUE="*"


This is to run montor website:

bundle exec resque-web -F


* you need -F option