create object fom underscore_syntax
// constantize - create instance
// (params[:property]) -- read data from web-form
@property = property_type.camelize.constantize.new(params[:property])
2772 users tagging and storing useful source code snippets
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!)
@property = property_type.camelize.constantize.new(params[:property])
<% page = request.request_uri %> page: <%= page %>
<% page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %> page: <%= page %>
def pluralize(count, noun, text) case count when 0: "There are no #{noun.pluralize} #{text}" when 1: "There is one #{noun} #{text}" else: "There are #{count} #{noun.pluralize} #{text}" end end
File.open("", "wb") do |f| while buff = file_field.read(4096) f.write(buff) end end
def parseForVoolish(voolishContent) rExps = {} rExps[:isVool] = /<<(.+)>>/ rExps[:voolType] = /<<([a-zA-Z]+) (.+)\s*>>/ rExps[:typeless] = /<< (.+)\s*>>/ rExps[:splitQuote] = /\s*---\s*/ rExps[:splitIRC] = /\s*<<\s*/ parsedContent = {} if voolishContent =~ rExps[:isVool] parsedContent[:type] = 'Voolish' if voolishContent =~ rExps[:voolType] parsedContent[:type] = "#$1" tempContent = "#$2" else parsedContent[:type] = 'link' voolishContent =~ rExps[:typeless] tempContent = "#$1" end else parsedContent[:type] = 'thought' tempContent = voolishContent end if parsedContent[:type] == 'link' tempContent = tempContent.split(/\s*<<\s*/) if tempContent.length == 1 parsedContent[:content] = tempContent[0] elsif tempContent.length == 2 parsedContent[:content] = '' + tempContent[0] + '">' + tempContent[1] + '' elsif tempContent.length == 3 # Fix (link_to_remote is a function - parameters? parsedContent[:content] = '' + tempContent[0] + '">' + tempContent[1] + ' ' + tempContent[2] end end if parsedContent[:type] == 'thought' parsedContent[:content] = tempContent end if parsedContent[:type] == 'tip' parsedContent[:content] = tempContent end if parsedContent[:type] == 'quote' tempContent = tempContent.split(rExps[:splitQuote]) parsedContent[:content] = tempContent[0] , content_tag("small", [" — ", tempContent[1]]) end if parsedContent[:type] == 'irc' tempContent = tempContent.split(rExps[:splitQuote]) ircContent = tempContent[0].gsub(rExps[:splitIRC], '
') if tempContent.length == 1 parsedContent[:content] = ircContent elsif tempContent.length == 2 parsedContent[:content] = ircContent , content_tag("small", [" — ", tempContent[1]]) end end return parsedContent end
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
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
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
before_create :generate_smthng def generate_smthng @attributes['smthng'] = "smthng" end
for service in @services s = Object.const_get(service.name.classify) end
for service in @services s = Object.const_get(service.name.classify).find(service.s_id) end
railstrunk app_name
mkdir -p happy_app happy_app/trunk happy_app/tags happy_app/branches svn import happy_app http://myserver/myrepos/happy_app -m "Layout for happy_app" rm -rf happy_app svn co http://myserver/myrepos/happy_app/trunk happy_app railstrunk happy_app
#!/bin/bash rails_dir=~/code/ruby/rails if [ $# != 1 ]; then echo "Usage: $0 app_name" echo echo "Creates a rails application under app_name." echo echo "If app_name is a subversion working copy, rails is set as an external" echo "and generated files are committed." echo echo "If $rails_dir exists, things are sped up by either symlinking it" echo "(for non-versioned apps) or copying it to the vendor dir." echo echo "$rails_dir should be a rails trunk working copy." exit fi dir=$1 mkdir -p $dir cd $dir if [ -n "`ls`" ]; then echo "Can't create app: $dir is not empty." >&2 exit 1 fi if [ -d $rails_dir ]; then if [ "`svn info $rails_dir 2>/dev/null | grep URL:`" != "URL: http://dev.rubyonrails.org/svn/rails/trunk" ]; then echo "$rails_dir is not a rails trunk working copy. Not going to use it." >&2 elif [ -n "`svn st $rails_dir`" ]; then echo "$rails_dir is modified. Not going to use it." >&2 else use_rails_dir=1 fi fi if [ -z "`svn info 2>/dev/null`" ]; then mkdir vendor if [ -n "$use_rails_dir" ]; then ln -s $rails_dir vendor/rails cd vendor/rails svn up cd ../.. else svn co http://dev.rubyonrails.org/svn/rails/trunk vendor/rails fi ruby vendor/rails/railties/bin/rails . rm public/index.html else svn mkdir vendor svn ps svn:externals 'rails http://dev.rubyonrails.org/svn/rails/trunk' vendor svn ci -m 'set rails external to rails trunk' [ -n "$use_rails_dir" ] && cp -r $rails_dir vendor/rails svn up ruby vendor/rails/railties/bin/rails . rm public/index.html rm log/* mv config/database.yml config/database.yml.sample svn add . --force svn ps svn:ignore '*' tmp/cache tmp/pids tmp/sessions tmp/sockets svn ps svn:ignore 'database.yml' config svn ps svn:ignore '*.log' log svn ci -m "- created rails app - moved database.yml to database.yml.sample - deleted public/index.html - ignored logs and tmp" cp config/database.yml.sample config/database.yml fi
arount_filter ScopeAccess::Filter.new(Secret,:mine) def mine {:find=>{:conditions=>["user_id=?",1]}, :create=>{:user_id=>1}} end def index @secrets=Secret.find(:all) end def new secret = Secret.create(:name=>rand(10000)) if (secret.new_record?) flash[:notice] = "Could not create Secret" end redirect_to :action=>"index" end
def do_upload_image image_types = ["image/jpeg", "image/pjpeg", "image/gif", "image/png", "image/x-png"] @recipe = Recipe.find(params[:id]) @recipe.update_attributes(params[:recipe]) logger.info "@recipe.file = " + @recipe.file.to_s if !@recipe.file.blank? if (@recipe.file.size != 0) logger.info "content_type = " + @recipe.file.content_type.to_s logger.info "size = " + @recipe.file.size.to_s if (image_types.include?@recipe.file.content_type.chomp) if @recipe.file.size < 2097152 @recipe.file.rewind # Load the file field directly into the blob, straight from memory and into RMagick! No need to write the raw file out to the filesystem pic = Magick::Image.from_blob(@recipe.file.read)[0] # I want to place the image inside a 200x200 jpg, with a white background. # so all my images are the same size, while maintaning proportions. width = pic.columns height = pic.rows if (width > height) pic.scale!((200.0/width.to_f)) else pic.scale!((200.0/height.to_f)) end back = Magick::Image.new(200,200) { self.background_color = 'white' self.format = 'JPG' } back.composite!(pic, Magick::CenterGravity, Magick::InCompositeOp) # Now scale this down for a thumbnail thumb = back.scale(50,50) # Add a rand to the filename, so if we upload a new image, we're shown it, instead of whatever we have cached. rand = Kernel.rand(1000).to_s File.open(RAILS_ROOT + "/public/images/recipes/" + @recipe.id.to_s + "-" + rand + ".jpg", "wb") do |f| f.write(back.to_blob) end File.open(RAILS_ROOT + "/public/images/recipes/" + @recipe.id.to_s + "-" + rand + "t.jpg", "wb") do |f| f.write(thumb.to_blob) end @recipe.picture = "/images/recipes/" + @recipe.id.to_s + "-" + rand + "." + "jpg" @recipe.thumbnail = "/images/recipes/" + @recipe.id.to_s + "-" + rand + "t." + "jpg" @recipe.file = nil @recipe.save flash[:notice] = 'Image Uploaded and Thumbnailed' redirect_to :action => "edit", :id => @recipe #session[:recipe] = @recipe else flash.now[:error] = "File size greater then 2 Megabytes, please upload a smaller file." render :action => 'upload_image', :id => @recipe end else flash.now[:error] = "File doesn't seem to be JPG, GIF, or PNG, please ensure it is a valid image file." render :action => 'upload_image', :id => @recipe end else flash.now[:error] = "Unable to upload the file you selected, please try again." render :action => 'upload_image', :id => @recipe end else flash.now[:error] = "Unable to upload the file you selected, please try again." render :action => 'upload_image', :id => @recipe end end
def test_creates_entry log_test_name # Rest of your test code goes here # ... end
ENV["RAILS_ENV"] = "test" require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'test_help' module Test::Unit::Assertions def assert_block(message="assert_block failed.") # :yields: _wrap_assertion do if (! yield) logger.debug("\e[0;41mFailure:\e[m #{message}") raise Test::Unit::AssertionFailedError.new(message.to_s) else logger.debug("\e[0;32mSuccess\e[m") end end end end class Test::Unit::TestCase # Transactional fixtures accelerate your tests by wrapping each test method # in a transaction that's rolled back on completion. This ensures that the # test database remains unchanged so your fixtures don't have to be reloaded # between every test method. Fewer database queries means faster tests. # # Read Mike Clark's excellent walkthrough at # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting # # Every Active Record database supports transactions except MyISAM tables # in MySQL. Turn off transactional fixtures in this case; however, if you # don't care one way or the other, switching from MyISAM to InnoDB tables # is recommended. self.use_transactional_fixtures = true # Instantiated fixtures are slow, but give you @david where otherwise you # would need people(:david). If you don't want to migrate your existing # test cases which use the @david style and don't mind the speed hit (each # instantiated fixtures translates to a database query per test method), # then set this back to true. self.use_instantiated_fixtures = false def logger RAILS_DEFAULT_LOGGER end def log_test_name(test_name=nil) test_name = caller[0].match(/`(test_[a-z_]+)'$/) unless test_name logger.debug "\n\e[1m#{self.class}::\e[0;31m#{test_name}\e[m\n" end end class ActionController::Integration::Session def logger RAILS_DEFAULT_LOGGER end def log_step_name(step_name=nil) step_name = caller[0].match(/`(test_[a-z_]+)'$/) unless step_name logger.debug "\n\e[1m#{step_name}\e[m" end end
ln -s /home/user/railsapps/app1/public /home/user/public_html/app1
Workout.within_n_units(:user_id => 12) #-> Workout.find(:all, :conditions => 'created_at > n.units.ago and user_id = 12')
class Workout < ActiveRecord::Base class << self def within_n_temporal_units(number, units, further_conds={}) conds = "created_at > '#{eval("#{number}.#{units}.ago").to_s(:db)}'" further_conds.each {|key, val| conds << " and #{key} = #{val}"} Workout.find(:all, :conditions => conds) end def method_missing(name, *args) return within_n_temporal_units($~[1], $~[2], args[0] || {}) if name.to_s =~ /within_(\d+)_(\w+)/ raise NoMethodError, "undefined method `#{name}' for #{self}:Class" end end end
controller.controller_name
class ActiveRecord::Base def dom_id(prefix=nil) display_id = new_record? ? "new" : id prefix ||= self.class.name.underscore prefix != :bare ? "#{prefix.to_s.dasherize}-#{display_id}" : display_id end endSo, you can do stuff like this in your views: <ul> <% @entries.each do |entry| %>> <%= entry.body %> <% end %> And stuff like this in your controller: def remove_entry entry = JournalEntry.find(params[:id]) update_page do |page| page[entry.dom_id].remove end end
@connection = OCI8.new("db_user", "db_password", "db_name") plsql = @connection.parse("BEGIN P_MYPROCEDURE.my_method(:out); END;") plsql.bind_param(':out', OCI8::Cursor) plsql.exec cursor = plsql[':out'] plsql.close x = '' while r = cursor.fetch() x = x + r.join(', ') + '
' end @out = x @connection.logout