http://blog.obiefernandez.com/content/2008/06/railsconf-2008.html
1 def javascript_include_all
2 includes = ''
3 Dir.new("#{RAILS_ROOT}/public/javascripts").each do |js|
4 next if js == "." or js == '..' or !js[".js"]
5 includes += javascript_include_tag(js) + "\n"
6 end
7 end
def javascript_include_all
includes = ''
Dir.new("#{RAILS_ROOT}/public/javascripts").each do |js|
next if js == "." or js == '..' or !js[".js"]
includes += javascript_include_tag(js) + "\n"
end
end
this helper returns a current url
1 def current_url
2 url_for :only_path => false, :overwrite_params=>{}
3 end
def current_url
url_for :only_path => false, :overwrite_params=>{}
end
+ info:
http://adam.blog.heroku.com/past/2008/11/2/pony_the_express_way_to_send_email_from_ruby/
http://github.com/adamwiggins/pony/
1 require 'rubygems'
2 require 'pony'
3 Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'Hello')
require 'rubygems'
require 'pony'
Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'Hello')
1 def get_file_as_string(filename)
2 data = ''
3 f = File.open(filename, "r")
4 f.each_line do |line|
5 data += line
6 end
7 return data
8 end
9
10 test = get_file_as_string 'myfile.txt'
11 puts test
def get_file_as_string(filename)
data = ''
f = File.open(filename, "r")
f.each_line do |line|
data += line
end
return data
end
test = get_file_as_string 'myfile.txt'
puts test
it’s only a remember.
1 render_component(:action => 'upload', :layout => 'popup', :params => { :l => params[:language], :d => params[:media][:definition_id], :t => 1 })
render_component(:action => 'upload', :layout => 'popup', :params => { :l => params[:language], :d => params[:media][:definition_id], :t => 1 })
this task show all TODO comments in your project.
save in lib/tasks/todo.rake
1 require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
2
3 namespace :todo do
4 desc 'List TODOs in all .rb files under app/'
5 task(:list) do
6 FileList["app/**/*.rb"].egrep(/TODO/)
7 end
8 end
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
namespace :todo do
desc 'List TODOs in all .rb files under app/'
task(:list) do
FileList["app/**/*.rb"].egrep(/TODO/)
end
end
another solutions:
http://blog.craigambrose.com/articles/2007/03/01/a-rake-task-for-database-backups
http://tiago.zusee.com/blog/2007/06/12/rake-task-para-backup-de-banco-de-dados-em-rails/
http://derenci.us/2007/7/backup-do-banco-com-rake
1 require 'ftools'
2 require 'find'
3 namespace :db do
4 desc "Backup the database to a file. Options: DIR=base_dir RAILS_ENV=production COMPACT=true MAX=2"
5 task :backup => [:environment] do
6 datestamp = Time.now.strftime("%Y-%m-%d")
7 hourstamp = Time.now.strftime("%H-%M-%S")
8 base_path = ENV["DIR"] || "db"
9 backup_base = File.join(base_path, 'backup')
10 backup_file = File.join(backup_base, "#{RAILS_ENV}_#{datestamp}_#{hourstamp}_dump.sql")
11 File.makedirs(backup_base)
12 db_config = ActiveRecord::Base.configurations[RAILS_ENV]
13 host = " -h #{db_config['host']} " if db_config['host'] and !db_config['socket']
14
15 sh "mysqldump #{host} -u #{db_config['username']} -p#{db_config['password']} #{db_config['database']} > #{backup_file}"
16 puts "Created backup: #{backup_file}"
17
18 sh "bzip2 -z #{backup_file}" if ENV["COMPACT"] == "true"
19 puts "Compacted backup: #{backup_file}.bz2" if ENV["COMPACT"] == "true"
20
21 dir = Dir.new(backup_base)
22 all_backups = dir.entries[2..-1].sort
23 max_backups = ENV["MAX"].to_i || 5
24 unwanted_backups = all_backups[max_backups..-2] || []
25 for unwanted_backup in unwanted_backups
26 FileUtils.rm_rf(File.join(backup_base, unwanted_backup))
27 puts "- Deleted old backup: #{unwanted_backup}"
28 end
29 puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
30 end
31 end
require 'ftools'
require 'find'
namespace :db do
desc "Backup the database to a file. Options: DIR=base_dir RAILS_ENV=production COMPACT=true MAX=2"
task :backup => [:environment] do
datestamp = Time.now.strftime("%Y-%m-%d")
hourstamp = Time.now.strftime("%H-%M-%S")
base_path = ENV["DIR"] || "db"
backup_base = File.join(base_path, 'backup')
backup_file = File.join(backup_base, "#{RAILS_ENV}_#{datestamp}_#{hourstamp}_dump.sql")
File.makedirs(backup_base)
db_config = ActiveRecord::Base.configurations[RAILS_ENV]
host = " -h #{db_config['host']} " if db_config['host'] and !db_config['socket']
sh "mysqldump #{host} -u #{db_config['username']} -p#{db_config['password']} #{db_config['database']} > #{backup_file}"
puts "Created backup: #{backup_file}"
sh "bzip2 -z #{backup_file}" if ENV["COMPACT"] == "true"
puts "Compacted backup: #{backup_file}.bz2" if ENV["COMPACT"] == "true"
dir = Dir.new(backup_base)
all_backups = dir.entries[2..-1].sort
max_backups = ENV["MAX"].to_i || 5
unwanted_backups = all_backups[max_backups..-2] || []
for unwanted_backup in unwanted_backups
FileUtils.rm_rf(File.join(backup_base, unwanted_backup))
puts "- Deleted old backup: #{unwanted_backup}"
end
puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
end
end
returns “Displaying records 1 – 10 of 35”. used in codestacker
1
2
3 def self.per_page
4 10
5 end
6
7
8
9 def paginate_range(in_collection, in_tot_count)
10 endnumber = in_collection.offset + in_collection.per_page > in_tot_count ?
11 in_tot_count : in_collection.offset + in_collection.per_page
12 "Displaying records #{in_collection.offset + 1} - #{endnumber} of #{in_tot_count}"
13 end
14
15
16
17 @codes = Code.paginate :conditions => conditions, :page => params[:page]
18 @codes_count = Code.count
19
20
21
22 <%= paginate_range @codes, @codes_count %>
def self.per_page
10
end
def paginate_range(in_collection, in_tot_count)
endnumber = in_collection.offset + in_collection.per_page > in_tot_count ?
in_tot_count : in_collection.offset + in_collection.per_page
"Displaying records #{in_collection.offset + 1} - #{endnumber} of #{in_tot_count}"
end
@codes = Code.paginate :conditions => conditions, :page => params[:page]
@codes_count = Code.count
<%= paginate_range @codes, @codes_count %>
source: http://blog.carlosgabaldon.com/calabro/blog/post/2008/04/08/Ruby_Arrays
1
2
3 array = Array.new
4
5 array = []
6
7
8
9 array = []
10 array[0] = "first element"
11
12
13
14 array << "last element"
15
16
17 array.push("last element")
18
19
20
21
22 array = ["first element", "second element"]
23 array.unshift("before first element")
24
25
26
27
28
29
30 array = ["first element", "second element", "third element"]
31 third_element = array[2]
32
33
34
35
36
37
38
39
40
41 array = ["first element", "second element"]
42 first_element = array.shift
43
44
45
46 last_element = array.pop
47
48
49
50
51
52 array = [1, 2, 3]
53 array.concat([4, 5, 6, 7])
54
55 new_array = array + [4, 5, 6]
56
57
58
59
60 array.replace([4, 5, 6])
61
62
63 array = [1, 2, 3]
64 new_paired_array = array.zip([4, 5, 6])
65
66
67
68
69 array = [1, 2, 3]
70 new_paired_array_flattened = array.zip([4, 5, 6]).flatten
71
72
73
74
75
76 array = [1, 2, 3]
77 array.collect {|x| x * 2 }
78
79
80
81
82
83 [1, 2, 3, 4] .each {|x| puts x}
84
85
86
87
88
89
90 [1, 2, 3, 4, 5, 6] .find {|x| puts x > 5}
91
92
93
94
95 array.find_all {|item| criteria }
96
97
98 array.size
99
100
101 array.empty?
102
103
104 array.include?(item)
105
106
107 array.any? {|item| criteria }
108
109
110 array.all? {|item| criteria }
111
array = Array.new
array = []
array = []
array[0] = "first element"
array << "last element"
array.push("last element")
array = ["first element", "second element"]
array.unshift("before first element")
array = ["first element", "second element", "third element"]
third_element = array[2]
array = ["first element", "second element"]
first_element = array.shift
last_element = array.pop
array = [1, 2, 3]
array.concat([4, 5, 6, 7])
new_array = array + [4, 5, 6]
array.replace([4, 5, 6])
array = [1, 2, 3]
new_paired_array = array.zip([4, 5, 6])
array = [1, 2, 3]
new_paired_array_flattened = array.zip([4, 5, 6]).flatten
array = [1, 2, 3]
array.collect {|x| x * 2 }
[1, 2, 3, 4] .each {|x| puts x}
[1, 2, 3, 4, 5, 6] .find {|x| puts x > 5}
array.find_all {|item| criteria }
array.size
array.empty?
array.include?(item)
array.any? {|item| criteria }
array.all? {|item| criteria }
when a form is invalid, rails shows a default error message. for hide the messages, try these options:
1 <%= error_messages_for :order, :header_message => nil, :message => nil %>
<%= error_messages_for :order, :header_message => nil, :message => nil %>