<?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>Al&#039;s blog &#187; Geekeries</title>
	<atom:link href="http://blog.alweb.org/category/geekeries/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.alweb.org</link>
	<description>Alexis Toulotte</description>
	<lastBuildDate>Wed, 09 Mar 2011 02:07:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>TimeZone coté navigateur &amp; RubyOnRails</title>
		<link>http://blog.alweb.org/2011/01/timezone-cote-navigateur-rubyonrails/</link>
		<comments>http://blog.alweb.org/2011/01/timezone-cote-navigateur-rubyonrails/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 05:36:33 +0000</pubDate>
		<dc:creator>al</dc:creator>
				<category><![CDATA[Geekeries]]></category>

		<guid isPermaLink="false">http://blog.alweb.org/?p=179</guid>
		<description><![CDATA[Il est parfois utile d&#8217;afficher les dates et les heures selon la TimeZone de l&#8217;utilisateur dans nos applications Web. Il y a la manière que l&#8217;on retrouve le plus (pénible à mon gout) qui consiste à la faire choisir à l&#8217;utlisateur via un énorme &#60;select&#62;, ceci qui ressemble à peu près à ça : De [...]]]></description>
			<content:encoded><![CDATA[<p>Il est parfois utile d&#8217;afficher les dates et les heures selon la TimeZone de l&#8217;utilisateur dans nos applications Web.</p>
<p>Il y a la manière que l&#8217;on retrouve le plus (pénible à mon gout) qui consiste à la faire choisir à l&#8217;utlisateur via un énorme <code>&lt;select&gt;</code>, ceci qui ressemble à peu près à ça :</p>
<p><img src="/wp-content/uploads/TimeZone-select.png" alt="TimeZone select" /></p>
<p>De plus, il faut ensuite stocker cette TimeZone en base de données ou dans un cookie. Bref, pénible pour le développeur et pour l&#8217;utilisateur.</p>
<p>C&#8217;est pourquoi, le mieux serait d&#8217;obtenir la TimeZone du navigateur directement. Malheureusement, les navigateurs n&#8217;envoient pas de <code>header HTTP</code> avec la TimeZone configurée sur la machine (un peu comme pour les locales). La seule manière donc, est d&#8217;utiliser Javascript.</p>
<p>Dans le petit script ci-dessous, on va stocker dans un cookie l&#8217;<code><a href="http://fr.wikipedia.org/wiki/Temps_universel_coordonné">UTC</a> offset</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> TimeZone <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> getOffset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTimezoneOffset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> getOffset<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>offset <span style="color: #339933;">!=</span> Cookie.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'time_zone_offset'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            Cookie.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'time_zone_offset'</span><span style="color: #339933;">,</span> offset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">'init'</span><span style="color: #339933;">:</span> init
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
document.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dom:loaded'</span><span style="color: #339933;">,</span> TimeZone.<span style="color: #660066;">init</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note : les méthodes <code>Cookie.get</code> et <code>Cookie.set</code> sont issues d&#8217;un autre bout de Javascript. Demandez-moi si vous en avez besoin.</p>
<p>Maintenant coté serveur (Rails), on va changer la TimeZone en fonction de la valeur de ce cookie :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
&nbsp;
  before_filter <span style="color:#ff3333; font-weight:bold;">:set_time_zone_from_cookies</span>
&nbsp;
  private
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> set_time_zone_from_cookies
    <span style="color:#9966CC; font-weight:bold;">if</span> cookies<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'time_zone_offset'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">present</span>?
      <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">zone</span> = cookies<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'time_zone_offset'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">zone</span> = Rails.<span style="color:#9900CC;">configuration</span>.<span style="color:#9900CC;">time_zone</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>On se retrouve maintenant avec les dates et les heures qui s&#8217;affichent correctement partout dans le monde.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alweb.org/2011/01/timezone-cote-navigateur-rubyonrails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fixtures on Rails 2.1</title>
		<link>http://blog.alweb.org/2008/06/fixtures-on-rails-21/</link>
		<comments>http://blog.alweb.org/2008/06/fixtures-on-rails-21/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 14:41:00 +0000</pubDate>
		<dc:creator>al</dc:creator>
				<category><![CDATA[Geekeries]]></category>

		<guid isPermaLink="false">http://daiquiri.alweb.org/?p=18</guid>
		<description><![CDATA[Suite à mon précédent billet concernant la création d&#8217;un jeu de données propre à une migration en utilisant les fixtures ainsi qu&#8217;aux nouveautés concernant les migrations dans rails 2.1, quelques ajustements ont du être effectués. Les fichiers de migrations sont désormais préfixées par un timestamp (exemple : db/migrate/20080618211857_create_admin_user.rb) et non plus par un simple identifiant [...]]]></description>
			<content:encoded><![CDATA[<p>Suite à mon <a title="Fixtures on Rails" href="/2007/07/fixtures-on-rails/">précédent billet</a> concernant la création d&#8217;un jeu de données propre à une migration en utilisant les <em>fixtures</em> ainsi qu&#8217;aux <a href="http://railscasts.com/episodes/107">nouveautés concernant les migrations dans rails 2.1</a>, quelques ajustements ont du être effectués.</p>
<p>Les fichiers de migrations sont désormais préfixées par un <code>timestamp</code> (exemple : <code>db/migrate/20080618211857_create_admin_user.rb</code>) et non plus par un simple identifiant incrémental. Vos fichiers de <em>fixtures</em> doivent désormais être préfixés par ce même <code>timestamp</code> (exemple : <code>db/fixtures/20080618211857_users.yml</code>).</p>
<p>Voici donc le script <code>lib/create_fixtures.rb</code> mis à jour :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> MigrateFixtures
&nbsp;
  FIXTURES_LOCATION = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../db/fixtures'</span>
  MIGRATIONS_LOCATION = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../db/migrate'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">extended</span><span style="color:#006600; font-weight:bold;">&#40;</span>object<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> object
      alias_method <span style="color:#ff3333; font-weight:bold;">:migrate_without_fixtures</span>, <span style="color:#ff3333; font-weight:bold;">:migrate</span> <span style="color:#9966CC; font-weight:bold;">unless</span> method_defined?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:migrate_without_fixtures</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      alias_method <span style="color:#ff3333; font-weight:bold;">:migrate</span>, <span style="color:#ff3333; font-weight:bold;">:migrate_with_fixtures</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> migrate_with_fixtures<span style="color:#006600; font-weight:bold;">&#40;</span>direction<span style="color:#006600; font-weight:bold;">&#41;</span>
    migrate_without_fixtures<span style="color:#006600; font-weight:bold;">&#40;</span>direction<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#ff3333; font-weight:bold;">:down</span> == direction
    migration = current_migration
    cnx = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>
    files = fixture_files.<span style="color:#9900CC;">find_all</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
      file =~ <span style="color:#006600; font-weight:bold;">/</span>\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#008000; font-style:italic;">#{migration}_/</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    files.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
      table_name = file.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>\<span style="color:#006600; font-weight:bold;">/</span>\d<span style="color:#006600; font-weight:bold;">+</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>\.<span style="color:#9900CC;">yml</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'<span style="color:#000099;">\1</span>'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      say_with_time<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Adding #{table_name} fixtures&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        Fixtures.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>cnx, table_name, <span style="color:#0000FF; font-weight:bold;">nil</span>, file.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>\.<span style="color:#9900CC;">yml</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'<span style="color:#000099;">\1</span>'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">insert_fixtures</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> current_migration
      version = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migrator</span>.<span style="color:#9900CC;">current_version</span>
      file = migration_files.<span style="color:#9900CC;">find</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
        version = <span style="color:#996600;">'%03d'</span> <span style="color:#006600; font-weight:bold;">%</span> version <span style="color:#9966CC; font-weight:bold;">if</span> version.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">size</span> <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#006666;">3</span>
        f =~ <span style="color:#006600; font-weight:bold;">/</span>\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#008000; font-style:italic;">#{version}_/</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      index = migration_files.<span style="color:#9900CC;">index</span> file
      file = index ? migration_files<span style="color:#006600; font-weight:bold;">&#91;</span>index <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> : migration_files.<span style="color:#9900CC;">first</span>
      file.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*</span>\<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>_.<span style="color:#006600; font-weight:bold;">*/</span>, <span style="color:#996600;">'<span style="color:#000099;">\1</span>'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> fixture_files
      <span style="color:#0066ff; font-weight:bold;">@fixture_files</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;#{FIXTURES_LOCATION}/[0-9]*_*.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">sort</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> migration_files
      <span style="color:#0066ff; font-weight:bold;">@migration_files</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;#{MIGRATIONS_LOCATION}/[0-9]*_*.rb&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">sort</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>MigrateFixtures<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Note : Cette version est compatible avec les anciennes version de rails.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alweb.org/2008/06/fixtures-on-rails-21/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iSport</title>
		<link>http://blog.alweb.org/2008/04/isport/</link>
		<comments>http://blog.alweb.org/2008/04/isport/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 11:23:00 +0000</pubDate>
		<dc:creator>al</dc:creator>
				<category><![CDATA[Geekeries]]></category>

		<guid isPermaLink="false">http://daiquiri.alweb.org/?p=17</guid>
		<description><![CDATA[Voici une petite appli Web adaptée pour iPhone / iPod Touch : iSport. Elle permet d&#8217;avoir les informations sportives ainsi que les résultats des matchs en direct (pour l&#8217;instant, que Fooball, Tennis, Basket US et Rugby). La chose utilise Ruby, Rails, Hpricot et comme d&#8217;habitude : Prototype. Quelques screenshots pour la peine : Des retours [...]]]></description>
			<content:encoded><![CDATA[<p>Voici <a href="http://isport.alweb.org">une petite appli Web adaptée pour iPhone / iPod Touch : iSport</a>.</p>
<p>Elle permet d&#8217;avoir les informations sportives ainsi que les résultats des matchs en direct (pour l&#8217;instant, que Fooball, Tennis, Basket US et Rugby).</p>
<p>La chose utilise <a href="http://fr.wikipedia.org/wiki/Ruby">Ruby</a>, <a href="http://rubyonrails.com">Rails</a>, <a href="http://code.whytheluckystiff.net/hpricot/">Hpricot</a> et comme d&#8217;habitude : <a href="http://prototypejs.org/">Prototype</a>.</p>
<p>Quelques screenshots pour la peine :</p>
<p><img class="aligncenter size-full wp-image-88" title="iSport (1)" src="/wp-content/uploads/2009/01/isport-1.jpg" alt="iSport (1)" width="320" height="480" /></p>
<p><img class="aligncenter size-full wp-image-89" title="iSport (2)" src="/wp-content/uploads/2009/01/isport-2.jpg" alt="iSport (2)" width="320" height="480" /></p>
<p>Des retours et / ou commentaires sont les bienvenus !</p>
<ul>
<li><a href="http://www.iphon.fr/post/2008/04/26/iSport-%3A-tout-le-sportau-format-iPhone">Ils en parlent ailleurs</a></li>
<li><a href="http://www.alweb.org/projects/isport/">Accès au source, etc.</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.alweb.org/2008/04/isport/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mount /dev/null</title>
		<link>http://blog.alweb.org/2008/04/mount-devnull/</link>
		<comments>http://blog.alweb.org/2008/04/mount-devnull/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 10:19:00 +0000</pubDate>
		<dc:creator>al</dc:creator>
				<category><![CDATA[Geekeries]]></category>

		<guid isPermaLink="false">http://daiquiri.alweb.org/?p=16</guid>
		<description><![CDATA[Ce matin un collègue a trouvé un gentil mot de notre administrateur système :]]></description>
			<content:encoded><![CDATA[<p>Ce matin un collègue a trouvé un gentil mot de notre administrateur système :</p>
<p><img src="/wp-content/uploads/2009/01/format_m.jpg" alt="Format" title="Format" width="336" height="448" class="aligncenter size-full wp-image-86" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alweb.org/2008/04/mount-devnull/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Diff SVN</title>
		<link>http://blog.alweb.org/2008/02/diff-svn/</link>
		<comments>http://blog.alweb.org/2008/02/diff-svn/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 14:08:00 +0000</pubDate>
		<dc:creator>al</dc:creator>
				<category><![CDATA[Geekeries]]></category>

		<guid isPermaLink="false">http://daiquiri.alweb.org/?p=15</guid>
		<description><![CDATA[En regardant le script de davux, permettant la colorisation d&#8217;un diff, je me suis dit qu&#8217;il devait y avoir une possibilité d&#8217;intégrer ça de manière simple à subversion. Collez ce petit script dans un fichier (nous allons prendre ici /usr/local/bin/colorized_diff) et rendez le exécutable : #! /bin/sh &#160; plus='^[[0;32m' minus='^[[0;31m' diff &#34;$@&#34; &#124; sed 's/^+.*$/'&#34;$plus&#34;'&#38;^[[0m/; [...]]]></description>
			<content:encoded><![CDATA[<p>En regardant le <a href="http://da.weeno.net/code/diff-u">script de davux</a>, permettant la colorisation d&#8217;un diff, je me suis dit qu&#8217;il devait y avoir une possibilité d&#8217;intégrer ça de manière simple à <a title="aka SVN" href="http://subversion.tigris.org/">subversion</a>.</p>
<p>Collez ce petit script dans un fichier (nous allons prendre ici <code>/usr/local/bin/colorized_diff</code>) et rendez le exécutable  :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/sh</span>
&nbsp;
<span style="color: #007800;">plus</span>=<span style="color: #ff0000;">'^[[0;32m'</span>
<span style="color: #007800;">minus</span>=<span style="color: #ff0000;">'^[[0;31m'</span>
<span style="color: #c20cb9; font-weight: bold;">diff</span> <span style="color: #ff0000;">&quot;$@&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/^+.*$/'</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$plus</span>&quot;</span><span style="color: #ff0000;">'&amp;^[[0m/; s/^-.*$/'</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$minus</span>&quot;</span><span style="color: #ff0000;">'&amp;^[[0m/'</span></pre></div></div>

<p>Ensuite, modifiez le fichier <code>~/.subversion/config</code> pour y rajouter ou modifier la ligne suivante dans la section <code>[helpers]</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">diff-cmd = <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>colorized_diff</pre></div></div>

<p>Vous pouvez omettre le chemin (<code>/usr/local/bin</code>) si il est dans votre <code>PATH</code>. Maintenant, un <code>svn diff</code> dans un shell vous affichera les lignes supprimées en rouge et les lignes ajoutées en vert. C&#8217;est pas plus mignon ?</p>
<p>Note : <code>^[</code> est le caractère d&#8217;échappement, vous pouvez le taper dans vim en faisant Ctrl-V puis Echap</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alweb.org/2008/02/diff-svn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixtures on Rails</title>
		<link>http://blog.alweb.org/2007/07/fixtures-on-rails/</link>
		<comments>http://blog.alweb.org/2007/07/fixtures-on-rails/#comments</comments>
		<pubDate>Tue, 10 Jul 2007 14:40:00 +0000</pubDate>
		<dc:creator>al</dc:creator>
				<category><![CDATA[Geekeries]]></category>

		<guid isPermaLink="false">http://daiquiri.alweb.org/?p=14</guid>
		<description><![CDATA[Il est assez fréquent d&#8217;avoir du contenu à pré-remplir dans une base de données lors du déploiement d&#8217;une application. La plupart du temps on se fait un script SQL qui fait les insertions. En Rails, c&#8217;est mieux, on mets les insertions dans les scripts de migration et ça roule. Petit hic, mettons que l&#8217;on a [...]]]></description>
			<content:encoded><![CDATA[<p>Il est assez fréquent d&#8217;avoir du contenu à pré-remplir dans une base de données lors du déploiement d&#8217;une application.</p>
<p>La plupart du temps on se fait un script SQL qui fait les insertions. En Rails, c&#8217;est mieux, on mets les insertions dans les scripts de migration et ça roule.</p>
<p>Petit hic, mettons que l&#8217;on a beaucoup de données et contenant des champs <code>text</code>, le script de migration va vite devenir illisible. Pour ça il y a les <strong>Fixtures</strong>. Seulement, ce n&#8217;est pas géré par les migrations (de base). Les fixtures sont représentés grâce à des fichiers <acronym title="YAML Ain't Markup language">YAML</acronym> décrivant vos modèles.</p>
<p>Voilà un petit script à rajouter dans le répertoire /lib/ (nommez-le create_fixtures.rb) :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_record/fixtures'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> CreateFixtures
&nbsp;
  FIXTURES_LOCATION = <span style="color:#996600;">'db/fixtures'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">extended</span><span style="color:#006600; font-weight:bold;">&#40;</span>object<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> object
      alias_method <span style="color:#ff3333; font-weight:bold;">:migrate_without_fixtures</span>, <span style="color:#ff3333; font-weight:bold;">:migrate</span> <span style="color:#9966CC; font-weight:bold;">unless</span> method_defined?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:migrate_without_fixtures</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      alias_method <span style="color:#ff3333; font-weight:bold;">:migrate</span>, <span style="color:#ff3333; font-weight:bold;">:migrate_with_fixtures</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> migrate_with_fixtures<span style="color:#006600; font-weight:bold;">&#40;</span>direction<span style="color:#006600; font-weight:bold;">&#41;</span>
    migrate_without_fixtures<span style="color:#006600; font-weight:bold;">&#40;</span>direction<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#ff3333; font-weight:bold;">:down</span> == direction
    version = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migrator</span>.<span style="color:#9900CC;">current_version</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
    cnx = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>
    files = <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;#{FIXTURES_LOCATION}/[0-9]*_*.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">unless</span> file.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*/</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>_.<span style="color:#006600; font-weight:bold;">*</span>.<span style="color:#9900CC;">yml</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_i</span> == version
      table_name = file.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*/</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">yml</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      Fixtures.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>cnx, table_name, <span style="color:#0000FF; font-weight:bold;">nil</span>, file.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">yml</span><span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">'1'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">insert_fixtures</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>CreateFixtures<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Ensuite, rajouter cette ligne dans le fichier <code>config/environment.rb</code> :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'/../lib/create_fixtures'</span></pre></div></div>

<p>Voilà la chose est en place. Maintenant voyons l&#8217;utilisation.</p>
<p>Vous avez juste à créer un répertoire <code>db/fixtures</code> dans votre application et y mettre vos fichiersYAML. Nommez vos fichiers de la sorte :</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;numéro_de_migration&gt;&lt;nom_de_la_table&gt;.yml</pre></div></div>

<p>Rien de mieux qu&#8217;un petit exemple :</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ cat db/fixtures/002_users.yml
login: admin
hashed_password: d033e22ae348aeb5660fc2140aec35850c4da997
status: administrator</pre></div></div>

<p>Ici, l&#8217;utilisateur administrateur sera directement ajouté à la fin de la deuxième migration. Vous pouvez bien-entendu mettre plusieurs fichiers de fixtures pour une même migration et créer des utilisateurs par exemple après la deuxième ou même la 42e migration.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.alweb.org/2007/07/fixtures-on-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

