Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 256 lines (224 sloc) 7.125 kB
109c3cf @oriolgual Power up railsrc
oriolgual authored
1 # .railsrc for Rails 3, encoding: utf-8
2 # see http://rbjl.net/49-railsrc-rails-console-snippets
37c2942 @txus dotfilezz
txus authored
3
109c3cf @oriolgual Power up railsrc
oriolgual authored
4 if !Rails.application then warn "Rails isn't loaded, yet... skipping .railsrc" else
5 # # #
6
7 def ripl?; defined?(Ripl) && Ripl.instance_variable_get(:@shell); end
8
9 # # #
10 # loggers
11 ActiveRecord::Base.logger = Logger.new STDOUT
12 ActiveRecord::Base.clear_reloadable_connections!
13
14 ActionController::Base.logger = Logger.new STDOUT
15
16 # # #
17 # named routes and helpers
18 include Rails.application.routes.url_helpers
19 default_url_options[:host] = Rails.application.class.parent_name.downcase
20
21 #include ActionView::Helpers # All Rails helpers
22 include ApplicationController._helpers # Your own helpers
23 #
24 # unfortunately that breaks some functionality (e.g. the named route helpers above)
25 # so, look at actionpack/lib/action_view/helpers.rb and choose the helpers you need
26 # (and which don't break anything), e.g.
27 include ActionView::Helpers::DebugHelper
28 include ActionView::Helpers::NumberHelper
29 include ActionView::Helpers::SanitizeHelper
30 include ActionView::Helpers::TagHelper
31 include ActionView::Helpers::TextHelper
32 include ActionView::Helpers::TranslationHelper
33
e089bcf @mrcasals Add railsrc file to config, need some refactor
mrcasals authored
34 if defined?(Hirb)
35 # # #
36 # route list view helpers (requires hirb)
37
38 # hirb view for a route
39 class Hirb::Helpers::Route < Hirb::Helpers::AutoTable
40 def self.render(output, options = {})
41 super( output.requirements.map{ |k,v|
42 [k, v.inspect]
43 }, options.merge({
44 :headers => [output.name || '', "#{ output.verb || 'ANY' } #{ output.path }"],
45 :unicode => true,
46 :description => nil,
47 }) )
48 end
109c3cf @oriolgual Power up railsrc
oriolgual authored
49 end
e089bcf @mrcasals Add railsrc file to config, need some refactor
mrcasals authored
50 Hirb.add_view ActionDispatch::Routing::Route, :class => Hirb::Helpers::Route
51
52 # short and long route list
53 def routes(long_output = false)
54 if long_output
55 Rails.application.routes.routes.each{ |e|
56 puts Hirb::Helpers::Route.render(e)
57 }
58 true
59 else
60 Hirb::Console.render_output Rails.application.routes.routes.map{|e|
61 [e.name || '', e.verb || 'ANY', e.path]
62 },{
63 :class => Hirb::Helpers::AutoTable,
64 :headers => %w<name verb path>,
65 }
66 end
109c3cf @oriolgual Power up railsrc
oriolgual authored
67 end
e089bcf @mrcasals Add railsrc file to config, need some refactor
mrcasals authored
68
69 # # #
70 # misc db helpers (requires hirb)
71 module DatabaseHelpers
72 extend self
73
74 def tables
75 Hirb::Console.render_output ActiveRecord::Base.connection.tables.map{|e|[e]},{
76 :class => Hirb::Helpers::AutoTable,
77 :headers => %w<tables>,
78 }
79 true
80 end
81
82 def table(which)
83 Hirb::Console.render_output ActiveRecord::Base.connection.columns(which).map{ |e|
84 [e.name, e.type, e.sql_type, e.limit, e.default, e.scale, e.precision, e.primary, e.null]
85 },{
86 :class => Hirb::Helpers::AutoTable,
87 :headers => %w<name type sql_type limit default scale precision primary null>,
88 }
89 true
90 end
91
92 def counts
93 Hirb::Console.render_output ActiveRecord::Base.connection.tables.map{|e|
94 [e, ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM #{e}")]
95 },{
96 :class => Hirb::Helpers::AutoTable,
97 :headers => %w<table count>,
98 }
99 true
100 end
101
102 # ...
103 end
104 def db; DatabaseHelpers; end
105
106 # # #
107 # temp patches
108
109 # hirb vs irb vs ripl
110 # =begin
111 # class << Hirb::View
112 # def enable_output_method
113 # if defined?(Ripl) && Ripl.instance_variable_get(:@shell)
114 # @output_method = true
115 # require 'ripl/hirb'
116 # elsif defined? IRB
117 # @output_method = true
118 # ::IRB::Irb.class_eval do
119 # alias_method :non_hirb_view_output, :output_value
120 # def output_value #:nodoc:
121 # Hirb::View.view_or_page_output(@context.last_value) || non_hirb_view_output
122 # end
123 # end
124 # end
125 # end
126 # end
127 # #Hirb::View.disable
128 # #Hirb::View.enable
129 # =end
130
109c3cf @oriolgual Power up railsrc
oriolgual authored
131 end
132
133 # get a specific route via index or name
134 def route(index_or_name)
135 route = case index_or_name
136 when Integer
137 Rails.application.routes.routes[ index_or_name ]
138 when Symbol # named route
139 Rails.application.routes.named_routes.get index_or_name
140 end
141 end
142
143 # access to routeset for easy recognize / generate
144 def r
e089bcf @mrcasals Add railsrc file to config, need some refactor
mrcasals authored
145 Rails.application.reload_routes!
146 all_routes = Rails.application.routes.routes
147
148 require 'rails/application/route_inspector'
149 inspector = Rails::Application::RouteInspector.new
150 puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n"
37c2942 @txus dotfilezz
txus authored
151 end
152
109c3cf @oriolgual Power up railsrc
oriolgual authored
153 # # #
154 # rails prompt
155 if ripl?
156 module Ripl::RailsPrompt
157 def prompt
158 @prompt = "#{ Rails.application.class.parent_name.downcase }(#{ Rails.env[0...3] })> "
159 super
160 end
161 end
162 Ripl::Shell.include Ripl::RailsPrompt
163 else
164 app_name = Rails.application.class.parent_name.downcase
165 app_env = Rails.env[0...3]
166 IRB.conf[:PROMPT] ||= {}
167 IRB.conf[:PROMPT][:RAILS] = {
168 :PROMPT_I => "#{ app_name }(#{ app_env })> ",
169 :PROMPT_N => "#{ app_name }(#{ app_env })| ",
170 :PROMPT_C => "#{ app_name }(#{ app_env })| ",
171 :PROMPT_S => "#{ app_name }(#{ app_env })%l ",
172 :RETURN => "=> %s\n",
173 :AUTO_INDENT => true,
174 }
175 IRB.conf[:PROMPT_MODE] = :RAILS
37c2942 @txus dotfilezz
txus authored
176 end
177
109c3cf @oriolgual Power up railsrc
oriolgual authored
178 # # #
179 # per project histories
180 history_file = File.join Dir.pwd, '.console_history'
181 if ripl?
182 Ripl.config[:history] = history_file
183 else
184 if !IRB.conf[:PROMPT][:RVM]
185 IRB.conf[:HISTORY_FILE] = history_file
186 else # RVM workaround, code from ~/.rvm/scripts/irbrc.rb
187 # NOTE: messes up your ~/.irb-history
188 # consider editing the rvm script directly
189 if File.exists?(history_file)
190 lines = IO.readlines(history_file).collect { |line| line.chomp }
191 Readline::HISTORY.clear
192 Readline::HISTORY.push(*lines)
193 end
194
195 Kernel::at_exit do
196 maxhistsize = IRB.conf[:SAVE_HISTORY] || 100
197 history_file = File.join Dir.pwd, ".console_history"
198 lines = Readline::HISTORY.to_a.reverse.uniq.reverse
199 lines = lines[-maxhistsize, maxhistsize] if lines.compact.length > maxhistsize
200 File::open(history_file, "w+") { |io| io.puts lines.join("\n") }
201 end
202 end
37c2942 @txus dotfilezz
txus authored
203 end
204
109c3cf @oriolgual Power up railsrc
oriolgual authored
205 # # #
206 # plain sql
207 def sql(query)
208 ActiveRecord::Base.connection.select_all(query)
37c2942 @txus dotfilezz
txus authored
209 end
109c3cf @oriolgual Power up railsrc
oriolgual authored
210
211 # # #
212 # instead of User.find(...) you can do user(...)
213 # without arguments it only returns the model class
214 # based on http://www.clarkware.com/blog/2007/09/03/console-shortcut
215 Dir.glob( File.join(Dir.pwd, *%w<app models ** *.rb>) ).map { |file_name|
216 table_name = File.basename(file_name).split('.')[0..-2].join
217 Object.instance_eval do
218 define_method(table_name) do |*args|
219 table_class = table_name.camelize.constantize
220 if args.empty?
221 table_class
222 else
223 table_class.send(:find, *args)
224 end
225 end
226 end
227 }
228
229 # # #
230 # edit records with vim, emacs...
231 # class Url
232 # can_console_update
233 # end
234 # Url.first.console_update
235 # see https://github.com/cldwalker/console_update
e089bcf @mrcasals Add railsrc file to config, need some refactor
mrcasals authored
236 # require 'console_update'
109c3cf @oriolgual Power up railsrc
oriolgual authored
237 # ConsoleUpdate.editor = 'vim' # not necessary if env var $EDITOR is set
238
239 # # #
240 # gems & plugin initializers
241
242 # initiate authlogic session
243 if defined? Authlogic
244 Authlogic::Session::Base.controller =
245 Authlogic::ControllerAdapters::RailsAdapter.new(self)
246 end
247
248 # ...
249
250
251 # # #
252 end # if Rails.application
253 # # #
254 #
255 # J-_-L
Something went wrong with that request. Please try again.