<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Leethal's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 13:36:41 GMT</pubDate>
    <description>DZone Snippets: Leethal's Code Snippets</description>
    <item>
      <title>Array to HTL list helper</title>
      <link>http://snippets.dzone.com/posts/show/4791</link>
      <description>Makes a HTML list out of any array. The objects in the array is yielded to the block. Usage:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;%= list(@posts) { |post| link_to post.title, posts_url(post) } %&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And the actual helper (put it in application_helper.rb):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def list(stuff, &amp;block)&lt;br /&gt;  content_tag(:ul, stuff.map { |thingie| content_tag(:li, yield(thingie)) })&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 17 Nov 2007 05:37:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4791</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
    <item>
      <title>Freeze rails to older version</title>
      <link>http://snippets.dzone.com/posts/show/4730</link>
      <description>This is an explanation of how to make a new, fresh Rails app of any released version of Rails.&lt;br /&gt;&lt;br /&gt;First, generate a new rails app:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;rails my_project&lt;br /&gt;cd my_project&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Find the release you want to use from &lt;a href="http://dev.rubyonrails.org/browser/tags"&gt;this list&lt;/a&gt;. There's a bunch of stuff in there - the Rails releases are prefixed with &lt;i&gt;rel_&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;Then, ender this command to freeze Rails to the version you want. Remember to change the TAG= part (doh).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;rake rails:freeze:edge TAG=rel_1-0-0&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;When this finishes, run the 'rails' command of the rails version you just froze to to have the rails app itself (the stuff in environment/ and so on) turned into the correct version.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;ruby vendor/rails/railties/bin/rails .&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Woot!</description>
      <pubDate>Fri, 02 Nov 2007 09:47:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4730</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
    <item>
      <title>RSpec extension: testing attr_protected and attr_accessible</title>
      <link>http://snippets.dzone.com/posts/show/4712</link>
      <description>Include this module in a spec or inside the Spec::Runner.configure block in the spec_helper.rb. Because i suck, should_not is not working as expected. Usage:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  # either &lt;br /&gt;  attr_accessible :username, :full_name&lt;br /&gt;  # or&lt;br /&gt;  attr_protected :admin, :foo&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# ...&lt;br /&gt;&lt;br /&gt;it "should protect admin and foo" do&lt;br /&gt;  @user.should protect_attributes(:admin, :foo)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here goes:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module CustomExpectations&lt;br /&gt;  class ProtectAttributes&lt;br /&gt;    def initialize(*attributes)&lt;br /&gt;      @attributes = attributes&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def matches?(target)&lt;br /&gt;      @target = target&lt;br /&gt;      &lt;br /&gt;      calculate_protected_methods&lt;br /&gt;      perform_check&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def failure_message&lt;br /&gt;      "expected #{@failed_attribute} to be protected"&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def negative_failure_message&lt;br /&gt;      "expected #{@failed_attribute} to not be protected"&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    private&lt;br /&gt;    &lt;br /&gt;    def calculate_protected_methods&lt;br /&gt;      read = proc {|var| @target.instance_eval { self.class.read_inheritable_attribute(var) } }&lt;br /&gt;      accessible = read.call("attr_accessible")&lt;br /&gt;      protekted = read.call("attr_protected")&lt;br /&gt;      all = @target.class.column_names.map(&amp;:to_sym)&lt;br /&gt;      &lt;br /&gt;      @protected = []&lt;br /&gt;      @protected &lt;&lt; protekted if protekted&lt;br /&gt;      @protected &lt;&lt; (all - accessible) if accessible&lt;br /&gt;      @protected.flatten!&lt;br /&gt;      &lt;br /&gt;      @accessible = all - @protected&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def perform_check&lt;br /&gt;      failed_attributes = (@attributes &amp; @accessible)&lt;br /&gt;      @failed_attribute = failed_attributes.first&lt;br /&gt;      failed_attributes.empty?&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def protect_attributes(attributes)&lt;br /&gt;    ProtectAttributes.new(attributes)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def protect_attribute(attribute)&lt;br /&gt;    ProtectAttributes.new(*[attribute])&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 29 Oct 2007 20:28:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4712</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
    <item>
      <title>Downgrading a new rails app</title>
      <link>http://snippets.dzone.com/posts/show/4687</link>
      <description>Use this code to downgrade to version X of rails. Use the tags found in &lt;a href="http://dev.rubyonrails.org/browser/tags"&gt;the repository&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;rails my_project&lt;br /&gt;cd my_project&lt;br /&gt;&lt;br /&gt;rake rails:freeze:edge TAG=rel_1-1-0&lt;br /&gt;ruby vendor/rails/railties/bin/rails .&lt;br /&gt;&lt;/pre&gt;</description>
      <pubDate>Mon, 22 Oct 2007 13:29:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4687</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
    <item>
      <title>Simple user model with password crypting</title>
      <link>http://snippets.dzone.com/posts/show/4676</link>
      <description>A simple user model. It's using the virtual password attribute 'password' to store the clear-text password. This is what e.g. forms use for password input. It stores this password in the password_hash column. &lt;br /&gt;&lt;br /&gt;It allows for user editing, using the same form as user creation. The password won't be updated, and validations will pass, if the user doesn't touch the password field in the form.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require "digesh/sha1"&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  validates_confirmation_of :password, :if =&gt; :perform_password_validation?&lt;br /&gt;  validates_presence_of :password, :if =&gt; :perform_password_validation?&lt;br /&gt;&lt;br /&gt;  before_save :hash_password&lt;br /&gt;  attr_accessor :password&lt;br /&gt;&lt;br /&gt;  # Returns true if the password passed matches the password in the DB&lt;br /&gt;  def valid_password?(password)&lt;br /&gt;    self.password_hash == self.class.hash_password(password)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  private&lt;br /&gt;&lt;br /&gt;  # Performs the actual password encryption. You want to change this salt to something else.&lt;br /&gt;  def self.hash_password(password, salt = "meeQue8Zucijoo7")&lt;br /&gt;    Dihest::SHA1.hexdigest(password, salt)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # Sets the hashed version of self.password to password_hash, unless it's blank.&lt;br /&gt;  def hash_password&lt;br /&gt;    self.password_hash = self.class.hash_password(self.password) unless self.password.blank?&lt;br /&gt;  end&lt;br /&gt; &lt;br /&gt;  # Assert wether or not the password validations should be performed. Always on new records, only on existing&lt;br /&gt;  # records if the .password attribute isn't blank.&lt;br /&gt;  def perform_password_validation?&lt;br /&gt;    self.new_record? ? true : !self.password.blank?&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Oct 2007 12:50:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4676</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
    <item>
      <title>Api key (or any kind of key) generator</title>
      <link>http://snippets.dzone.com/posts/show/4657</link>
      <description>Generates a random key for API neatness.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class KeyGenerator&lt;br /&gt;  require "digest/sha1"&lt;br /&gt;  def self.generate(length = 10)&lt;br /&gt;    Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)[1..length]&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 15 Oct 2007 13:03:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4657</guid>
      <author>leethal (August Lilleaas)</author>
    </item>
  </channel>
</rss>
