Jul 25, 2011

Delayed Job with RSpec

In your test code, just use this line to execute delayed job:


Delayed::Worker.new.work_off

Jul 20, 2011

Rails 3 Madness

I have been struggling with Rails 3 testing framework for a few days now.

This is a madness. There are so many gems to install:

gem 'rspec', :group => [:development, :test]

gem 'rspec-rails', :group => [:development, :test]
gem 'database_cleaner', :group => :test
gem 'factory_girl_rails', :group => :test
gem 'mongoid-rspec', :group => :test

gem 'cucumber-rails', :group => :test
gem 'capybara', :group => :test

gem 'win32-process', :group => :test
gem 'spork', :git => "https://github.com/timcharper/spork.git", :group => :test


rspec-rails is obvious. It is RSpec for Rails.
database_cleaner is for truncate database. It supports Mongoid. That's why I use it.
factory_girl_rails is a mocking library for Rails. It's so simple.
mongoid-rspec is an adapter of RSpec to Mongoid.
cucumber-rails is a BDD testing framework for Rails.
capybara is for testing AJAX and javascript. It can fire an event click on a button or check if the result HTML contains some specific values.
Spork preloads the Rails environment, so that the testing runs faster.

Now you'll need to install RSpec

rails generate rspec:install


Before you run spork, you'll have to make some changes to application.rb in order to make Spork automatically reload any changes


class Application < Rails::Application
.
.
.

if Rails.env.test?
initializer :after => :initialize_dependency_mechanism do
ActiveSupport::Dependencies.mechanism = :load
end
end

end


And in spec_helper.rb, we put this line at the bottom of Spork.prefork block:


ActiveSupport::Dependencies.clear


And in Spork.each_run block, we put in these lines:


load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }


Now you can start Spork

bundle exec spork --bootstrap

bundle exec spork



Related links: https://github.com/RailsApps/rails3-mongoid-devise/wiki/Tutorial

Jul 18, 2011

Eccentric Mongoid behaviour

If you field's suffix is _id or _ids, when you make a query, it will turns your querying string into BSON::ObjectId automatically.

Therefore, please beware.

Jul 17, 2011

Add custom field to sunspot

In schema.xml, we add certain tags:


<types>
<fieldType name="autosuggest" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.LetterTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.NGramFilterFactory" minGramSize="1" maxGramSize="25" />
<filter class="solr.ThaiWordFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.LetterTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
</types>

<fields>
<dynamicField name="*_as" type="autocomplete" indexed="true" multiValued="true" stored="false"/>
<dynamicField name="*_ass" type="autocomplete" indexed="true" multiValued="true" stored="true"/>
</fields>


Please note that *_ac is for non-stored field and *_acs is for stored fields. It's just how Sunspot stores fields and we have to follow the convention.

And then in Sunspot's model, we would like to force it to use the new field:

class Tag
.
.
.

include Sunspot::Mongoid
searchable do
text :name, :as => :name_ac

end

end

Jul 13, 2011

Sunspot Solr startup problem on Windows

There is an error on Windows because command-line arguments are escaped in a wrong way.

This fix works for me:http://www.trdev.co.uk/2011/07/03/sunspot-and-solr-search-on-windows-7-errors/

Basically, we just go to sunspot gems in Ruby and change this file:

sunspot/lib/sunspot/server.rb

and do this patch

command << "-Xms#{min_memory}" if min_memory

command << "-Xmx#{max_memory}" if max_memory

command << "-Djetty.port=#{port}" if port

- command << "-Dsolr.data.dir=#{solr_data_dir}" if solr_data_dir

- command << "-Dsolr.solr.home=#{solr_home}" if solr_home

- command << "-Djava.util.logging.config.file=#{logging_config_path}" if logging_config_path

+ command << "-Dsolr.data.dir=\"#{solr_data_dir}\"" if solr_data_dir

+ command << "-Dsolr.solr.home=\"#{solr_home}\"" if solr_home

+ command << "-Djava.util.logging.config.file=\"#{logging_config_path}\"" if logging_config_path

command << '-jar' << File.basename(solr_jar)

FileUtils.cd(File.dirname(solr_jar)) do

- exec(Escape.shell_command(command))

+ exec(command.join(" "))

end

end

Rename muliple files, remove prefix on linux


for f in $(ls uploads__*); do mv "${f}" "${f#uploads__}";done

Jul 12, 2011

Rails 3 strangely turns the 8th field of MySql into a BigDecimal

Please see my post: http://stackoverflow.com/questions/6646507/activerecord-in-rails-3-0-3-turns-the-8th-field-of-mysql-into-a-bigdecimal-how-t

I still cannot solve this problem.


-----------

I can solve it now. It is just a mismatch version between MySQL database and client.