Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

August Lilleaas http://august.lilleaas.net

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Array to HTL list helper

Makes a HTML list out of any array. The objects in the array is yielded to the block. Usage:


<%= list(@posts) { |post| link_to post.title, posts_url(post) } %>


And the actual helper (put it in application_helper.rb):

def list(stuff, &block)
  content_tag(:ul, stuff.map { |thingie| content_tag(:li, yield(thingie)) })
end

Freeze rails to older version

This is an explanation of how to make a new, fresh Rails app of any released version of Rails.

First, generate a new rails app:

rails my_project
cd my_project


Find the release you want to use from this list. There's a bunch of stuff in there - the Rails releases are prefixed with rel_.

Then, ender this command to freeze Rails to the version you want. Remember to change the TAG= part (doh).

rake rails:freeze:edge TAG=rel_1-0-0


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.

ruby vendor/rails/railties/bin/rails .


Woot!

RSpec extension: testing attr_protected and attr_accessible

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:

class User < ActiveRecord::Base
  # either 
  attr_accessible :username, :full_name
  # or
  attr_protected :admin, :foo
end

# ...

it "should protect admin and foo" do
  @user.should protect_attributes(:admin, :foo)
end


Here goes:

module CustomExpectations
  class ProtectAttributes
    def initialize(*attributes)
      @attributes = attributes
    end
    
    def matches?(target)
      @target = target
      
      calculate_protected_methods
      perform_check
    end
    
    def failure_message
      "expected #{@failed_attribute} to be protected"
    end
    
    def negative_failure_message
      "expected #{@failed_attribute} to not be protected"
    end
    
    private
    
    def calculate_protected_methods
      read = proc {|var| @target.instance_eval { self.class.read_inheritable_attribute(var) } }
      accessible = read.call("attr_accessible")
      protekted = read.call("attr_protected")
      all = @target.class.column_names.map(&:to_sym)
      
      @protected = []
      @protected << protekted if protekted
      @protected << (all - accessible) if accessible
      @protected.flatten!
      
      @accessible = all - @protected
    end
    
    def perform_check
      failed_attributes = (@attributes & @accessible)
      @failed_attribute = failed_attributes.first
      failed_attributes.empty?
    end
  end
  
  def protect_attributes(attributes)
    ProtectAttributes.new(attributes)
  end
  
  def protect_attribute(attribute)
    ProtectAttributes.new(*[attribute])
  end
end

Downgrading a new rails app

Use this code to downgrade to version X of rails. Use the tags found in the repository.


rails my_project
cd my_project

rake rails:freeze:edge TAG=rel_1-1-0
ruby vendor/rails/railties/bin/rails .

Simple user model with password crypting

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.

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.

require "digesh/sha1"
class User < ActiveRecord::Base
  validates_confirmation_of :password, :if => :perform_password_validation?
  validates_presence_of :password, :if => :perform_password_validation?

  before_save :hash_password
  attr_accessor :password

  # Returns true if the password passed matches the password in the DB
  def valid_password?(password)
    self.password_hash == self.class.hash_password(password)
  end

  private

  # Performs the actual password encryption. You want to change this salt to something else.
  def self.hash_password(password, salt = "meeQue8Zucijoo7")
    Dihest::SHA1.hexdigest(password, salt)
  end

  # Sets the hashed version of self.password to password_hash, unless it's blank.
  def hash_password
    self.password_hash = self.class.hash_password(self.password) unless self.password.blank?
  end
 
  # Assert wether or not the password validations should be performed. Always on new records, only on existing
  # records if the .password attribute isn't blank.
  def perform_password_validation?
    self.new_record? ? true : !self.password.blank?
  end
end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS