<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ruby, Rails, OSX and Linux fun &#187; ruby</title>
	<atom:link href="http://www.frederico-araujo.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frederico-araujo.com</link>
	<description>Ruby, Rails, OSX and linux sysadmin</description>
	<lastBuildDate>Thu, 02 Sep 2010 08:13:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Install a Rails enviroment on Snow Leopard with Macports, Mysql and ModRails Passenger (updated)</title>
		<link>http://www.frederico-araujo.com/2010/08/19/installing-rails-enviroment-on-snow-leopard-with-macports-mysql-and-modrails-passenger/</link>
		<comments>http://www.frederico-araujo.com/2010/08/19/installing-rails-enviroment-on-snow-leopard-with-macports-mysql-and-modrails-passenger/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 08:36:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://www.frederico-araujo.com/?p=175</guid>
		<description><![CDATA[I choose the simple and I believe clean way to use everything from macports and passenger.
The advantage is that it&#8217;s all isolated from the OS X system and using latest cutting edge version of softwares.
I assume you have textmate, because it&#8217;s easy to edit files that need sudo access. Textmate will just ask for your [...]]]></description>
			<content:encoded><![CDATA[<p>I choose the simple and I believe clean way to use everything from macports and passenger.</p>
<p>The advantage is that it&#8217;s all isolated from the OS X system and using latest cutting edge version of softwares.</p>
<p>I assume you have textmate, because it&#8217;s easy to edit files that need sudo access. Textmate will just ask for your password.</p>
<p>Try and download the 30 days trial for easy of this tutorial. <a href="http://macromates.com/">http://macromates.com/</a></p>
<p>Trust me, you will love Textmate, it&#8217;s state of the art editor.</p>
<p>ps: I use &#8220;mate&#8221; instead of &#8220;open -a TextMate&#8221; but not everyone might have the mate symlink.</p>
<h3>Requirements:</h3>
<ul>
<li> Snow Leopard 10.6.4 or greater</li>
<li> Xcode 3.2.3, 2.4.1 or greater</li>
<li> Admin privileges on your mac.</li>
</ul>
<h3>Software preliminary:</h3>
<ul>
<li> Mysql 5.1.49 (macports)</li>
<li> Ruby 1.8.7 (macports)</li>
<li> Apache 2.2.9 (part of OS X)</li>
<li> Rubygems (download)</li>
<li> passenger (gem)</li>
<li> ImageMagick</li>
<li> rmagick</li>
</ul>
<h3>1. Preparing System</h3>
<p>If you are upgrading from Leopard to Snow Leopard, I recommend you delet your old installation.</p>
<pre class="command">sudo rm -rf \
    /opt/local \
    /Applications/DarwinPorts \
    /Applications/MacPorts \
    /Library/LaunchDaemons/org.macports.* \
    /Library/Receipts/DarwinPorts*.pkg \
    /Library/Receipts/MacPorts*.pkg \
    /Library/StartupItems/DarwinPortsStartup \
    /Library/Tcl/darwinports1.0 \
    /Library/Tcl/macports1.0 \
    ~/.macports
</pre>
<h4>1.1 Prepare PATH environment:</h4>
<pre class="command">$ open -a TextMate ~/.bash_profile
</pre>
<p>Edit your ~/.bash_profile file and add these 2 line.<br />
Check if they are not there already.</p>
<pre>export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH
</pre>
<h4>1.2 Download and Install Xcode</h4>
<p>http://developer.apple.com/technologies/xcode.html</p>
<p>http://developer.apple.com/technologies/xcode.html</p>
<h4>1.3 Download Macports and install</h4>
<p>Download Page: http://www.macports.org/install.php</p>
<p>Download Direct Link: http://distfiles.macports.org/MacPorts/MacPorts-1.9.1-10.6-SnowLeopard.dmg</p>
<p>You might use this guide for installing Macports:</p>
<p>Full Install Guide: http://guide.macports.org/#installing</p>
<h4>1.3.1 Update macports</h4>
<pre class="command">sudo port -v selfupdate</pre>
<h3>2. Mysql</h3>
<h4>2.1 Intall Mysql</h4>
<pre class="command">sudo port -v install mysql5-server mysql5</pre>
<h4>2.2 Make mysql autoload on startup</h4>
<pre class="command">sudo port load mysql5-server
sudo -u mysql mysql_install_db5
</pre>
<h3>3. Memcached (Optional)</h3>
<pre class="command">sudo port -v install memcached libmemcached</pre>
<h4>3.1 Autoload memcached on startup</h4>
<pre class="command">sudo port load memcached</pre>
<h3>4. Ruby, Rubygems, Rails, other gems</h3>
<p>4.1 Install Ruby from macports</p>
<pre class="command">sudo port -v install ruby</pre>
<pre class="command">$ ruby -v
ruby 1.8.7 (2010-08-08 patchlevel 302) [x86_64-darwin10]
</pre>
<h4>4.2 download rubygems from http://rubygems.org/pages/download</h4>
<pre class="command">cd /tmp
wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
tar xpf rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo ruby setup.rb
</pre>
<h4>4.3 Install rails, rake, rspec etc.</h4>
<pre class="command">sudo gem install rake rails thin tzinfo capistrano ruby-debug rspec</pre>
<p>extra:</p>
<h4>4.4 install mysql gem</h4>
<pre class="command">sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
</pre>
<h3>5. ImageMagick, Rmagick and mini_magick (Optional)</h3>
<p>lets install ImageMagick with support for JPEG, TIFF, WMF, PDF, and PNG images, and for Postscript and TrueType fonts.</p>
<pre class="command">sudo port -v install tiff -macosx imagemagick +q8 +gs +wmf</pre>
<pre class="command">sudo gem install mini_magick rmagick</pre>
<p>Test rmagick</p>
<pre class="command">$ irb -rubygems -r RMagick
&gt;&gt; puts Magick::Long_version
This is RMagick 2.13.1 ($Date: 2009/12/20 02:33:33 $) Copyright (C) 2009 by Timothy P. Hunter
Built with ImageMagick 6.6.3-0 2010-08-19 Q8 http://www.imagemagick.org
Built for ruby 1.8.7
Web page: http://rmagick.rubyforge.org
Email: rmagick@rubyforge.org
=&gt; nil
</pre>
<h3>6. Passenger</h3>
<h4>6.1 Install Passenger gem</h4>
<pre class="command">sudo gem install passenger</pre>
<h4>6.2 Check Passenger path</h4>
<pre class="command">passenger-config --root</pre>
<pre> -&gt; /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.15</pre>
<h4>6.3 Build passenger for apache</h4>
<p>follow instructions on screen</p>
<pre class="command">sudo passenger-install-apache2-module</pre>
<h4>6.4 Enable Passenger on apache:</h4>
<pre class="command">open -a TextMate /etc/apache2/extra/httpd-passenger.conf</pre>
<p>Put this on that file, change wherever necessary for your directories.</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">LoadModule</span> passenger_module /opt/local/lib/ruby/gems/<span style="color: #ff0000;">1.8</span>/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/<span style="color: #ff0000;">1.8</span>/gems/passenger-2.2.15
PassengerRuby /opt/local/bin/ruby
PassengerMaxPoolSize <span style="color: #ff0000;">6</span>  <span style="color: #adadad; font-style: italic;"># maximum global rails servers</span>
PassengerMaxInstancesPerApp <span style="color: #ff0000;">2</span>  <span style="color: #adadad; font-style: italic;"># maximum rails servers per application</span>
RailsFrameworkSpawnerIdleTime <span style="color: #ff0000;">1800</span>
RailsAppSpawnerIdleTime <span style="color: #ff0000;">600</span>
PassengerPoolIdleTime <span style="color: #ff0000;">600</span>
PassengerMaxRequests <span style="color: #ff0000;">1000</span>  <span style="color: #adadad; font-style: italic;"># after 1000 requests will restart server, to skip memory leak :)</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># Enabling NameBased Virtualhost</span>
<span style="color: #00007f;">NameVirtualHost</span> *:<span style="color: #ff0000;">80</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># my rails app virtual host 1</span>
&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;
    <span style="color: #00007f;">ServerName</span> my-rails-app.local
    <span style="color: #00007f;">DocumentRoot</span> <span style="color: #7f007f;">&quot;/Users/fred/rails/my-rails-app/public&quot;</span> <span style="color: #adadad; font-style: italic;"># change this to match your folder</span>
    RailsEnv <span style="color: #7f007f;">&quot;development&quot;</span>
   &lt;<span style="color: #000000; font-weight:bold;">Directory</span> /Users/fred/rails/my-rails-app/public&gt;
        <span style="color: #adadad; font-style: italic;"># change this to match your folder</span>
        <span style="color: #adadad; font-style: italic;"># MultiViews must be turned off</span>
        <span style="color: #00007f;">Options</span> -MultiViews
        <span style="color: #00007f;">AllowOverride</span> <span style="color: #00007f;">All</span>
        <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span>
        <span style="color: #00007f;">Allow</span> <span style="color: #00007f;">from</span> <span style="color: #00007f;">all</span>
    &lt;/<span style="color: #000000; font-weight:bold;">Directory</span>&gt;
    <span style="color: #adadad; font-style: italic;"># logs are optional, change this to match your folder</span>
    <span style="color: #00007f;">CustomLog</span>  <span style="color: #7f007f;">&quot;/Users/fred/rails/my-rails-app/log/access_log&quot;</span> combined
    <span style="color: #00007f;">ErrorLog</span>   <span style="color: #7f007f;">&quot;/Users/fred/rails/my-rails-app/log/error_log&quot;</span>
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;
&nbsp;
<span style="color: #adadad; font-style: italic;"># my rails app virtual host 2</span>
&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;
    <span style="color: #00007f;">ServerName</span> myapp-xyz.local
    <span style="color: #00007f;">DocumentRoot</span> <span style="color: #7f007f;">&quot;/Users/fred/rails/myapp-xyz/public&quot;</span>  <span style="color: #adadad; font-style: italic;"># change this to match your folder</span>
    RailsEnv <span style="color: #7f007f;">&quot;development&quot;</span>
    &lt;<span style="color: #000000; font-weight:bold;">Directory</span> /Users/fred/rails/my-rails-app/public&gt;
        <span style="color: #adadad; font-style: italic;"># change this to match your folder</span>
        <span style="color: #adadad; font-style: italic;"># MultiViews must be turned off</span>
        <span style="color: #00007f;">Options</span> -MultiViews
        <span style="color: #00007f;">AllowOverride</span> <span style="color: #00007f;">All</span>
        <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span>
        <span style="color: #00007f;">Allow</span> <span style="color: #00007f;">from</span> <span style="color: #00007f;">all</span>
    &lt;/<span style="color: #000000; font-weight:bold;">Directory</span>&gt;
    <span style="color: #00007f;">CustomLog</span>  <span style="color: #7f007f;">&quot;/Users/fred/rails/myapp-xyz/log/access_log&quot;</span> combined
    <span style="color: #00007f;">ErrorLog</span>  <span style="color: #7f007f;">&quot;/Users/fred/rails/myapp-xyz/log/error_log&quot;</span>
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</pre></div></div>

<p>6.5 Enable Virtual host on apache:</p>
<pre class="command">open -a TextMate /etc/apache2/httpd.conf</pre>
<p>Add this new line at the bottom:</p>
<pre># Include Passenger ModRails config file
Include /private/etc/apache2/extra/httpd-passenger.conf
</pre>
<h4>6.6 Add your .local domain to /etc/hosts/</h4>
<pre class="command">open -a TextMate /etc/hosts</pre>
<p>Add this line to that file and change to the name of your choosen application</p>
<p>You might add as many as you want, each line for each that ServerName on your VirtualHost blocks</p>
<pre>127.0.0.1 my-rails-app.local
127.0.0.1 myapp-xyz.local
</pre>
<p>I have more than 50 .local apps in there.</p>
<h4>5 Start Apache</h4>
<p>first let&#8217;s test apache configuration:</p>
<pre class="command">apachectl configtest</pre>
<p>if you get &#8220;Syntax OK&#8221; then you are ready to start it</p>
<pre class="command">sudo apachectl start</pre>
<p>If you want Apache to autostart when you boot your computer,<br />
then enable Web Sharing from preferences.</p>
<h4>7. Final</h4>
<p>go to your browser and open the url of your application http://my-rails-app.local</p>
<p>If it works, congratulations.</p>
<p>If didn&#8217;t work, let me know here in the comments.</p>
<h3>Extras</h3>
<h4>Nokogiri</h4>
<pre class="command">  sudo port -v install libxml2 libxslt
  sudo gem install nokogiri -- --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib --with-xslt-dir=/opt/local
</pre>
<h4>Sqlite</h4>
<pre class="command">sudo port install sqlite3
sudo gem install sqlite3-ruby
</pre>
<h3>9. Sphinx, thinking-sphinx, sphinxsearchlogic</h3>
<pre class="command">sudo port install sqlite3
sudo gem install thinking-sphinx sphinxsearchlogic
</pre>
<h4>Update:</h4>
<p>From the comments, some people might want to enable php and your Sites folder.</p>
<p>To be able to enable php and others you will have to enable another default vhost with localhost as servername.</p>
<p>Edit the file /etc/apache2/httpd.conf to enable vhosts</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;"># Virtual hosts</span>
<span style="color: #00007f;">Include</span> /private/etc/apache2/extra/httpd-vhosts.conf</pre></div></div>

<p>then open /private/etc/apache2/extra/httpd-vhosts.conf file,<br />
delete or comment everything from there and add this block only:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">#</span>
<span style="color: #adadad; font-style: italic;"># Use name-based virtual hosting.</span>
<span style="color: #adadad; font-style: italic;">#</span>
<span style="color: #00007f;">NameVirtualHost</span> *:<span style="color: #ff0000;">80</span>
&nbsp;
<span style="color: #adadad; font-style: italic;"># Change /Users/fred/ to your appropriate login name</span>
&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;
    <span style="color: #00007f;">ServerName</span> localhost
    <span style="color: #00007f;">DocumentRoot</span> /Users/fred/Sites/
    <span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
    <span style="color: #00007f;">DefaultType</span> text/html
    &lt;<span style="color: #000000; font-weight:bold;">Directory</span> <span style="color: #7f007f;">&quot;/Users/fred/Sites&quot;</span>&gt;
        <span style="color: #00007f;">DefaultType</span> text/html
        <span style="color: #00007f;">Options</span> +ExecCGI <span style="color: #0000ff;">FollowSymLinks</span> <span style="color: #0000ff;">Indexes</span>
        <span style="color: #00007f;">AllowOverride</span> <span style="color: #00007f;">All</span>
        <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span>
        <span style="color: #00007f;">Allow</span> <span style="color: #00007f;">from</span> <span style="color: #00007f;">all</span>
    &lt;/<span style="color: #000000; font-weight:bold;">Directory</span>&gt;
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</pre></div></div>

<h3>Links and Resources</h3>
<p><a href="http://rmagick.rubyforge.org/install-osx.html">http://rmagick.rubyforge.org/install-osx.html</a></p>
<p><a href="http://wiki.github.com/tenderlove/nokogiri/what-to-do-if-libxml2-is-being-a-jerk">http://wiki.github.com/tenderlove/nokogiri/what-to-do-if-libxml2-is-being-a-jerk</a></p>
<p><a href="http://www.macports.org/install.php">http://www.macports.org/install.php</a></p>
<p><a href="http://guide.macports.org/#installing">http://guide.macports.org/#installing</a></p>
<p><a href="http://distfiles.macports.org/MacPorts/MacPorts-1.9.1-10.6-SnowLeopard.dmg">http://distfiles.macports.org/MacPorts/MacPorts-1.9.1-10.6-SnowLeopard.dmg</a></p>
<p><a href="http://rob.by/2009/installing-mysql-via-macports-on-snow-leopard-for-ruby-development">http://rob.by/2009/installing-mysql-via-macports-on-snow-leopard-for-ruby-development</a>/</p>
<p><a href="http://macromates.com/">http://macromates.com/</a></p>
<p><a href="http://github.com/joost/sphinxsearchlogic">http://github.com/joost/sphinxsearchlogic</a></p>
<p><a href="http://www.sphinxsearch.com/">http://www.sphinxsearch.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederico-araujo.com/2010/08/19/installing-rails-enviroment-on-snow-leopard-with-macports-mysql-and-modrails-passenger/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Really checking the content-type/mime_type of a file in OSX and Linux</title>
		<link>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/</link>
		<comments>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 18:55:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[osx]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[content_type]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mime_type]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.frederico-araujo.com/?p=95</guid>
		<description><![CDATA[I have came across many projects where checking file uploads and content-type (mime-type) is poorly implemented or heavy in resource.
Methods I have seen so far:
1. Checking content-type from file name: this inefficient, a user can just rename a file and you are fooled, or the file can have a different file format and you will [...]]]></description>
			<content:encoded><![CDATA[<p>I have came across many projects where checking file uploads and content-type (mime-type) is poorly implemented or heavy in resource.</p>
<p>Methods I have seen so far:</p>
<p>1. Checking content-type from file name: this inefficient, a user can just rename a file and you are fooled, or the file can have a different file format and you will not get the expected result.</p>
<p>2. Using Rmagick to check if the file is an image. This is so slow and uses so much Ram. You can try to initialize an rmagick object from an image file, then rescue when the file is not an image.</p>
<p>3. Using mini_magick to check if a file. This method is faster than rmagick. Implemen ted same way as rmagick.</p>
<p>A Better method for OSX and Linux,  is to use the command line tool &#8220;file&#8221; included in most UNIX operating systems.</p>
<p>It is very fast and very accurate.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">file = <span style="color:#996600;">&quot;/path/to/file.ext&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> RUBY_PLATFORM.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>darwin<span style="color:#006600; font-weight:bold;">|</span>linux<span style="color:#006600; font-weight:bold;">|</span>unix<span style="color:#006600; font-weight:bold;">|</span>solaris<span style="color:#006600; font-weight:bold;">|</span>bsd<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
 content_type = <span style="color:#996600;">`file --raw --brief &quot;#{file}&quot;`</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>
 <span style="color:#9966CC; font-weight:bold;">case</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>image<span style="color:#006600; font-weight:bold;">|</span>png<span style="color:#006600; font-weight:bold;">|</span>jpg<span style="color:#006600; font-weight:bold;">|</span>jpeg<span style="color:#006600; font-weight:bold;">|</span>gif<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;image&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>pdf<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;pdf&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Microsoft Word|Microsoft Office Document&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;doc&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#008000; font-style:italic;"># This can go on and on</span>
   real_type = <span style="color:#996600;">&quot;Unknown&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Some examples of content types:</p>
<p>.doc =  Microsoft Word document data</p>
<p>.doc = Microsoft Office Document</p>
<p>.pdf = PDF document, version 1.4</p>
<p>.pdf = PDF document, version 1.3</p>
<p>.psd = Adobe Photoshop Image</p>
<p>.png  = PNG image data, 3508 x 4961, 8-bit/color RGBA, non-interlaced</p>
<p>.gif = GIF image data, version 89a, 195 x 109</p>
<p>.jpg = JPEG image data, EXIF standard</p>
<p>etc&#8230;</p>
<p>I hope this can be useful to someone.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A little Haml tutorial on how to render different formats</title>
		<link>http://www.frederico-araujo.com/2008/08/19/a-little-haml-tutorial-on-how-to-render-different-formats/</link>
		<comments>http://www.frederico-araujo.com/2008/08/19/a-little-haml-tutorial-on-how-to-render-different-formats/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 17:00:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[haml]]></category>

		<guid isPermaLink="false">http://wp.frederico-araujo.com/?p=8</guid>
		<description><![CDATA[Suppose you have a Model called Article that contains a text field and a format field.
You would like to use haml, textile or HTML to edit your Article from the admin interface.

  create_table &#34;articles&#34;, :force =&#62; true do &#124;t&#124;
    t.string   &#34;title&#34;
    t.text     [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you have a Model called Article that contains a text field and a format field.</p>
<p>You would like to use haml, textile or HTML to edit your Article from the admin interface.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  create_table <span style="color:#996600;">&quot;articles&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:force</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
    t.<span style="color:#CC0066; font-weight:bold;">string</span>   <span style="color:#996600;">&quot;title&quot;</span>
    t.<span style="color:#9900CC;">text</span>     <span style="color:#996600;">&quot;body&quot;</span>
    t.<span style="color:#CC0066; font-weight:bold;">string</span>   <span style="color:#996600;">&quot;formatting_type&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">20</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;HTML&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>app/models/article.rb</p>
<p>It’s quite simple. All you have to do is to add this helper in your application_helper.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> print_formated<span style="color:#006600; font-weight:bold;">&#40;</span>type,text<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> type
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;HTML&quot;</span>
      text
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;Plain Text&quot;</span>
      h text
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;HAML&quot;</span>
      <span style="color:#6666ff; font-weight:bold;">Haml::Engine</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">render</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;Syntaxy&quot;</span>
      Syntaxi.<span style="color:#9900CC;">line_number_method</span> = <span style="color:#996600;">'none'</span>
      Syntaxi.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">process</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;Textile&quot;</span>
      RedCloth.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>text<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_html</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In your views/articles/_form.haml add the select field.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  = label <span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:formatting_type</span>
  = <span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:formatting_type</span>, <span style="color:#6666ff; font-weight:bold;">Article::FORMATTING_TYPES</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>p<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:include_blank</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Then in the Show view (articles/show.haml)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
.<span style="color:#9900CC;">article</span>
  .<span style="color:#9900CC;">title</span>
    <span style="color:#006600; font-weight:bold;">%</span>h1
      = h <span style="color:#0066ff; font-weight:bold;">@article</span>.<span style="color:#9900CC;">title</span>
  .<span style="color:#9900CC;">body</span>
    = print_formated<span style="color:#006600; font-weight:bold;">&#40;</span>@article.<span style="color:#9900CC;">formatting_type</span>, <span style="color:#0066ff; font-weight:bold;">@article</span>.<span style="color:#9900CC;">body</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>that’s pretty much it.<br />
 <img src='http://www.frederico-araujo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.frederico-araujo.com/2008/08/19/a-little-haml-tutorial-on-how-to-render-different-formats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
