<?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>Anders Hassis &#187; Rails</title>
	<atom:link href="http://hassis.com/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://hassis.com</link>
	<description>Did mmorpgs overwrite your common sense?</description>
	<lastBuildDate>Sat, 21 Jan 2012 01:19:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Riding the Rails: Amazon S3 &amp; Flowplayer</title>
		<link>/2010/08/01/riding-the-rails-amazon-s3-flowplayer/</link>
		<comments>/2010/08/01/riding-the-rails-amazon-s3-flowplayer/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 07:00:52 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Programmering]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://hassis.com/?p=329</guid>
		<description><![CDATA[Vi har börjat använda Amazon S3 för att lagra filmer som vi sedan ska spela upp (med hjälp av Flowplayer) i en inloggningsskyddad applikation. För att sätta upp kopplingen mot S3 så använder vi oss av Paperclip och AWS::S3. Att få igång uppladdningen mot S3 tillsammans med Paperclip var absolut inga problem, det fungerade i [...]]]></description>
			<content:encoded><![CDATA[<p>Vi har börjat använda <a href="http://aws.amazon.com/s3/">Amazon S3</a> för att lagra filmer som vi sedan ska spela upp (med hjälp av <a href="http://flowplayer.org/">Flowplayer</a>) i en inloggningsskyddad applikation. </p>
<p>För att sätta upp kopplingen mot S3 så använder vi oss av <a href="http://github.com/thoughtbot/paperclip">Paperclip</a> och <a href="http://amazon.rubyforge.org/">AWS::S3</a>. Att få igång uppladdningen mot S3 tillsammans med Paperclip var absolut inga problem, det fungerade i princip så fort vi hade lagt in något liknande detta i vår modell:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">has_attached_file <span style="color:#ff3333; font-weight:bold;">:media_file</span>,
    <span style="color:#ff3333; font-weight:bold;">:storage</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:s3</span>,
    <span style="color:#ff3333; font-weight:bold;">:s3_credentials</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'config'</span>, <span style="color:#996600;">'s3.yml'</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#ff3333; font-weight:bold;">:path</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;:attachment/:id/:style/:basename.:extension&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:bucket</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'our_bucket'</span>,
    <span style="color:#ff3333; font-weight:bold;">:s3_permissions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'authenticated-read'</span>,
    <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;:s3_protected_url&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Video Validations</span>
  validates_attachment_presence <span style="color:#ff3333; font-weight:bold;">:media_file</span>
  validates_attachment_content_type <span style="color:#ff3333; font-weight:bold;">:media_file</span>, <span style="color:#ff3333; font-weight:bold;">:content_type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'application/x-shockwave-flash'</span>, <span style="color:#996600;">'application/x-shockwave-flash'</span>, <span style="color:#996600;">'application/flv'</span>, <span style="color:#996600;">'video/x-flv'</span>, <span style="color:#996600;">'video/H264'</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>(Det är <em>authenticated-read</em> som sätter rättigheterna på anslutningen och gör att vi kan använda oss av Paperclips <a href="http://yardoc.org/docs/JasonKing-paperclip/Paperclip/Storage/S3">expiring_url</a>-metod.)</p>
<p>Problemet uppstod när vi skulle använda Flowplayer för att streama direkt ifrån S3, vi vill bara ha verifierade användare som ser våra filmer. Det blev strul med tecknen i de meckiga länkarna som S3 genererar och Flowplayer kunde inte läsa de rakt av.</p>
<p>Vi använde oss då av en &#8221;<a href="http://flowplayer.org/demos/installation/alternate/index.html">Alternate embedding method</a>&#8221; aka. flashembed.js. Så här beskrivs <a href="http://flowplayer.org/tools/demos/toolbox/flashembed/flowplayer.html"> &#8221;Flashembed and Flowplayer&#8221;</a>: </p>
<blockquote><p>Flowplayer is a regular Flash component just like any other Flash component. It can be placed on your page with this tool or by using HTML-based object tags or you can use our &#8221;competitor&#8221;, SWFObject.</p></blockquote>
<p>Sedan när vi skickade den &#8221;säkra&#8221; länken från <strong>controllern</strong> till viewn så fick vi manuellt ersätta lite tecken:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@media</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:flash_url</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@media</span>.<span style="color:#9900CC;">media_file</span>.<span style="color:#9900CC;">expiring_url</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&amp;&quot;</span>, <span style="color:#996600;">&quot;%26&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;?&quot;</span>, <span style="color:#996600;">&quot;%3F&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;=&quot;</span>, <span style="color:#996600;">&quot;%3D&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>För att embedda den i viewn så får du även se till att säga åt Rails att det är en säker länk (med hjälp av html_safe):</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a id=&quot;player&quot; style=&quot;width:400px;height:280px;&quot;&gt;&lt;/a&gt;
&lt;script language=&quot;JavaScript&quot;&gt;
flashembed(&quot;player&quot;, &quot;http://releases.flowplayer.org/swf/flowplayer-3.2.2.swf&quot;, {
    config: {
        clip: { url: '&lt;%= @media.flash_url.html_safe %&gt;', autoPlay: false },
        plugins: { controlbar:null },
        autoPlay: false
    }
});
&lt;/script&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>/2010/08/01/riding-the-rails-amazon-s3-flowplayer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Riding the Rails: Aktiva menyobjekt</title>
		<link>/2010/07/31/riding-the-rails-aktiva-menyobjekt/</link>
		<comments>/2010/07/31/riding-the-rails-aktiva-menyobjekt/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 11:17:49 +0000</pubDate>
		<dc:creator>Anders</dc:creator>
				<category><![CDATA[Programmering]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://hassis.com/?p=325</guid>
		<description><![CDATA[Fredrik W visade mig en riktigt stilig sak häromdagen på ett Rails-projekt vi håller på med. Att göra layouter och framför allt visa vilket menyobjekt som är &#8221;aktivt&#8221;, dvs vilken sida du är på just nu. Lägg in denna helpern, i exempelvis application_helper.rb: def menu_item&#40;text, link, options = &#123;:use_span =&#62; false, :only_controller =&#62; false&#125;&#41; active [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bitsamppixels.com/">Fredrik W</a> visade mig en riktigt stilig sak häromdagen på ett Rails-projekt vi håller på med. Att göra layouter och framför allt visa vilket menyobjekt som är &#8221;aktivt&#8221;, dvs vilken sida du är på just nu. </p>
<p>Lägg in denna helpern, i exempelvis 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> menu_item<span style="color:#006600; font-weight:bold;">&#40;</span>text, link, options = <span style="color:#006600; font-weight:bold;">&#123;</span>:use_span <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:only_controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <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>
   active = Rails.<span style="color:#9900CC;">application</span>.<span style="color:#9900CC;">routes</span>.<span style="color:#9900CC;">recognize_path</span> link
   text = content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;span&quot;</span>, text<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:use_span</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
   link_is_currently_active = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:only_controller</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:controller</span><span style="color:#006600; font-weight:bold;">&#93;</span> == active<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:controller</span><span style="color:#006600; font-weight:bold;">&#93;</span> : params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:controller</span><span style="color:#006600; font-weight:bold;">&#93;</span> == active<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:controller</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:action</span><span style="color:#006600; font-weight:bold;">&#93;</span> == active<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:action</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
   klass = link_is_currently_active ? <span style="color:#996600;">&quot;active&quot;</span> : <span style="color:#996600;">&quot;&quot;</span>
   <span style="color:#0000FF; font-weight:bold;">return</span> content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;li&quot;</span>, link_to<span style="color:#006600; font-weight:bold;">&#40;</span>text, link<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;">=&gt;</span> klass<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> menu<span style="color:#006600; font-weight:bold;">&#40;</span>options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ul&quot;</span>, options, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Vi läser alltså av existerande routen och jämför den med menyobjektets. Så nu istället för att länk runt dig i applikationen med <em>link_to</em> så kan du nu använda blocket <em>menu</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> menu <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= menu_item <span style="color:#996600;">&quot;Link 1&quot;</span>, project_path<span style="color:#006600; font-weight:bold;">&#40;</span>@project<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= menu_item <span style="color:#996600;">&quot;Link 2&quot;</span>, project_medias_path<span style="color:#006600; font-weight:bold;">&#40;</span>@project<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Vips, så har vi nu fått en lista med det aktiva objektets klass som &#8221;<em>active</em>&#8221;. </p>
<p>Nu är det klart att detta är en väldigt begränsad lösning och kanske inte lämpar sig i större projekt med stora menystrukturer, men för enklare projekt fungerar detta alldeles utmärkt.</p>
]]></content:encoded>
			<wfw:commentRss>/2010/07/31/riding-the-rails-aktiva-menyobjekt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Object Caching 399/399 objects using memcached

Served from: hassis.com @ 2012-02-04 03:48:26 -->
