Searchlogic, Postgres, and Case Insensitive Ordering
Posted by gabe-fuzz
Unfortunately when using postgres the default ordering is case sensitive such that all Capitalized values come before all lowercase values. This seems counter intuitive and certainly not desired in my project. As far as I can tell there is currently no support for case insensitive ordering with searchlogic. I was able to solve this by adding two new named_scopes in Searchlogic::NamedScopes::Ordering.create_ordering_conditions like so:
named_scope("iascend_by_#{column}".to_sym, {:order => "upper(#{table_name}.#{column}) ASC"})
named_scope("idescend_by_#{column}".to_sym, {:order => "upper(#{table_name}.#{column}) DESC"})
Search logic rocks, and I know it’s bad practice to be hacking plugins so I’m hoping this will be included in the next release. There were a couple other subtle changes that I had to make to get it to work. To use this patch, copy the below code into /config/initializers/searchlogic_extensions.rb:
module Searchlogic
module NamedScopes
module Ordering
def ordering_condition_details(name)
# raise name.to_s
if name.to_s =~ /^(ascend|descend|iascend|idescend)_by_(#{column_names.join("|")})$/
{:order_as => $1, :column => $2}
elsif name.to_s =~ /^order$/
{}
end
end
def create_ordering_conditions(column)
named_scope("ascend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} ASC"})
named_scope("descend_by_#{column}".to_sym, {:order => "#{table_name}.#{column} DESC"})
named_scope("iascend_by_#{column}".to_sym, {:order => "upper(#{table_name}.#{column}) ASC"})
named_scope("idescend_by_#{column}".to_sym, {:order => "upper(#{table_name}.#{column}) DESC"})
end
end
module AssociationOrdering
private
def association_ordering_condition_details(name)
associations = reflect_on_all_associations.collect { |assoc| assoc.name }
if name.to_s =~ /^(ascend|descend|iascend|idescend)_by_(#{associations.join("|")})_(\w+)$/
{:order_as => $1, :association => $2, :condition => $3}
end
end
end
end
end
You can now use MyObject.iascend_by_name and MyObject.idescend_by_name.
Tags: ruby on rails / ruby / rails / searchlogic / postgres / order / named_scopes /
Those damn field with errors divs
Posted by gabe-fuzz
It seems to be a common issue where css developers are battling with the <div> tags that rails drops in when there are errors in a form. In searching for a solution to this I came across this one:
ActionView::Base.field_error_proc = Proc.new { |html_tag, instance|
"<span class=\"fieldWithErrors\">#{html_tag}</span>" }
placed at the end of your config/environment.rb file. This appeared to work fine until I tried firing up my console getting the following error:
Loading development environment (Rails 2.3.3)/config/environment.rb:45:NameError: uninitialized constant ActionView/Library/Ruby/Gems/1.8/gems/rails-2.3.3/lib/rails/backtrace_cleaner.rb:2:NameError: uninitialized constant ActiveSupport::BacktraceCleaner/Library/Ruby/Gems/1.8/gems/rails-2.3.3/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController
… so console doesn’t know anything about ActionView and thusly fails to load your environment when it crosses that line. So instead I decided to move the code to config/initializers/field_with_errors.rb like so:
module ActionView
class Base
def self.field_error_proc
Proc.new { |html_tag, instance| "<span class=\"fieldWithErrors\">#{html_tag}</span>" }
end
end
end
Worked like a charm.
Tags: ruby on rails / ruby / rails / fieldWithErrors / errors /
Rails, Postgres, and Snow Leopard
Posted by gabe-fuzz
Upon installing Snow Leopard I found that my postgres installation was no longer working. Turns out a newer version of postgres (postgres84… I was using 83) was released and is compatible with Snow Leopard. My postgres installation for Snow Leopard using macports looks like this:
sudo port install postgresql84-server
sudo mkdir -p /opt/local/var/db/postgresql84/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql84/defaultdb
sudo su postgres -c ‘/opt/local/lib/postgresql84/bin/initdb -D /opt/local/var/db/postgresql84/defaultdb’
sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql84-server.plist
sudo launchctl start org.macports.postgresql84-server
psql84 -U postgres
And the postgres gem:
sudo env ARCHFLAGS=”-arch x86_64” gem install postgres — —with-pgsql-lib=/opt/local/lib/postgresql84 —with-pgsql-include=/opt/local/include/postgresql84
Tags: ruby / rails / postgres / postgres84 / snow leopard /
Vlad with Git
Posted by gabe-fuzz
So I just got a new computer and I was installing all my gems and downloading all my repos onto it to get my new system up to speed. I did the standard
sudo gem install vlad
and then tried to deploy with
rake vlad:deploy
only to find that for some reason vlad was refusing to read my config/deploy.rb file. Instead I got the error “Don’t know how to build task: ‘vlad:deploy’”.
As it turns out when using vlad with git you have to also include the vlad-git library. So I ran
sudo gem install vlad-git
and everything ran as normal. Too bad that little fact didn’t make it into the vlad documentation, but here it is anyway. Happy deploying.