<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>acts_as_blog</title>
	
	<link>http://actsasblog.de</link>
	<description>...or so ;-)</description>
	<lastBuildDate>Thu, 05 Apr 2012 20:44:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/actsasblog" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="actsasblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>jQuery Autocomplete like facebook/twitter/quora</title>
		<link>http://actsasblog.de/index.php/2011/10/28/jquery-autocomplete-like-facebooktwitterquora/</link>
		<comments>http://actsasblog.de/index.php/2011/10/28/jquery-autocomplete-like-facebooktwitterquora/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 14:57:46 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=203</guid>
		<description><![CDATA[Do you know the autocomplete for usernames on twitter? If you enter and @-sign into the textbox, a autocomplete will be shown. I havn&#8217;t found an existing plugin for that, so I have written one. autocompleteTrigger is based on jQuery-UI Autocomplete and set/overwrite some functions only. Url: https://github.com/experteer/autocompleteTrigger Demo: http://jsfiddle.net/dmattes/2YRgW/1/ Code Example $('input,textarea').autocompleteTrigger({ triggerStart : [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know the autocomplete for usernames on twitter?</p>
<p>If you enter and @-sign into the textbox, a autocomplete will be shown.</p>
<p><a href="http://actsasblog.de/index.php/2011/10/28/jquery-autocomplete-like-facebooktwitterquora/jquery-autocomplete-trigger/" rel="attachment wp-att-211"><img class="aligncenter size-medium wp-image-211" title="jquery-autocomplete-trigger" src="http://actsasblog.de/wp-content/uploads/2011/10/jquery-autocomplete-trigger-300x171.png" alt="" width="300" height="171" /></a></p>
<p>I havn&#8217;t found an existing plugin for that, so I have written one. autocompleteTrigger is based on jQuery-UI Autocomplete and set/overwrite some functions only.</p>
<p>Url: <a href="https://github.com/experteer/autocompleteTrigger">https://github.com/experteer/autocompleteTrigger</a></p>
<p>Demo: <a href="http://jsfiddle.net/dmattes/2YRgW/1/">http://jsfiddle.net/dmattes/2YRgW/1/</a></p>
<p><strong>Code Example</strong></p>
<pre class="brush: js">$('input,textarea').autocompleteTrigger({
      triggerStart : '@',
      triggerEnd: '',
      source: [
        "Asp",
        "BASIC",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
      ]
    });</pre>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2011/10/28/jquery-autocomplete-like-facebooktwitterquora/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set constants through database</title>
		<link>http://actsasblog.de/index.php/2010/01/17/set-constants-through-database/</link>
		<comments>http://actsasblog.de/index.php/2010/01/17/set-constants-through-database/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 13:19:21 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=127</guid>
		<description><![CDATA[Dan Chak explained in his book Enterprise Rails, why domain data should be stored in database tables. Domain data don&#8217;t change frequently and normally not through an interaction with the application. For example, domain data can be the roles of users. Role.create(:key => 'user', :description => 'A normal user') Role.create(:key => 'admin', :description => 'The [...]]]></description>
			<content:encoded><![CDATA[<p>Dan Chak explained in his book <a href="http://enterpriserails.chak.org/" target="_blank">Enterprise Rails</a>, why  <em>domain  data</em> should be stored in database tables. <em>Domain  data</em> don&#8217;t change frequently and normally not through an interaction with the application.</p>
<p>For example, domain data can be the roles of users.</p>
<pre class="brush: ruby">
Role.create(:key => 'user', :description => 'A normal user')
Role.create(:key => 'admin', :description => 'The admin of the page')
</pre>
<p>The role constants can be set in the following way (based on the example in <em>Enterprise Rails, Chapter 7)</em></p>
<pre class="brush: ruby">
class Role < ActiveRecord::Base
  USER = Role.find_by_key('user')
  ADMIN = Role.find_by_key('admin')
end
</pre>
<p>Now you can use this constants like</p>
<pre class="brush: ruby">
my_user.role = Role::ADMIN
</pre>
<p>My solution is, to set the constants dynamicly</p>
<pre class="brush: ruby">
class Role < ActiveRecord::Base
  # set role contants like Role::ADMIN,..
  Role.all.each do |role|
    Role.const_set(role.key.upcase, role)
  end
end
</pre>
<p>The big advantage by setting a constant through the database is that the select query will be executed only once for a running application. If you restart the application, the constants will be refreshed.</p>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2010/01/17/set-constants-through-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Update HeidiSQL on Ubuntu with Shell-Script</title>
		<link>http://actsasblog.de/index.php/2010/01/17/update-heidisql-on-ubuntu-with-shell-script/</link>
		<comments>http://actsasblog.de/index.php/2010/01/17/update-heidisql-on-ubuntu-with-shell-script/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 11:49:04 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=147</guid>
		<description><![CDATA[For working with my MySQL database on Ubuntu, I&#8217;m using HeidiSQL under Wine. Each day, HeidiSQL publishs a Nightly Build, which I wanted to have. Therefore I wrote a little shell-script, which makes that work for me. #!/bin/bash path=/home/mattesd/.wine/drive_c/prog/HeidiSQL abs_filename=$path/heidisql.`date +%s`.exe wget http://www.heidisql.com/latest_build.php -O $abs_filename rm $path/heidisql.exe ln -s $abs_filename $path/heidisql.exe Each build will be [...]]]></description>
			<content:encoded><![CDATA[<p>For working with my MySQL database on Ubuntu, I&#8217;m using <a href="http://www.heidisql.com/" target="_blank">HeidiSQL</a> under <a href="http://www.winehq.org/" target="_blank">Wine</a>.</p>
<p>Each day, HeidiSQL publishs a <em>Nightly Build</em>, which I wanted to have.<br />
Therefore I wrote a little shell-script, which makes that work for me.</p>
<pre class="brush: ruby">
#!/bin/bash
path=/home/mattesd/.wine/drive_c/prog/HeidiSQL
abs_filename=$path/heidisql.`date +%s`.exe

wget http://www.heidisql.com/latest_build.php -O $abs_filename
rm $path/heidisql.exe
ln -s $abs_filename $path/heidisql.exe
</pre>
<p>Each build will be downloaded as <em>heidisql.&lt;TIMESTAMP&gt;.exe</em> (e. g. heidisql.1263728741.exe) and linked to heidisql.exe.</p>
<p><strong>Note:</strong><br />
I created the softlink <em>prog</em> before.<br />
<code>ln -s home/mattesd/.wine/drive_c/Program\ Files home/mattesd/.wine/drive_c/prog</code></p>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2010/01/17/update-heidisql-on-ubuntu-with-shell-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Railsroad model diagram with magic fields</title>
		<link>http://actsasblog.de/index.php/2010/01/17/railsroad-model-diagram-with-magic-fields/</link>
		<comments>http://actsasblog.de/index.php/2010/01/17/railsroad-model-diagram-with-magic-fields/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 11:24:33 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[gems]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=152</guid>
		<description><![CDATA[RailRoad is a very good class diagram generator for Ruby on Rails applications. Roy Wright modified it to support the state machine AASM. Following you see an example of a model diagram, generated by railroad -Mt &#124; neato -Tpng > models_default.png This is very great, but in bigger projects, I wanted to see the magic [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://railroad.rubyforge.org/" target="_blank">RailRoad</a> is a very good class diagram generator for <a href="http://rubyonrails.org/" target="_blank">Ruby on Rails</a> applications.</p>
<p><a href="http://github.com/royw/railroad_xing" target="_blank">Roy Wright</a> modified it to support the state machine <a href="http://github.com/rubyist/aasm/" target="_blank">AASM</a>.</p>
<p>Following you see an example of a model diagram, generated by<br />
<code>railroad -Mt | neato -Tpng > models_default.png</code><br />
<a href="http://actsasblog.de/wp-content/uploads/2010/01/models_default.png"><img class="aligncenter size-medium wp-image-160" title="models_default" src="http://actsasblog.de/wp-content/uploads/2010/01/models_default-300x166.png" alt="" width="300" height="166" /></a></p>
<p>This is very great, but in bigger projects, I <strong>wanted to see the magic fields </strong>like <em>id, user_id, post_id,&#8230;</em></p>
<p>Therefore, I modified the gem by adding an additional parameter  -<em>-show-magic</em> to show the magic fields.</p>
<p><code>railroad -Mt --show-magic | neato -Tpng > models_with_magics.png</code> creates the following diagram</p>
<p><a href="http://actsasblog.de/wp-content/uploads/2010/01/models_with_magics.png"><img class="aligncenter size-medium wp-image-161" title="models_with_magics" src="http://actsasblog.de/wp-content/uploads/2010/01/models_with_magics-300x226.png" alt="" width="300" height="226" /></a></p>
<p>The plugin can be installed via gemcutter.org<br />
<code>gem install dmattes-railroad_xing</code></p>
<p>Source is available on Github: <a href="http://github.com/dmattes/railroad_xing" target="_blank">http://github.com/dmattes/railroad_xing</a></p>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2010/01/17/railsroad-model-diagram-with-magic-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails (ActiveRecord) – Get changed attributes with update_attributes</title>
		<link>http://actsasblog.de/index.php/2009/06/04/rails-activerecord-get-changed-attributes-with-update_attributes/</link>
		<comments>http://actsasblog.de/index.php/2009/06/04/rails-activerecord-get-changed-attributes-with-update_attributes/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 16:39:03 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=110</guid>
		<description><![CDATA[&#160; I had to update some attributes of an object. This can easily be done by my_obj.update_attributes(params). If you want to know the updated attributes &#8230;it&#8217;s not so easy. update_attributes internally calls the save-method, so that my_obj.changes can&#8217;t be called after update_attributes. My solution was to patch ActiveRecord, so that it additionally returns the changed [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>I had to update some attributes of an object. This can easily be done by <em>my_obj.update_attributes(params)</em>. If you want to know the updated attributes &#8230;it&#8217;s not so easy.</p>
<p><em>update_attributes</em> internally calls the save-method, so that <em>my_obj.changes</em> can&#8217;t be called after update_attributes. My solution was to patch ActiveRecord, so that it additionally returns the changed attributes.</p>
<p><strong>Usage example </strong></p>
<pre class="brush: ruby">status, changes = my_obj.update_attributes_changed(params)</pre>
<p><strong>config/initializers/active_record_patch.rb</strong></p>
<pre class="brush: ruby">module ActiveRecord
  class Base
    def update_attributes_changed(attributes)
      self.attributes = attributes
      changes = self.changes
      return save, changes
    end
  end
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2009/06/04/rails-activerecord-get-changed-attributes-with-update_attributes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Paperclip – Reprocess in Background</title>
		<link>http://actsasblog.de/index.php/2009/05/28/paperclip-reprocess-in-background/</link>
		<comments>http://actsasblog.de/index.php/2009/05/28/paperclip-reprocess-in-background/#comments</comments>
		<pubDate>Thu, 28 May 2009 22:28:46 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[paperclip]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=94</guid>
		<description><![CDATA[In my last blog about paperclip I described the rotation of images. Since I have added more styles with different sizes for the attachment, I got some performance problems, because for every image, 7 sizes has to be reprocessed and this took a long of time. My solution is, to handle the reprocessing in background [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://actsasblog.de/index.php/2009/03/27/rails-and-fileuploads-with-paperclip-hashed-filename-and-rotating-images/">blog</a> about paperclip I described the rotation of images.<br />
Since I have added more <em>styles </em> with different sizes for the attachment, I got some performance problems, because for every image, 7 sizes has to be reprocessed and this took a long of time. </p>
<p>My solution is, to handle the reprocessing in background with <a href="http://github.com/tobi/delayed_job/tree/master">delayed_job</a> and <a href="http://github.com/rubyist/aasm/tree/master">aasm (acts_as_state_machine)</a>.</p>
<p><strong>Media-Model</strong><br />
This is the &#8220;normal&#8221; Media-Model. First at all, there is AASM included, to see the current state (pending, error, ready) of processing. Furthermore, to show the image, I create a <em>url</em> method, which give back the path to the image (if the state is :ready) or an placeholder (if the state is NOT :ready).</p>
<p><em>media.rb</em></p>
<pre class="brush: ruby">
class Media

  has_attached_file( :source,
	:styles => {
	:bigger => '1600x1600>',
	:big => '800x600>',
	:album => '560x420>',
	:album_preview => '150x150>',
	:album_folder => '70x70#',
	:profile => '180x250>',
	:avatar => '50x50#' },
	:storage => :filesystem,
  )

  include AASM

  aasm_column :status
  aasm_initial_state :pending

  aasm_state :pending
  aasm_state :error
  aasm_state :ready

  aasm_event :converted do
	transitions :to => :ready, :from => [:pending]
  end

  def url(style = :original)
    if(self.ready?)
      source.url(style)
    else
      # Image is processing, please wait
      Media.find(3).source.url(style)
    end
  end
end
</pre>
<p>&nbsp;</p>
<p><strong>The &#8220;Uploading&#8221; Media-Model</strong><br />
For uploading new images, the model <em>MediaUpload</em> will be used.<br />
This model inherits from the Media-Model, but the paperclip configuration will be overriden.<br />
There is only one style available, because it&#8217;s the feedback image for the uploading user.<br />
After saving the image, a new <em>MediaJob</em> will be created in background with the help of delayed_job.</p>
<p><em>media_upload.rb</em></p>
<pre class="brush: ruby">
class MediaUpload < Media

  # paperclip
  has_attached_file( :source,
    :styles => { :avatar => '50x50#' },
      :storage => :filesystem
  )

  after_create :create_all_styles

  def create_all_styles
    Delayed::Job.enqueue MediaJob.new(self.id)
  end
end
</pre>
<p>&nbsp;</p>
<p><strong>The REPROCESSOR</strong><br />
At least, the media-job created all styles for the image &#8230;and it&#8217;s DONE.</p>
<p><em>media_jobs.rb</em></p>
<pre class="brush: ruby">
class MediaJob < Struct.new(:id)
  def perform
    m = Media.find(id)
    m.source.reprocess!
  end
end
</pre>
<p><strong>Helpful links:</strong></p>
<ul>
<li><a href="http://eastblue.org/blag/2009/05/07/delaying-paperclip.html">http://eastblue.org/blag/2009/05/07/delaying-paperclip.html</a></li>
<li><a href="http://groups.google.com/group/paperclip-plugin/browse_thread/thread/8af52cc25cd97d02/05221d1a7254601d?lnk=gst&amp;q=background&amp;pli=1">http://groups.google.com/group/paperclip-plugin/browse_thread/thread/8af52cc25cd97d02/05221d1a7254601d?lnk=gst&amp;q=background&amp;pli=1</a></li>
<li><a href="http://github.com/tobi/delayed_job/tree/master">http://github.com/tobi/delayed_job/tree/master</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2009/05/28/paperclip-reprocess-in-background/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Closing a boxy modal-window (jquery-plugin)</title>
		<link>http://actsasblog.de/index.php/2009/05/10/closing-a-boxy-modal-window-jquery-plugin/</link>
		<comments>http://actsasblog.de/index.php/2009/05/10/closing-a-boxy-modal-window-jquery-plugin/#comments</comments>
		<pubDate>Sun, 10 May 2009 12:57:20 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=65</guid>
		<description><![CDATA[I&#8217;m using Boxy to display a modal window to edit privacy settings. The data of the dialog will be loaded by an ajax-call. For closing the dialog, the following code is described in the boxy api, but it doesn&#8217;t run for me. It runs for a &#8220;normal&#8221; boxy window, but not for an ajax-window. Close [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using Boxy to display a modal window to edit privacy settings.<br />
The data of the dialog will be loaded by an ajax-call.</p>
<p><img class="aligncenter size-full wp-image-72" title="privacy_edit" src="http://actsasblog.de/wp-content/uploads/2009/05/privacy_edit.png" alt="privacy_edit" width="438" height="182" /></p>
<p>For closing the dialog, the following code is described in the boxy api, but it doesn&#8217;t run for me.<br />
It runs for a &#8220;normal&#8221; boxy window, but not for an ajax-window.</p>
<pre class="brush: ruby"><a onclick="Boxy.get(this).hide();" href="#">Close dialog</a>.</pre>
<p>My solution is, to &#8220;link&#8221; the modal-window to an DOM element. You can do this, by using the <em>actuator</em> option.<br />
In this example, the DOM element is a link with the id &#8220;address_name&#8221; and it opens the modal-window.</p>
<pre class="brush: ruby"><a id="address_name" title="Privatsphäre" onclick="new Boxy.load('/users/1/privacy/edit?cls=address&amp;field=name&amp;section=general', {title: 'Privatsphäre', modal: true, hideShrink: '', actuator: $('#address_name')[0]})" href="#">
<img src="/images/private.png?1233668167" alt="Nur für mich" />
</a></pre>
<p>For closing the modal-window, you can get the boxy object with <em>Boxy.linkedTo()</em> and can close it with <em>hide()</em> method.</p>
<pre class="brush: ruby">function boxy_hide(obj_name){
  Boxy.linkedTo($('#' + obj_name)[0]).hide();
}</pre>
<p><strong>Helpful links:</strong></p>
<ul>
<li><a href="http://onehackoranother.com/projects/jquery/boxy/index.html">http://onehackoranother.com/projects/jquery/boxy/index.html</a></li>
<li><a href="http://github.com/jaz303/boxy/tree/master">http://github.com/jaz303/boxy/tree/master</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2009/05/10/closing-a-boxy-modal-window-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails and Fileuploads with Paperclip – Hashed filename and rotating images</title>
		<link>http://actsasblog.de/index.php/2009/03/27/rails-and-fileuploads-with-paperclip-hashed-filename-and-rotating-images/</link>
		<comments>http://actsasblog.de/index.php/2009/03/27/rails-and-fileuploads-with-paperclip-hashed-filename-and-rotating-images/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 09:13:31 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[paperclip]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=48</guid>
		<description><![CDATA[In my current app, I&#8217;m using paperclip for handling images. Paperclip includes the option for id_partition, but I prefer an hashed stucture (descriped in my first blog). For example, the image url looks like http://my_url/medias/0d/898/0d898414092854eacbf27f8db12ce4db_avatar.jpg This is the basic configuration in my Media-Model # paperclip has_attached_file( :source, :styles => { :original => '1600x1600>', :avatar => [...]]]></description>
			<content:encoded><![CDATA[<p>In my current app, I&#8217;m using <a href="http://www.thoughtbot.com/projects/paperclip">paperclip</a> for handling images.</p>
<p>Paperclip includes the option for id_partition, but I prefer an <strong>hashed stucture</strong> (descriped in my first <a href="http://actsasblog.de/index.php/2008/12/16/rails-verzeichnisstruktur-fur-bilderaudiovideos/">blog</a>).</p>
<p>For example, the image url looks like</p>
<p>http://my_url/medias/0d/898/0d898414092854eacbf27f8db12ce4db_avatar.jpg</p>
<p><strong>This is the basic configuration in my Media-Model</strong></p>
<pre class="brush: ruby">
  # paperclip
  has_attached_file( :source,
    :styles => {
      :original => '1600x1600>',
      :avatar => '50x50#' },
    :storage => :filesystem,
    :url => "/:class/:hashed_public_id/:public_id_:style.:extension",
    :path => ":rails_root/public/:class/:hashed_public_id/:public_id_:style.:extension"
  )
</pre>
<p>To set the public id, I used the following code and create an md5-hash.<br />
Surely, you can use what you want, for example ActiveSupport::SecureRandom.hex(32).<br />
My <em>set_public_id</em> creates a random string like &#8220;6e374ca829eaead5090f6cdd26c08017&#8243;.</p>
<pre class="brush: ruby">
  def set_public_id
    self.public_id = Digest::MD5.hexdigest(self.source_file_name + self.source_file_size.to_s + rand.to_s + Time.now.to_i.to_s)
  end
</pre>
<p>For using <strong>hashed_public_id</strong> or <strong>public_id</strong> in the paperclip configuration <em>:url => &#8220;/:class/:hashed_public_id/:public_id_:style.:extension&#8221;</em>, you must set the following attachment interpolations.</p>
<pre class="brush: ruby">
  Paperclip::Attachment.interpolations[:public_id] = lambda do |attachment, style|
    # e. g. "6e374ca829eaead5090f6cdd26c08017"
    attachment.instance.public_id
  end

  Paperclip::Attachment.interpolations[:hashed_public_id] = lambda do |attachment, style|
    # e. g. "6e/374"
    File.join(attachment.instance.public_id[0..1], attachment.instance.public_id[2..4])
  end
</pre>
<p>Furthermore, I need to <strong>rotate an image</strong>. Therefore, I used the follwing code.<br />
All styles of the image (e. g. original, avatar) will be rotated.</p>
<pre class="brush: ruby">
require 'RMagick'
def rotate(degrees)

    if(degrees.class == Fixnum &#038;&#038; degrees % 90 == 0)
      each_attachment do |n, a|
        self.source.styles.each do |styles|
          image_path = a.path(styles.first)

          image   = Magick::ImageList.new(image_path)
          image   = image.rotate(degrees)
          image.write(image_path)
        end
        return true
      end
    end
    nil

  end
</pre>
<p><strong>Helpful links:</strong></p>
<ul>
<li><a href="http://addictedtonew.com/archives/127/rotating-images-with-rmagick-and-ruby-on-rails/">http://addictedtonew.com/archives/127/rotating-images-with-rmagick-and-ruby-on-rails/</a></li>
<li><a href="http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/">http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/</a></li>
<li><a href="http://wiki.github.com/thoughtbot/paperclip/interpolations">http://wiki.github.com/thoughtbot/paperclip/interpolations</a></li>
<li><a href="http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip">http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2009/03/27/rails-and-fileuploads-with-paperclip-hashed-filename-and-rotating-images/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>RSpec Helper for testing accessible attributes</title>
		<link>http://actsasblog.de/index.php/2009/03/24/rspec-helper-for-testing-accessible-attributes/</link>
		<comments>http://actsasblog.de/index.php/2009/03/24/rspec-helper-for-testing-accessible-attributes/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 08:17:09 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://actsasblog.de/?p=30</guid>
		<description><![CDATA[I using RSpec for testing my Ruby on Rails app. For testing, if the correct attr_accessible are set, I use the following code. My Class Media class Media < ActiveRecord::Base attr_accessible :caption end Usage in my media_spec.rb it "should have protected attributes" do @media = Media.create!(@valid_attributes) @media.should accessible_attributes(:caption) end Here the used code in lib/rspec_attributes_accessible.rb [...]]]></description>
			<content:encoded><![CDATA[<p>I using RSpec for testing my Ruby on Rails app.<br />
For testing, if the correct <em>attr_accessible</em> are set, I use the following code.</p>
<p>My Class <em>Media</em></p>
<pre class="brush: ruby">
class Media < ActiveRecord::Base
  attr_accessible :caption
end
</pre>
<p>Usage in my <em>media_spec.rb</em></p>
<pre class="brush: ruby">
it "should have protected attributes" do
  @media = Media.create!(@valid_attributes)
  @media.should accessible_attributes(:caption)
end
</pre>
<p>Here the used code in <em>lib/rspec_attributes_accessible.rb</em></p>
<pre class="brush: ruby">
module RspecAttributesAccessible
  class AccessibleAttributes

    def initialize(*attributes)
      @attributes = attributes.to_a
    end

    def matches?(target)
      @target = target
      calculate_accessible_attributes()
      perform_check()
    end

    def failure_message
      "expected #{@failed_attribute} to be accessible"
    end

    def negative_failure_message
      "expected #{@failed_attribute} to not be accessible"
    end

    private

    def calculate_accessible_attributes()
      attr_access = @target.class.send("attr_accessible").to_a.map(&#038;:to_sym)
      @failed_attribute = []
      if((@attributes - attr_access).length != 0 or (attr_access - @attributes).length != 0)
        @failed_attribute << (@attributes - attr_access)
        @failed_attribute << (attr_access - @attributes)
      end
      nil
    end   

    def perform_check()
      @failed_attribute.empty?
    end
  end

  def accessible_attributes(*attributes)
    AccessibleAttributes.new(*attributes)
  end
end
</pre>
<p>You have to include the lib in your <em>spec_helper.rb</em></p>
<pre class="brush: ruby">
Spec::Runner.configure do |config|
   config.include RspecAttributesAccessible # lib/rspec_attributes_accessible.rb
end
</pre>
<p>Useful links:</p>
<ul>
<li><a href="http://railspikes.com/2008/9/22/is-your-rails-application-safe-from-mass-assignment">http://railspikes.com/2008/9/22/is-your-rails-application-safe-from-mass-assignment</a></li>
<li><a href="http://afreshcup.com/2008/12/14/auditing-for-attr_accessible/">http://afreshcup.com/2008/12/14/auditing-for-attr_accessible/</a></li>
<li><a href="http://snippets.dzone.com/posts/show/4712">http://snippets.dzone.com/posts/show/4712</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2009/03/24/rspec-helper-for-testing-accessible-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails – Verzeichnisstruktur für Bilder/Audio/Videos</title>
		<link>http://actsasblog.de/index.php/2008/12/16/rails-verzeichnisstruktur-fur-bilderaudiovideos/</link>
		<comments>http://actsasblog.de/index.php/2008/12/16/rails-verzeichnisstruktur-fur-bilderaudiovideos/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 08:46:00 +0000</pubDate>
		<dc:creator>daniel.mattes</dc:creator>
				<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://actsasblog.de/index.php/2008/12/16/rails-verzeichnisstruktur-fur-bilderaudiovideos/</guid>
		<description><![CDATA[Webseite mit sehr vielen Bildern, Audio-Dateien und Videos sollten nicht alle hochgeladenen Medien in ein Verzeichnis legen &#8230;sonst mag *nix bald nicht mehr ;-) Hier meine verwendete Lösung, ausgelagert in lib/hashed_dir_structure.rb module HashedDirStructure #p_filename md5 encrypted or number def HashedDirStructure.get_path_for_save(p_base_path, p_filename) if( p_filename.to_s.length != 32) p_filename = ("%05d" % p_filename) end part16 = p_filename[0..1] part256 [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: verdana;">Webseite mit sehr vielen Bildern, Audio-Dateien und Videos sollten nicht alle hochgeladenen Medien in ein Verzeichnis legen &#8230;sonst mag *nix bald nicht mehr ;-)</span></p>
<p><span style="font-family: verdana;">Hier meine verwendete Lösung, ausgelagert in lib/hashed_dir_structure.rb</span></p>
<pre class="brush: ruby">module HashedDirStructure

  #p_filename md5 encrypted or number
  def HashedDirStructure.get_path_for_save(p_base_path, p_filename)

    if( p_filename.to_s.length != 32)
      p_filename = ("%05d" % p_filename)
    end

    part16 = p_filename[0..1]
    part256 = p_filename[2..4]

    path = File.join(p_base_path, part16, part256)

    # ensure the directory exists...
    FileUtils.mkdir_p(path)

    return File.join(p_base_path, part16, part256)
  end

  def HashedDirStructure.get_path_for_url(p_filename)

    if( p_filename.to_s.length != 32)
      p_filename = ("%05d" % p_filename)
    end

    part16 = p_filename[0..1]
    part256 = p_filename[2..4]

    return File.join(part16, part256)
  end

end</pre>
<p><span style="font-weight: bold; font-family: verdana;">Verwendung:<br />
</span></p>
<p><span style="font-family: verdana;">Jedes Bild/Audio/Video ist als Medien-Objekt in der DB vorhanden. Diesen Objekten ist jeweils eine Public-ID zugewiesen, was ein MD5-Hash ist.</span></p>
<pre class="brush: ruby">class Media &lt; ActiveRecord::Base
  require 'digest/md5'

  # setzen der public_id
  # z. B. 6e374ca829eaead5090f6cdd26c08017
  def set_public_id
    self.public_id = Digest::MD5.hexdigest(rand.to_s)
  end

  def save
    # hashed_path hat z. B. folgenden Inhalt
    # "/www/medias/6e/374"
    hashed_path =" HashedDirStructure.get_path_for_save("/www/medias", self.public_id)

    # absoluter Dateiname zum speichern
    # "/www/medias/6e/374/6e374ca829eaead5090f6cdd26c08017.jpg"
    filepath = "#{hashed_path}/#{self.public_id}.jpg"

    #...jetzt kann das speichern beginnen
  end
end</pre>
<p><span style="font-family: verdana;">Die Verzeichnisstruktur sieht dann wie folgt aus.</span></p>
<pre class="Ruby">/www/medias
          /6e
          /...
          /6e/374
          /6e/7c0
          /6e/...
          /6e/374/6e374ca829eaead5090f6cdd26c08017.jpg</pre>
<p><span style="font-weight: bold; font-family: verdana;">Links:</span></p>
<ul>
<li><a href="http://jxh.bingodisk.com/bingo/public/presentations/JHoffmanRailsConf-Berlin-Sept2007.pdf">http://jxh.bingodisk.com/bingo/public/presentations/JHoffmanRailsConf-Berlin-Sept2007.pdf</a></li>
<li><a href="http://www.37signals.com/svn/archives2/id_partitioning.php">http://www.37signals.com/svn/archives2/id_partitioning.php</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://actsasblog.de/index.php/2008/12/16/rails-verzeichnisstruktur-fur-bilderaudiovideos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

