<?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>le-matt. &#187; ruby on rails</title>
	<atom:link href="http://www.any-where.de/blog/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.any-where.de/blog</link>
	<description>news from the world (the real one and the virtual)</description>
	<lastBuildDate>Sun, 15 Aug 2010 13:24:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby Hash: Convert String keys to Symbols</title>
		<link>http://www.any-where.de/blog/ruby-hash-convert-string-keys-to-symbols/</link>
		<comments>http://www.any-where.de/blog/ruby-hash-convert-string-keys-to-symbols/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 14:55:06 +0000</pubDate>
		<dc:creator>Matthias Müller</dc:creator>
				<category><![CDATA[Programming the web]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.any-where.de/blog/?p=146</guid>
		<description><![CDATA[Ever had a hash which contained strings as keys and needed symbols instead? I do: From a REXML::Document I created a hash. Unfortunately, the keys were all strings and I needed them to be symbols at some other point in the app. Thank god, Ruby is capable of extending any object &#8220;on the fly&#8221;. So, [...]]]></description>
			<content:encoded><![CDATA[<p>Ever had a hash which contained strings as keys and needed symbols instead? I do: From a REXML::Document I created a hash. Unfortunately, the keys were all strings and I needed them to be symbols at some other point in the app. Thank god, Ruby is capable of extending any object &#8220;on the fly&#8221;. So, I wrote a simple extension to Hash, which you might find useful as well:</p>
<pre>class Hash
  #take keys of hash and transform those to a symbols
  def self.transform_keys_to_symbols(value)
    return value if not value.is_a?(Hash)
    hash = value.inject({}){|memo,(k,v)| memo[k.to_sym] = Hash.transform_keys_to_symbols(v); memo}
    return hash
  end
end
</pre>
<p>Usage is:</p>
<pre>a = { "key" =&gt; 123, "inner_hash" =&gt; { "another_key" =&gt; "blabla" }}
Hash.transform_keys_to_symbols(a)
#returns
a = { :key =&gt; 123, :inner_hash =&gt; { :another_key =&gt; "blabla" }}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.any-where.de/blog/ruby-hash-convert-string-keys-to-symbols/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Passenger and OpenID</title>
		<link>http://www.any-where.de/blog/passenger-and-openid/</link>
		<comments>http://www.any-where.de/blog/passenger-and-openid/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 09:10:40 +0000</pubDate>
		<dc:creator>Matthias Müller</dc:creator>
				<category><![CDATA[Programming the web]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[openid]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.any-where.de/blog/?p=121</guid>
		<description><![CDATA[I recently updated Passenger 2.0.6 to 2.2.4 and experienced that openID logins were not working anymore. Seems like it tries to log output to some strange log location.
However: If anyone comes across the same problem: Simply add
OpenID::Util.logger = RAILS_DEFAULT_LOGGER
to your environment.rb file and everything will work like a treat again.
]]></description>
			<content:encoded><![CDATA[<p>I recently updated Passenger 2.0.6 to 2.2.4 and experienced that openID logins were not working anymore. Seems like it tries to log output to some strange log location.</p>
<p>However: If anyone comes across the same problem: Simply add</p>
<pre>OpenID::Util.logger = RAILS_DEFAULT_LOGGER</pre>
<p>to your environment.rb file and everything will work like a treat again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.any-where.de/blog/passenger-and-openid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails&#8217; to_xml w/ multiple associations.</title>
		<link>http://www.any-where.de/blog/rails-to_xml-with-multiple-associations/</link>
		<comments>http://www.any-where.de/blog/rails-to_xml-with-multiple-associations/#comments</comments>
		<pubDate>Thu, 14 May 2009 14:43:55 +0000</pubDate>
		<dc:creator>Matthias Müller</dc:creator>
				<category><![CDATA[Programming the web]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[multiple associations]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[to_xml]]></category>

		<guid isPermaLink="false">http://www.any-where.de/blog/?p=94</guid>
		<description><![CDATA[This is a pain and took me about an hour to figure out today.
Rails ActiveRecord instances offer a nice function to render xml:
my_model.to_xml()
This method can be fed with a parameter
:include => []
 to have all associations being integrated in the XML tree, just like when calling
my_model = MyModel.find( :include =&#62; [association])
But when I tried to [...]]]></description>
			<content:encoded><![CDATA[<p>This is a pain and took me about an hour to figure out today.</p>
<p>Rails ActiveRecord instances offer a nice function to render xml:</p>
<pre>my_model.to_xml()</pre>
<p>This method can be fed with a parameter
<pre>:include => []</pre>
<p> to have all associations being integrated in the XML tree, just like when calling</p>
<pre>my_model = MyModel.find( :include =&gt; [association])</pre>
<p>But when I tried to use it, it always failed when dealing with nested associations. Until I found out the trick.<br />
This is how it works: Basically you need to build a list of nested hashes.</p>
<pre>class Book &lt; ActiveRecord::Base
  has_many :pages
  has_one <img src='http://www.any-where.de/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> wner
end</pre>
<pre>class Owner &lt; ActiveRecord::Base
  has_many :books
end</pre>
<pre>class Page &lt; ActiveRecord::Base
  has_many :spots_of_coffees
  belongs_to :book
end</pre>
<pre>class SpotOfCoffee &lt; ActiveRecord::Base
  belongs_to :page
end</pre>
<p>Now, you want to create a nice XML output containing a book, with its owner, pages and all spots of coffee?<br />
Pretty easy:</p>
<pre>
  @book = Book.first
  includes = {} # let's build a hash; it's easier to read. At least for me...
  includes[:owner] = {} # owner has no association. So, let's take an empty hash as target
  includes[:pages] = { :include => :spots_of_coffee } #load pages and include its spots of coffee

  respond_to do |format|
    format.xml  { render :text => @book.to_xml(:include => includes) }
  end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.any-where.de/blog/rails-to_xml-with-multiple-associations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails: Custom image sizes using has_attachment</title>
		<link>http://www.any-where.de/blog/rails-custom-image-sizes-using-has_attachment/</link>
		<comments>http://www.any-where.de/blog/rails-custom-image-sizes-using-has_attachment/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 07:27:32 +0000</pubDate>
		<dc:creator>Matthias Müller</dc:creator>
				<category><![CDATA[Programming the web]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.any-where.de/blog/?p=70</guid>
		<description><![CDATA[Ever wanted to offer users the possibility to define their very own image sizes for uploaded pictures?
Here&#8217;s how you can quickly do that.
You will have a class acting as a an attachment such as:
class AssetImage &#60; ActiveRecord::Base

has_attachment :content_type =&#62; :image,
:storage =&#62; :file_system,
:max_size =&#62; 1.megabytes,
:path_prefix =&#62; "public/system/images/#{ActiveRecord::Base.configurations[RAILS_ENV]['domain_name']}/assets/images",
:thumbnails =&#62; { :large =&#62; '450&#62;', :normal =&#62; '300', :medium [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to offer users the possibility to define their very own image sizes for uploaded pictures?</p>
<p>Here&#8217;s how you can quickly do that.</p>
<p>You will have a class acting as a an attachment such as:</p>
<pre>class AssetImage &lt; ActiveRecord::Base

has_attachment :content_type =&gt; :image,
:storage =&gt; :file_system,
:max_size =&gt; 1.megabytes,
:path_prefix =&gt; "public/system/images/#{ActiveRecord::Base.configurations[RAILS_ENV]['domain_name']}/assets/images",
:thumbnails =&gt; { :large =&gt; '450&gt;', :normal =&gt; '300', :medium =&gt; '200', :thumbnail =&gt; [100,75] },
:processor =&gt; :rmagick

validates_as_attachment

...</pre>
<p>First add a cattr_accessor to store your custom size in the class:</p>
<pre>cattr_accessor :custom_size</pre>
<p>Now, add a before_save callback which takes the assigned value and adds it to the list of requested image sizes. It is using the value as a minimum size while keeping aspect ratio.</p>
<pre>#store custom size
def before_save
  return if AssetImage.custom_size.nil?
  attachment_options[:thumbnails][:custom] = AssetImage.custom_size.to_s + "&gt;"
end</pre>
<p>In your controller do something like this: Take a value from your parameters and store it in your model to have your image customized.</p>
<pre>def add_image
  AssetImage.custom_size = params[:custom_size].to_i.to_s rescue "100"
  a = AssetImage.new(params[:asset_image])
  a.save
  redirect_to :action =&gt; :index
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.any-where.de/blog/rails-custom-image-sizes-using-has_attachment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use ar_mailer for future mail delivery</title>
		<link>http://www.any-where.de/blog/use-ar_mailer-for-future-mail-delivery/</link>
		<comments>http://www.any-where.de/blog/use-ar_mailer-for-future-mail-delivery/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 11:49:01 +0000</pubDate>
		<dc:creator>Matthias Müller</dc:creator>
				<category><![CDATA[Programming the web]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[ar_mailer]]></category>

		<guid isPermaLink="false">http://www.le-matt.de/blog/use-ar_mailer-for-future-mail-delivery/</guid>
		<description><![CDATA[This little howto describes, how to use ar_mailer to schedule emails for future delivery.
Sorry for the bad formatting. Might change it someday&#8230;
1) Migrate emails table and add needed fields
  add_column :emails, :various, :string #holds classname and id of any object you want to
  add_column :emails, :type, :string #type of email
  add_column :emails, [...]]]></description>
			<content:encoded><![CDATA[<p>This little howto describes, how to use ar_mailer to schedule emails for future delivery.</p>
<p>Sorry for the bad formatting. Might change it someday&#8230;<br />
1) Migrate emails table and add needed fields</p>
<pre>  add_column :emails, :various, :string #holds classname and id of any object you want to
  add_column :emails, :type, :string #type of email
  add_column :emails, :date_to_send, :date #date to send that mail</pre>
<p>2) Override &#8220;def find&#8221; in email.rb</p>
<pre>  def self.find(*args)
    with_scope(:find=&gt;{ :conditions=&gt;["(date_to_send IS NULL OR date_to_send &lt;= ?)", Date.today] }) do
      super(*args)
    end
  end</pre>
<p>3) Create DelayedEmail class, inherting Email</p>
<pre>  class DelayedEmail &lt; Email
    before_save :set_due_date
    before_save :set_various_field
    @@days_before_sending = 21 #default is 3 weeks after creation of various object
    @@various_class = nil

    def set_due_date
      self.date_to_send = Date.today + @@days_before_sending
    end

    def set_various_field
      name = @@various_class.class.to_s + "_" rescue "NOCLASS_"
      id = @@various_class.id.to_s rescue "0"
      self.various = name + id
    end

    #tell email to which object it belongs. might be important for future deletion of unsent mails
    def self.set_various_class(c)
      @@various_class = c
    end

    #tell email how many days shall pass by before sending email
    def self.set_days(days)
      @@days_before_sending = days
    end
  end</pre>
<p>4) Modify ar_mailer standard emailer class in controller before delivery and set it back afterwards</p>
<pre>  DelayedEmail.set_days(10)
  DelayedEmail.set_various_class(SOMEOBJECT)
  ActionMailer::ARMailer.email_class=DelayedEmail
  #Let any mailer send an email through this class
  #eg. Mailer.deliver_mymail(...)
  ActionMailer::ARMailer.email_class=Email</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.any-where.de/blog/use-ar_mailer-for-future-mail-delivery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
