forked from ryanb/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
railsrc
175 lines (143 loc) · 4.56 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/ruby
# -*- ruby -*-
# puts "Using #{__FILE__}"
begin
require 'hirb'
require 'debug'
puts "railsrc: enabling hirb"
Hirb.enable pager: !(ENV['PAGER'] && "cat" == ENV['PAGER'].split(File::SEPARATOR).last)
def hirbv
Hirb.disable
Hirb.enable :output => {"ActiveRecord::Base" => {:class => :auto_table, :options => {:vertical => true}}}
end
def hirbh
Hirb.disable
Hirb.enable :output => {"ActiveRecord::Base" => {:class => :auto_table, :options => {:vertical => false}}}
end
rescue LoadError => e
# NOTE: puts interferes with 'robe-mode'
# puts "Unable to load and/or enable Hirb"
# puts e.inspect
end
begin
require ENV['HOME'] + '/.localrailsrc.rb'
rescue LoadError
end
def change_log(stream)
Rails.logger = Logger.new(stream)
ActiveRecord::Base.logger = Logger.new(stream)
ActiveRecord::Base.clear_active_connections!
ActionController::Base.logger = Logger.new STDOUT
end
def show_log
change_log(STDOUT)
nil
end
def hide_log
change_log(nil)
nil
end
# def how_to_load_a_rake_task
# require 'rake'
# Rails.application.load_tasks
# Rake::Task['routes'].invoke
# Rake::Task['routes'].reenable
# end
def show_routes(string = ".*")
if Rails.version =~ /^[4,5]/
show_routes_fur_fife string
elsif Rails.version =~ /^3.2/
show_routes_thee_two string
elsif Rails.version =~ /^3/
show_routes_legacy string
else
puts "Not implemented for Rails #{Rails.version}"
end
end
def show_routes_fur_fife match = ".*"
match = match.to_s
routes = raw_routes.select {|r| "#{r.name.to_s}\t#{r.verb}\t#{r.path.spec.to_s}\t#{r.requirements[:controller]}##{r.requirements[:action]}" =~ /#{match}/}
require 'action_dispatch/routing/inspector'
inspector = ActionDispatch::Routing::RoutesInspector.new(routes)
routes_formatted = inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
.split(/\n/)
.reject {|s| s =~ /^\s*$/ }
puts highlight(routes_formatted, match).join("\n")
end
def show_routes_thee_two match = ".*"
match = match.to_s
routes = raw_routes.select {|r| "#{r.name.to_s}\t#{r.verb}\t#{r.path.spec.to_s}" =~ /#{match}/}
require 'rails/application/route_inspector'
inspector = Rails::Application::RouteInspector.new
routes_formatted = inspector.format(routes, ENV['CONTROLLER'])
puts highlight(routes_formatted, match).join("\n")
end
def raw_routes
Rails.application.reload_routes!
Rails.application.routes.routes
end
def highlight strings = [], match = ".*"
ansi_bold = "\033[1m"
ansi_yellow = "\033[33m"
ansi_reset = "\033[0m"
unless match == ".*"
strings = strings.map do |r|
r.gsub!(match, "#{ansi_yellow}#{ansi_bold}#{match}#{ansi_reset}")
end
end
strings
end
def show_routes_legacy(string = nil)
string = string.to_s
# lifted from gems/railties-3.0.3/lib/rails/tasks/routes.rake
Rails.application.reload_routes!
all_routes = Rails.application.routes.routes
if ENV['CONTROLLER']
all_routes = all_routes.select{ |route| route.defaults[:controller] == ENV['CONTROLLER'] }
end
routes = all_routes.collect do |route|
reqs = route.requirements.dup
reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
reqs = if reqs.empty?
""
elsif reqs[:controller]
reqs[:controller] + '#' + reqs[:action]
else
reqs.inspect
end
{:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
end
routes.reject! { |r| r[:path] =~ %r{/rails/info/properties} } # Skip the route if it's internal info route
name_width = routes.map{ |r| r[:name].length }.max
verb_width = routes.map{ |r| r[:verb].length }.max
path_width = routes.map{ |r| r[:path].length }.max
ansi_bold = "\033[1m"
ansi_reset = "\033[0m"
ansi_lgray = "\033[0;37m"
ansi_gray = "\033[1;30m"
routes.each do |r|
if string.nil? or r[:path].index(string) or r[:name].index(string) or r[:reqs].index(string)
line = [
r[:name].rjust(name_width),
r[:verb].ljust(verb_width),
r[:path].ljust(path_width),
r[:reqs]
].join(' ')
unless string.nil?
line.gsub!(string, "#{ansi_bold}#{string}#{ansi_reset}")
end
puts line
end
end
nil
end
module ActiveRecord
class Relation
def to_sql_formatted
to_sql.gsub(/([A-Z][A-Z\s]+)/) { |m| "\n#{m}" }
end
def most(field, having=0)
group(field).select("count(#{field}) AS total").having("total >= #{having}").order("total DESC")
end
end
end