Never been to TextSnippets 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 (or not, you can keep them private!)

About this user

Ned Baldessin http://www.idsland.com

« Newer Snippets
Older Snippets »
10 total  XML / RSS feed 

SVN ignore for all assets that don't begin by "_"

svn propset svn:ignore "[!_]*" .

Extend FileColumn to work with ActsAsPartitionnedID


# environment.rb
module FileColumn
  class PermanentUploadedFile    
    alias_method :orig_move_from, :move_from
    def move_from(local_dir, just_uploaded)
      FileUtils.rm_rf @dir
      
      part_it_options = @instance.class.acts_as_partitioned_id_options
      partition_length = part_it_options[:digits] / part_it_options[:partitions]
      #last_partition = @dir[-partition_length.to_i..-1]
      the_rest = @dir[0...-partition_length.to_i]
      FileUtils.mkpath the_rest
      
      FileUtils.mv local_dir, @dir
      @just_uploaded = just_uploaded
    end
    
    private
    alias_method :orig_relative_path_prefix, :relative_path_prefix
    def relative_path_prefix
      raise RuntimeError.new("Trying to access file_column, but primary key got lost.") if @instance.partitioned_id.to_s.empty?
      @instance.partitioned_id.to_s
    end
  end
end

Extend FileColumn to work with ActsAsPartitionnedID


# environment.rb
module FileColumn
  class PermanentUploadedFile    
    alias_method :orig_move_from, :move_from
    def move_from(local_dir, just_uploaded)
      FileUtils.rm_rf @dir
      
      part_it_options = @instance.class.acts_as_partitioned_id_options
      partition_length = part_it_options[:digits] / part_it_options[:partitions]
      #last_partition = @dir[-partition_length.to_i..-1]
      the_rest = @dir[0...-partition_length.to_i]
      FileUtils.mkpath the_rest
      
      FileUtils.mv local_dir, @dir
      @just_uploaded = just_uploaded
    end
    
    private
    alias_method :orig_relative_path_prefix, :relative_path_prefix
    def relative_path_prefix
      raise RuntimeError.new("Trying to access file_column, but primary key got lost.") if @instance.partitioned_id.to_s.empty?
      @instance.partitioned_id.to_s
    end
  end
end

Extend FileColumn to work with ActsAsPartitionnedID


# environment.rb
module FileColumn
  class PermanentUploadedFile    
    alias_method :orig_move_from, :move_from
    def move_from(local_dir, just_uploaded)
      FileUtils.rm_rf @dir
      
      part_it_options = @instance.class.acts_as_partitioned_id_options
      partition_length = part_it_options[:digits] / part_it_options[:partitions]
      #last_partition = @dir[-partition_length.to_i..-1]
      the_rest = @dir[0...-partition_length.to_i]
      FileUtils.mkpath the_rest
      
      FileUtils.mv local_dir, @dir
      @just_uploaded = just_uploaded
    end
    
    private
    alias_method :orig_relative_path_prefix, :relative_path_prefix
    def relative_path_prefix
      raise RuntimeError.new("Trying to access file_column, but primary key got lost.") if @instance.partitioned_id.to_s.empty?
      @instance.partitioned_id.to_s
    end
  end
end

Extend FileColumn to work with ActsAsPartitionnedID


# environment.rb
module FileColumn
  class PermanentUploadedFile    
    alias_method :orig_move_from, :move_from
    def move_from(local_dir, just_uploaded)
      FileUtils.rm_rf @dir
      
      part_it_options = @instance.class.acts_as_partitioned_id_options
      partition_length = part_it_options[:digits] / part_it_options[:partitions]
      #last_partition = @dir[-partition_length.to_i..-1]
      the_rest = @dir[0...-partition_length.to_i]
      FileUtils.mkpath the_rest
      
      FileUtils.mv local_dir, @dir
      @just_uploaded = just_uploaded
    end
    
    private
    alias_method :orig_relative_path_prefix, :relative_path_prefix
    def relative_path_prefix
      raise RuntimeError.new("Trying to access file_column, but primary key got lost.") if @instance.partitioned_id.to_s.empty?
      @instance.partitioned_id.to_s
    end
  end
end

Get the name of the controller used, when inside a view.

controller.controller_name

Modify acts_as_urlnameable to un-accentuate unicode chars

The acts as urlnameable plugin (used to create permalinks) removes accentuated chars by default.

Change the urlnameify method in lib/acts_as_urlnameable.rb:74

def urlnameify(text)
  t = Iconv.new('ASCII//TRANSLIT', 'utf-8').iconv(text)
  t = t.downcase.strip.gsub(/[^-_\s[:alnum:]]/, '').squeeze(' ').tr(' ', '-')
  (t.blank?) ? '-' : t
end

Remote FTP transfer with ncftpput

When you can't use SCP…

ncftpput -u username -p password ftpperso.free.fr -R -m /local/path /remote/path

Setup Globalize plugin in Rails

Don't forget to edit config/database.yml first.

Terminal:
script/plugin install \
    http://svn.globalize-rails.org/svn/globalize/globalize/trunk 
rake globalize:setup 


…in config/environment.rb:
include Globalize
Locale.set_base_language 'en-US'


…in the app controller, before any other filter:
  before_filter :set_locale
  # FIXME, NOT TESTED YET, PROBABLY DOESN'T WORK.
  def set_locale
    if params[:locale]
      # The user has explicitly requested a locale.
      session[:locale] = params[:locale]
      Locale.set params[:locale]
    elsif session[:locale].empty?
      # The user has a current session.
      Locale.set session[:locale]
    elsif ! request.env["HTTP_ACCEPT_LANGUAGE"].empty?
      # We use the browser's locale settings.
      Locale.set request.env["HTTP_ACCEPT_LANGUAGE"][/[a-z]{2}-[a-z]{2}/i]
    else
      # the default locale defined in config/environment.rb is used.
    end
  end


…in the model:
class Blog < ActiveRecord::Base
  translates :title, :post
end

Check out all TextMate Bundles with SVN

svn --username anon --password anon co http://macromates.com/svn/Bundles/trunk/Bundles /Users/ned/Library/Application\ Support/TextMate/Bundles
« Newer Snippets
Older Snippets »
10 total  XML / RSS feed