RubyOnRails
4 六月 2011 | View Comments
By default, devise will redirect to login path if session timeout.
So user will redirect to login path when visit page do not need login after session timeout.
To solve this problem you can follow these steps:
- Set custom failure app, modify code at the end of
config/initializers/devise.rb
config.warden do |manager|
manager.failure_app = DeviseFailureApp
end
- create your custom failure app
DeviseFailureApp
class DeviseFailureApp < Devise::FailureApp
def redirect
message = warden.message || warden_options[:message]
if message == :timeout
redirect_to attempted_path
else
super
end
end
end
Tagged in devise, RubyOnRails
RubyOnRails,Tips,ruby
27 四月 2011 | View Comments
class UrlWriterSingleton
include Singleton
include ActionController::UrlWriter
def self.default_url_options
{:host => 'www.seravia.com'}
end
end
module NameRouteHelper
def self.included(base)
base.send(:include, InstanceMethods)
end
module InstanceMethods
def name_path_for(name, options = {})
UrlWriterSingleton.instance.send("#{name}_path", options)
end
end
end
Usage:
Class TestNamedRouteHelper
include NameRouteHelper
end
TestNamedRouteHelper.new.name_path_for("root")
Tagged in routes, RubyOnRails, Tips
RubyOnRails,Tips
25 十月 2010 | View Comments
create a file:
console.rb
#!/usr/bin/env ruby
app_path = File.expand_path("../", File.dirname(__FILE__))
boot_path = File.join(app_path,"boot")
command = "irb -r irb/completion -r #{boot_path}"
exec command
Tagged in irb, RubyOnRails
RubyOnRails
15 八月 2010 | View Comments
In this commit we found we big improvement.
They add :inverse_of for active_record association
What is that used for?
Here is the example from commit message
class Man < ActiveRecord::Base
has_one :face, :inverse_of => :man
end
class Face < ActiveRecord::Base
belongs_to :man, :inverse_of => :face
end
m = Man.first
f = m.face
If we don’t have :inverse_of, f.man will be different object, not m@ object and what's more active_record will fire another sql query if you can @f.man.
After we add :inverse_of option, @ m == f.man , and @f.man will not fire a sql query.
But now :inverse_of only works for has_one, has_many. It also supplies inverse support for belongs_to
associations where the inverse is a has_one and it’s not a polymorphic.
I think DataMapper need change their website why DataMapper. Active Record has Identity Map now
Thanks h-lame for giving us this very useful feature which also have sufficient test.
Tagged in 2.3.6, active_record, inverse_of, RubyOnRails
RubyOnRails
15 八月 2010 | View Comments
In this commit rails team improved redirect_to method, we can passing flash option directly.
# ... in an action
#old style
flash[:notice] = "Pay attention to the road"
redirect_to post_url(@post)
#new way
redirect_to post_url(@post), :notice => "Pay attention to the road"
They also add some convenient methods for flash object. Now you can get/set flash object in view by alert,alert=(message),notice,notice=(message)
Tagged in 2.3.6, active_controller, flash, RubyOnRails
RubyOnRails
31 七月 2010 | View Comments
In this commit Rails add a new feature that we can specify what’s the postillion to add a column.
# In migration
# add column to in first position
add_column :testings, :new_col, :integer, :first => true
# add column after another column
add_column :testings, :new_col, :integer, :after => "column_a"
# change a column with position
change_column :testings, :second, :integer, :first => true
change_column :testings, :second, :integer, :after => "column_c"
About how column position affect performance, you can see this blog post
Thanks bmarini commit this feature, also thanks jeremy reviewed this commit.
Tagged in 2.3.6, RubyOnRails
RubyOnRails,Tips
21 七月 2010 | View Comments
Rails UnitTest
in file
test/test_helper.rb in
class ActiveSupport::TestCase add
teardown :clean_mongodb
def clean_mongodb
puts "cleaning mongodb...."
Mongoid.database.collections.each do |collection|
unless collection.name =~ /^system\./
collection.remove
end
end
puts "finished cleaning mongodb."
end
Rspec
in file
spec/spec_helper.rb, in
Spec::Runner.configure add
config.after(:each) do
puts "cleaning mongodb...."
Mongoid.database.collections.each do |collection|
unless collection.name =~ /^system\./
collection.remove
end
end
puts "finished cleaning mongodb."
end
Cucumber
add new file
features/support/mongodb_clean.rb
After do |scenario|
puts "cleaning mongodb...."
Mongoid.database.collections.each do |collection|
unless collection.name =~ /^system\./
collection.remove
end
end
puts "finished cleaning mongodb."
end
Tagged in cucumber, mongoid, rspec, RubyOnRails, test
RubyOnRails
3 七月 2010 | View Comments
Before we use bundler, we unpack all dependencies into vendor folder. But we found it hard to use, especially one of your gem require rubygems in code, yes it happens.
The killer feature bunlder has in my view is: you can install gems in any folder, that’s means you can install gems into your project folder directly. So each of your projects will has it’s own gemset.I think this is a better solution than use rvm to create separate gemset for each project(see my previous entry).
If you have already installed bundler, or you have already using bundler, you can skip the first part, go bundler for passenger directly
Upgrade Your Gem
First, check whether your gem version >= 1.3.6, if not upgrade your gem
- For mac user, just
gem update --system
- For linux user:
- Install rubygems-update gem
gem install rubygems-update
- Upgrade rubygem, run
./bin/update_rubygems, if it says: “can’t find command”, you can go to your gem EXECUTABLE DIRECTORY which you can get by run command gem environment
Install bundle
Install bundler using gem install bundle, as this blog wrote, bundler verison is 0.9.26
Manage your gems use bundle
There are good documents on bundler website, you can check the basic usage there. I’ll not mention here.
ok, let’s do it.
Yes, that’s it. Now your gems will only live in your project folder. It’s very sweet when you need deploy multi projects on one machine.
Bundler with passenger
After you deploy your bundler enhanced project with passenger, passenger will yelling: please gem install bundler.
the solution is add gem dependencies in Gemfile. Yes it’s strange, but I google about it, I haven’t better solution, if you know you can tell me.
Tagged in bundler, deployment, gem, passenger, RubyOnRails
RubyOnRails,Tips
13 五月 2010 | View Comments
As previous solution doesn’t works, after read source code of ActiveSupport::TestCase I find a final solution.
class ActiveSupport::TestCase
setup :log_test
private
def log_test
unless @already_logged_this_test
puts "\n\nStarting #{@method_name} in class:#{self.class.name}\n#{'-' * (20 + @method_name.length + self.class.name.length)}\n"
end
@already_logged_this_test = true
end
setup is a callback of ActiveSupport::TestCase. So we doesn’t hack anything. It works fine.
And what’s more rake test already support verbose model
rake test:units TESTOPTS="-v"
if you using run test directly like this, TESTOPTS="-v" doesn’t work
ruby -Itest test/units/some_test.rb
Tagged in RubyOnRails, test, Tips
RubyOnRails,Tips
14 四月 2010 | View Comments
Deprecated
See this tips-print-test-name-and-file-name-when-testing-final-solution
Last time to introduced how to print test name and method when tesing
I found it actually have problem, it can’t print test name when some testcase implement setup method. alias_method_chain will not work.
Then I discovered we have a way to work around, last blog post mentioned Wrap a method which might be overrided by subclass
So I’ll changed the code a bit like this:
class ActiveSupport::TestCase
def self.inherited(base)
base.class_eval do
def setup_with_naming
puts "\n== #{@method_name} : #{self.class.name} =="
setup_without_naming
end
def setup
end
def self.method_added(meth)
return if @_in_method_added
@_in_method_added = true
if meth.to_s == "setup"
alias_method_chain :setup, :naming
end
@_in_method_added = false
end
end
end
end
Tagged in RubyOnRails, test