Posts mit dem Label Rails werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Rails werden angezeigt. Alle Posts anzeigen

2009-07-18

Rake Task to show installed and pending migrations

While developing with Rails I often want to know which migrations are already installed and which are pending. Migrations have names and version numbers. In some rake migration tasks you must specify a migration - but this must be the number :-|

To rest my brain I wrote two rake tasks to show the installed and pending migrations. Feel free to use it for your own - just copy it to a .rake file in your /lib/task directory.


namespace :db do
namespace :migrate do
desc 'Shows installed migrations'
task :installed => :environment do
mt = ActiveRecord::Migrator.new(:up, "db/migrate/")
list = mt.migrations.select{ |mi| mt.migrated.include?(mi.version.to_i) }
print_migration_list(list)
end

desc 'Shows pending migrations'
task :pending => :environment do
mt = ActiveRecord::Migrator.new(:up, "db/migrate/")
print_migration_list(mt.pending_migrations)
end
end
end

def print_migration_list(migrations)
return if migrations.empty?

max_length = migrations.inject(0) {|max, m| [max, m.name.length].max }
puts "Version Name"
puts "---------------" + "-" * max_length
migrations.each do |m|
puts "#{m.version} #{m.name}"
end
end


See also pastie.org

2009-07-14

Generic Cucumber Steps

Zwei generische Steps, die das Ergebnis erster Gehversuche mit Cucumber sind. (Factory = factory_girl)


# Match steps like:
# * And I have projects named A, B, C
Given /^I have .+ named (.+)$/ do |names|
names.split(', ').each do |name|
Factory.create(name)
end
end

# Match steps like:
# * And I should have 1 project
# * And I should have 5 users
Then /^I should have ([0-9]+) (.+)$/ do |count, classes|
Kernel.const_get(classes.singularize.capitalize).count.should == count.to_i
end

2009-04-03

Initd Script für cruisecontrolrb

Based on the $CCRB_HOME/daemon/cruise.sample version.


#!/usr/bin/ruby
# chkconfig: 235 99 10
# description: Start or stop the SnipSnap server
#
### BEGIN INIT INFO
# Provides: cruisecontrolrb
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start or stop the cruisecontrolrb daemon
### END INIT INFO

ENV['PATH']="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"

require "fileutils"
include FileUtils

require "rubygems"

begin
gem 'mongrel'
rescue => e
puts "Error: daemon mode of CC.rb requires mongrel installed"
exit 1
end

def cruise_path
"/home/cruise/cruisecontrolrb"
end

def cruise_user
"cruise"
end

def cruise_log
"/var/log/cruisecontrolrb.log"
end

def start
system "su - #{cruise_user} -c 'cd #{cruise_path} && ./cruise start -d >> #{cruise_log}'"
end

def stop
system "mongrel_rails stop -P #{cruise_path}/tmp/pids/mongrel.pid >> #{cruise_log}" if File.exists?("#{cruise_path}/tmp/pids/mongrel.pid")
Dir["#{cruise_path}/tmp/pids/builders/*.pid"].each do |pid_file|
pid = File.open(pid_file){|f| f.read }
system "kill -9 #{pid}"
rm pid_file
end
end

command = ARGV.shift

system "touch #{cruise_log}"
system "chown #{cruise_user} #{cruise_log}"


case command
when 'start'
start
exit 0
when 'stop'
stop
exit 0
when 'restart'
stop
start
exit 0
else
puts "Usage: /etc/init.d/cruise start|stop|restart"
exit 1
end

2007-12-20

Rails Upgrade

To upgrade, `gem install rails`, set RAILS_GEM_VERSION to ‘1.2.6’ in config/environment.rb, and `rake rails:update:configs`.

Source

2007-09-18

Identifier name ... too long

Bei Problemen mit zu langen (generierten) Indexnamen hilft

:name => ?something_idx?

als weiterer Parameter im add_index Aufruf.

2007-08-03

Umbenennen von Rake migration Scripts

Rake migration Scripts dürfen anscheinend nur kleinbuchstaben im Namen haben.

Z.B. 010_add_isNew.rb geht also nicht. Beim generieren mit dem rake Generator wird dann auch aus generate migration add_isNew das Script add_is_new.rb.

Ausserdem muss der Name der Klasse im Script mit dem Dateinamen korrespondieren: add_is_new.rb bzw. AddIsNew

2007-07-05

rake db:migration

Auswahl der Version über Parameter VERSION=number

rhtml Tags

Bedeutung der rhtml Tags
  • <%= /content/ %>
    /content/ wird als Ruby Code interpretiert und ausgeführt. Das Ergebnis ersetzt die Markierung.
  • <% /content/ %>
    /content/ wird als Ruby Code interpretiert und ausgeführt. Die Markierung wird im Output gelöscht, aber nicht ersetzt.
    Wird oft für die Generierung von Html Code in Schleifen benutzt.
  • <% /content/ -%>
    Wie die Markierung zuvor, allerdings wird im Output zusätzlich das NewLine nach der Markierung entfernt.