<?xml version="1.0" encoding="iso-8859-2"?>
<!-- name="generator" content="pyblosxom/1.0.0 (May 24, 2004)" -->
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">

<rss version="0.91">
<channel>
<title>... with flags unfurled... : programming/rails   </title>
<link>http://znouza.meinlschmidt.org/blog/programming/rails/index.rss</link>
<description>we reached the dizzy heights...</description>
<language>en</language>
<item>
    <title>Rails 2.1.0 - named_scope</title>
    <link>http://znouza.meinlschmidt.org/blog/programming/rails/080814-2336.html</link>
    <description>
New version of rails comes with a great feature called &lt;strong&gt;named_scope&lt;/strong&gt;.

&lt;pre class=&quot;prettyprint&quot;&gt;
# in your model.rb

class Model &lt; ActiveRecord::Base
 has_many ...
 belongs_to ...

 named_scope :active, :conditions =&gt; {:state =&gt; &apos;active&apos;}
 named_scope :today, :conditions =&gt; [&apos;date_created between ? and ?&apos;, Time.today.to_i, Time.today.to_i+3600*24-1]
 named_scope :by_date_asc, :order =&gt; &apos;date_created asc&apos;
 named_scope :by_date_desc, :order =&gt; &apos;date_created desc&apos;
 named_scope :user_name, lambda {|name| {:conditions =&gt; {:username =&gt; name}}
&lt;/pre&gt;

and you can use its as:


&lt;pre class=&quot;prettyprint&quot;&gt;
# somewhere in your code

 @data = Model.today
 @data = Model.today.active.by_date_asc
 @data = Model.user_name(&apos;some user&apos;).active.today.by_date_desc
&lt;/pre&gt;
</description>
  </item>
<item>
    <title>Exception handling</title>
    <link>http://znouza.meinlschmidt.org/blog/programming/rails/070709-0115.html</link>
    <description>
./lib/mymodule.rb

&lt;pre class=&quot;prettyprint&quot;&gt;
module MyModule

  class Error &lt; RuntimeError; end
  class ConnectionError &lt; Error; end

  def connect
    
    if something_failed?
      raise ConnectionError, &quot;connection failed due to...&quot;
    end
  end
end
&lt;/pre&gt;

somewhere in controller

&lt;pre class=&quot;prettyprint&quot;&gt;
  include MyModule

  def do_connect
    begin
      connect
    rescue MyModule::ConnectionError =&gt; err
      flash.now[:notice] =&gt; err
    end
  end
&lt;/pre&gt;
</description>
  </item>
<item>
    <title>own application configuration</title>
    <link>http://znouza.meinlschmidt.org/blog/programming/rails/070709-0108.html</link>
    <description>
./config/my_configuration.yml

&lt;pre class=&quot;prettyprint&quot;&gt;
MyConfig:
 username: my_username
 password: my_password
 host: my_host
&lt;/pre&gt;

./config/environment.rb

&lt;pre class=&quot;prettyprint&quot;&gt;
require &apos;ostruct&apos;
require &apos;yaml&apos;

OwnConfigFile = &quot;#{RAILS_ROOT}/config/my_configuration.yml&quot;
if File.exist?(MyConfigFile)
  ::ApplicationConfig = OpenStruct.new(YAML.load_file(MyConfigFile))
end
&lt;/pre&gt;

in application:

&lt;pre class=&quot;prettyprint&quot;&gt;
config = ApplicationConfig.MyConfig;
puts config[&apos;username&apos;]
&lt;/pre&gt;
</description>
  </item>
<item>
    <title>Retrieving user login/pass from HTTP_AUTH*</title>
    <link>http://znouza.meinlschmidt.org/blog/programming/rails/060913-1114.html</link>
    <description>&lt;p&gt;I&apos;m using this to get login information from mod_ldap_auth - user is authenticated
against Microsoft&apos;s Active Directory (AD)&lt;/p&gt;

&lt;p&gt;
&lt;pre class=&quot;code&quot;&gt;
&lt;font color=&quot;#c000c0&quot;&gt;def &lt;/font&gt;&lt;font color=&quot;#008080&quot;&gt;getAuthenticated&lt;/font&gt;
&lt;font color=&quot;#804000&quot;&gt;  if&lt;/font&gt; request.env[&lt;font color=&quot;#c000c0&quot;&gt;&apos;&lt;/font&gt;&lt;font color=&quot;#c00000&quot;&gt;HTTP_AUTHORIZATION&lt;/font&gt;&lt;font color=&quot;#c000c0&quot;&gt;&apos;&lt;/font&gt;] &lt;font color=&quot;#804000&quot;&gt;then&lt;/font&gt;
    authdata = request.env[&lt;font color=&quot;#c000c0&quot;&gt;&apos;&lt;/font&gt;&lt;font color=&quot;#c00000&quot;&gt;HTTP_AUTHORIZATION&lt;/font&gt;&lt;font color=&quot;#c000c0&quot;&gt;&apos;&lt;/font&gt;].to_s.split
    user,pass = &lt;font color=&quot;#c000c0&quot;&gt;Base64&lt;/font&gt;.decode64(authdata[&lt;font color=&quot;#c00000&quot;&gt;1&lt;/font&gt;]).split(&lt;font color=&quot;#c000c0&quot;&gt;&apos;&lt;/font&gt;&lt;font color=&quot;#c00000&quot;&gt;:&lt;/font&gt;&lt;font color=&quot;#c000c0&quot;&gt;&apos;&lt;/font&gt;)[&lt;font color=&quot;#c00000&quot;&gt;0&lt;/font&gt;..&lt;font color=&quot;#c00000&quot;&gt;1&lt;/font&gt;]
    [ user, pass ]
  &lt;font color=&quot;#804000&quot;&gt;else&lt;/font&gt;
    [ &lt;font color=&quot;#c000c0&quot;&gt;&amp;quot;&amp;quot;&lt;/font&gt;, &lt;font color=&quot;#c000c0&quot;&gt;&amp;quot;&amp;quot;&lt;/font&gt; ]
  &lt;font color=&quot;#804000&quot;&gt;end&lt;/font&gt;
&lt;font color=&quot;#c000c0&quot;&gt;end&lt;/font&gt;
&lt;/pre&gt;
&lt;/p&gt;
</description>
  </item>
<item>
    <title>Got Mongrel server working</title>
    <link>http://znouza.meinlschmidt.org/blog/programming/rails/060901-1337.html</link>
    <description>I&apos;ve just replaced old fashioned fast_cgi to the Mongrel ruby server. Bellow is
my attempt to startup script for mongrel servers - designed for my slackware box.
Hope it&apos;ll help anyone to start up his own mongrel server for Rails

&lt;p&gt;
&lt;xmp&gt;
#!/bin/sh

# include config
. /etc/rc.d/mongrel.servers

function start_servers {
	echo -n &quot;starting...&quot;
	COUNT=${#SERVER[@]}

	for ((;COUNT;COUNT--)); do
		echo -n &quot;#&quot;
		$MONGREL start $PARAMS_START -c ${SERVER[$COUNT]} -p ${PORT[$COUNT]} &gt;/dev/null 2&gt;&amp;1
	done
	echo &quot;Done&quot;
}

function stop_servers {
	echo -n &quot;stopping...&quot;
	COUNT=${#SERVER[@]}

	for ((;COUNT;COUNT--)); do
		echo -n &quot;#&quot;
		$MONGREL stop -c ${SERVER[$COUNT]} &gt;/dev/null 2&gt;&amp;1
	done
	echo &quot;Done&quot;
}

function status_servers {
	echo &quot;Mongrel status...&quot;
	echo &quot;&quot;
	echo &quot;  PID    PORT   PATH &quot;
	echo &quot;------------------------------------------------------------------------------&quot;
	COUNT=${#SERVER[@]}

	for ((;COUNT;COUNT--)); do
		if [ -f &quot;${SERVER[$COUNT]}/log/mongrel.pid&quot; ]; then
			PID=`cat ${SERVER[$COUNT]}/log/mongrel.pid | awk &apos;{print $1}&apos;`
		else
			PID=&quot;N/A&quot;
		fi
		printf &quot;%6s %6s %s\n&quot; &quot;$PID&quot; &quot;${PORT[$COUNT]}&quot; &quot;${SERVER[$COUNT]}&quot;
	done
	echo &quot;Done&quot;
}

case &quot;$1&quot; in
	&apos;start&apos;)
		start_servers
	;;
	&apos;stop&apos;)
		stop_servers
	;;
	&apos;status&apos;)
		status_servers
	;;
*)
	start_servers
esac
&lt;/xmp&gt;
&lt;center&gt;/etc/rc.d/rc.mongrel&lt;/center&gt;
&lt;/p&gt;

&lt;p&gt;
&lt;xmp&gt;
MONGREL=&quot;mongrel_rails&quot;
PARAMS_START=&quot;-d -a 127.0.0.1&quot;

SERVER[1]=&quot;/path_to_rails_app1/&quot;
PORT[1]=8001

SERVER[2]=&quot;/path_to_rails_app2/&quot;
PORT[2]=8002
&lt;/xmp&gt;
&lt;center&gt;/etc/rc.d/mongrel.servers&lt;/center&gt;
&lt;/p&gt;

&lt;p&gt;and Apache config...&lt;/p&gt;

&lt;p&gt;
&lt;xmp&gt;
# load proxy modules
LoadModule proxy_module  modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so

...

&lt;VirtualHost some_ip:80&gt;
	ServerAdmin awesome@administrator
	DocumentRoot path_to_public_dir_of_rails_app
	ServerName some_server_name
	ErrorLog log/radiant-error_log
	CustomLog log/radiant-access_log combined
	
	ProxyPass / http://127.0.0.1:8001/
	ProxyPassReverse / http://127.0.0.1:8001/
&lt;/VirtualHost&gt;
&lt;/xmp&gt;
&lt;/p&gt;
</description>
  </item>
<item>
    <title>Using GOOGLE API (WSDL example)</title>
    <link>http://znouza.meinlschmidt.org/blog/programming/rails/060829-2048.html</link>
    <description>
&lt;pre class=&quot;prettyprint&quot;&gt;
def search
 require &apos;soap/wsdlDriver&apos;
 @title = &apos;Search Results&apos;
 key = &apos;YOUR GOOGLE API KEY HERE&apos;
 yoursite = &apos;YOUR SITE ADDRESS HERE&apos;
 driver = SOAP::WSDLDriverFactory.new(&quot;http://api.google.com/GoogleSearch.wsdl&quot;).createDriver
 @results = driver.doGoogleSearch(key, @params[&apos;term&apos;]+&quot; site:#{yoursite}&quot;, 0, 10, true, &quot; &quot;, false, &quot; &quot;, &quot; &quot;, &quot; &quot;)
end
&lt;/pre&gt;

and parsing data

&lt;pre class=&quot;prettyprint&quot;&gt;
&lt;% for result in @results.resultElements %&gt;
 &lt;h2&gt;&lt;%= result.title %&gt;&lt;/h2&gt;
 &lt;p&gt;&lt;%= result.snippet %&gt;&lt;/p&gt;
 &lt;p&gt;&lt;a href=&quot;&lt;%= result.URL %&gt;&quot;&gt;&lt;%= result.URL %&gt;&lt;/a&gt;&lt;/p&gt;
&lt;% end %&gt;
&lt;/pre&gt;
</description>
  </item>
   </channel>
</rss>