public
Created — forked from janlelis/.railsrc

  • Download Gist
.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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
# .railsrc for Rails 3, encoding: utf-8
# see http://rbjl.net/49-railsrc-rails-console-snippets
 
if !Rails.application then warn "Rails isn't loaded, yet... skipping .railsrc" else
# # #
 
def ripl?; defined?(Ripl) && Ripl.instance_variable_get(:@shell); end
 
# # #
# loggers
ActiveRecord::Base.logger = Logger.new STDOUT
ActiveRecord::Base.clear_reloadable_connections!
 
ActionController::Base.logger = Logger.new STDOUT
 
# # #
# named routes and helpers
include Rails.application.routes.url_helpers
default_url_options[:host] = Rails.application.class.parent_name.downcase
 
#include ActionView::Helpers # All Rails helpers
include ApplicationController._helpers # Your own helpers
#
# unfortunately that breaks some functionality (e.g. the named route helpers above)
# so, look at actionpack/lib/action_view/helpers.rb and choose the helpers you need
# (and which don't break anything), e.g.
include ActionView::Helpers::DebugHelper
include ActionView::Helpers::NumberHelper
include ActionView::Helpers::RawOutputHelper
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::TranslationHelper
 
# # #
# route list view helpers (requires hirb)
 
# hirb view for a route
class Hirb::Helpers::Route < Hirb::Helpers::AutoTable
def self.render(output, options = {})
super( output.requirements.map{ |k,v|
[k, v.inspect]
}, options.merge({
:headers => [output.name || '', "#{ output.verb || 'ANY' } #{ output.path }"],
:unicode => true,
:description => nil,
}) )
end
end
Hirb.add_view ActionDispatch::Routing::Route, :class => Hirb::Helpers::Route
 
# short and long route list
def routes(long_output = false)
if long_output
Rails.application.routes.routes.each{ |e|
puts Hirb::Helpers::Route.render(e)
}
true
else
Hirb::Console.render_output Rails.application.routes.routes.map{|e|
[e.name || '', e.verb || 'ANY', e.path]
},{
:class => Hirb::Helpers::AutoTable,
:headers => %w<name verb path>,
}
end
end
 
# get a specific route via index or name
def route(index_or_name)
route = case index_or_name
when Integer
Rails.application.routes.routes[ index_or_name ]
when Symbol # named route
Rails.application.routes.named_routes.get index_or_name
end
end
 
# access to routeset for easy recognize / generate
def r
ActionController::Routing::Routes
end
 
# # #
# rails prompt
if ripl?
module Ripl::RailsPrompt
def prompt
@prompt = "#{ Rails.application.class.parent_name.downcase }(#{ Rails.env[0...3] })> "
super
end
end
Ripl::Shell.include Ripl::RailsPrompt
else
app_name = Rails.application.class.parent_name.downcase
app_env = Rails.env[0...3]
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
:PROMPT_I => "#{ app_name }(#{ app_env })> ",
:PROMPT_N => "#{ app_name }(#{ app_env })| ",
:PROMPT_C => "#{ app_name }(#{ app_env })| ",
:PROMPT_S => "#{ app_name }(#{ app_env })%l ",
:RETURN => "=> %s\n",
:AUTO_INDENT => true,
}
IRB.conf[:PROMPT_MODE] = :RAILS
end
 
# # #
# per project histories
history_file = File.join Dir.pwd, '.console_history'
if ripl?
Ripl.config[:history] = history_file
else
if !IRB.conf[:PROMPT][:RVM]
IRB.conf[:HISTORY_FILE] = history_file
else # RVM workaround, code from ~/.rvm/scripts/irbrc.rb
# NOTE: messes up your ~/.irb-history
# consider editing the rvm script directly
if File.exists?(history_file)
lines = IO.readlines(history_file).collect { |line| line.chomp }
Readline::HISTORY.clear
Readline::HISTORY.push(*lines)
end
 
Kernel::at_exit do
maxhistsize = IRB.conf[:SAVE_HISTORY] || 100
history_file = File.join Dir.pwd, ".console_history"
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[-maxhistsize, maxhistsize] if lines.compact.length > maxhistsize
File::open(history_file, "w+") { |io| io.puts lines.join("\n") }
end
end
end
 
# # #
# plain sql
def sql(query)
ActiveRecord::Base.connection.select_all(query)
end
 
# # #
# instead of User.find(...) you can do user(...)
# without arguments it only returns the model class
# based on http://www.clarkware.com/blog/2007/09/03/console-shortcut
Dir.glob( File.join(Dir.pwd, *%w<app models ** *.rb>) ).map { |file_name|
table_name = File.basename(file_name).split('.')[0..-2].join
Object.instance_eval do
define_method(table_name) do |*args|
table_class = table_name.camelize.constantize
if args.empty?
table_class
else
table_class.send(:find, *args)
end
end
end
}
 
# # #
# edit records with vim, emacs...
# class Url
# can_console_update
# end
# Url.first.console_update
# see https://github.com/cldwalker/console_update
require 'console_update'
# ConsoleUpdate.editor = 'vim' # not necessary if env var $EDITOR is set
 
# # #
# misc db helpers (requires hirb)
module DatabaseHelpers
extend self
 
def tables
Hirb::Console.render_output ActiveRecord::Base.connection.tables.map{|e|[e]},{
:class => Hirb::Helpers::AutoTable,
:headers => %w<tables>,
}
true
end
 
def table(which)
Hirb::Console.render_output ActiveRecord::Base.connection.columns(which).map{ |e|
[e.name, e.type, e.sql_type, e.limit, e.default, e.scale, e.precision, e.primary, e.null]
},{
:class => Hirb::Helpers::AutoTable,
:headers => %w<name type sql_type limit default scale precision primary null>,
}
true
end
 
def counts
Hirb::Console.render_output ActiveRecord::Base.connection.tables.map{|e|
[e, ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM #{e}")]
},{
:class => Hirb::Helpers::AutoTable,
:headers => %w<table count>,
}
true
end
 
# ...
end
def db; DatabaseHelpers; end
 
# # #
# gems & plugin initializers
 
# initiate authlogic session
if defined? Authlogic
Authlogic::Session::Base.controller =
Authlogic::ControllerAdapters::RailsAdapter.new(self)
end
 
# ...
 
 
# # #
# temp patches
 
# hirb vs irb vs ripl
=begin
class << Hirb::View
def enable_output_method
if defined?(Ripl) && Ripl.instance_variable_get(:@shell)
@output_method = true
require 'ripl/hirb'
elsif defined? IRB
@output_method = true
::IRB::Irb.class_eval do
alias_method :non_hirb_view_output, :output_value
def output_value #:nodoc:
Hirb::View.view_or_page_output(@context.last_value) || non_hirb_view_output
end
end
end
end
end
#Hirb::View.disable
#Hirb::View.enable
=end
 
# # #
end # if Rails.application
# # #
#
# J-_-L

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.