-
Notifications
You must be signed in to change notification settings - Fork 7
/
.railsrc
87 lines (77 loc) · 2.25 KB
/
.railsrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
if (rails_env = ENV['RAILS_ENV']) || (rails_env = Rails.env)
rails_root = File.basename(Dir.pwd)
rails_env_string = case rails_env
when 'development'
'[DEV]'
when 'production'
'[PROD]'
else
"[#{rails_env.upcase}]"
end
# script/console prompt
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
:PROMPT_I => "#{rails_root} #{rails_env_string}>> ",
:PROMPT_S => "#{rails_root} #{rails_env_string}* ",
:PROMPT_C => "#{rails_root} #{rails_env_string}? ",
:RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :RAILS
# Called after the irb session is initialized and Rails has been loaded
IRB.conf[:IRB_RC] = Proc.new do
next unless defined?(ActiveRecord::Base)
# [] acts as find()
ActiveRecord::Base.instance_eval { alias :[] :find }
def r!
reload!
end
# sql for arbitrary SQL commands through the AR
def sql(query)
ActiveRecord::Base.connection.execute(query)
end
end
end
# set logging to screen
if ENV.include?('RAILS_ENV')
# Rails 2.x
if !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
end
else
# Rails 3
if Rails.logger
Rails.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = Rails.logger
end
end
# .details method for pretty printing ActiveRecord's objects attributes
class Object
def details
if self.respond_to?(:attributes) and self.attributes.any?
max = self.attributes.keys.sort_by { |k| k.size }.pop.size + 5
puts
self.attributes.keys.sort.each do |k|
puts sprintf("%-#{max}.#{max}s%s", k, self.try(k))
end
puts
end
end
alias :detailed :details
end
# readable BigDecimal#inspect output (https://gist.github.com/henrik/6280438)
# for Ruby 1.9+ and Rails 3+
if RUBY_VERSION >= '1.9.0' && defined?(Rails)
class BigDecimal
def inspect
format("#<BigDecimal:%x %s>", object_id, to_s('F'))
end
end
end
# returns a collection of the methods that Rails added to the given class
# http://lucapette.com/irb/rails-core-ext-and-irb/
class Class
def core_ext
self.instance_methods.map {|m| [m, self.instance_method(m).source_location] }.select {|m| m[1] && m[1][0] =~/activesupport/}.map {|m| m[0]}.sort
end
end