Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of github.com:codegram/dotfiles

  • Loading branch information...
commit ee0f253cc436434d01f1e90ffaeefc5ae9c8a5c8 2 parents db1afba + 9207863
Josep M. Bach authored January 18, 2012

Showing 2 changed files with 255 additions and 12 deletions. Show diff stats Hide diff stats

  1. 15  ackrc
  2. 252  railsrc
15  ackrc
... ...
@@ -0,0 +1,15 @@
  1
+--type-add=ruby=.rake,.rsel,.builder
  2
+--type-add=html=.html.erb,.html.haml,.html.slim
  3
+--type-add=js=.js.erb
  4
+--type-add=css=.sass,.scss
  5
+--type-set=feature=.feature
  6
+--ignore-dir=vendor
  7
+--ignore-dir=log
  8
+--ignore-dir=tmp
  9
+--ignore-dir=doc
  10
+--ignore-dir=coverage
  11
+--sort-files
  12
+--no-sql
  13
+--color
  14
+--follow
  15
+--group
252  railsrc
... ...
@@ -1,20 +1,248 @@
1  
-#!/usr/bin/ruby
  1
+# .railsrc for Rails 3, encoding: utf-8
  2
+#   see http://rbjl.net/49-railsrc-rails-console-snippets
2 3
 
3  
-begin
4  
-  require 'hirb' # sudo gem install cldwalker-hirb --source http://gems.github.com
5  
-  Hirb.enable
6  
-rescue LoadError
  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::RawOutputHelper
  30
+include ActionView::Helpers::SanitizeHelper
  31
+include ActionView::Helpers::TagHelper
  32
+include ActionView::Helpers::TextHelper
  33
+include ActionView::Helpers::TranslationHelper
  34
+
  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
  49
+end
  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
  67
+end
  68
+
  69
+# get a specific route via index or name
  70
+def route(index_or_name)
  71
+  route = case index_or_name
  72
+  when Integer
  73
+    Rails.application.routes.routes[ index_or_name ]
  74
+  when Symbol # named route
  75
+    Rails.application.routes.named_routes.get index_or_name
  76
+  end
  77
+end
  78
+
  79
+# access to routeset for easy recognize / generate
  80
+def r 
  81
+  ActionController::Routing::Routes
7 82
 end
8 83
 
9  
-def change_log(stream)
10  
-  ActiveRecord::Base.logger = Logger.new(stream)
11  
-  ActiveRecord::Base.clear_active_connections!
  84
+# # #
  85
+# rails prompt
  86
+if ripl?
  87
+  module Ripl::RailsPrompt
  88
+    def prompt
  89
+      @prompt = "#{ Rails.application.class.parent_name.downcase }(#{ Rails.env[0...3] })> "
  90
+      super
  91
+    end
  92
+  end
  93
+  Ripl::Shell.include Ripl::RailsPrompt
  94
+else
  95
+  app_name = Rails.application.class.parent_name.downcase
  96
+  app_env  = Rails.env[0...3]
  97
+  IRB.conf[:PROMPT] ||= {}
  98
+  IRB.conf[:PROMPT][:RAILS] = {
  99
+    :PROMPT_I => "#{ app_name }(#{ app_env })> ",
  100
+    :PROMPT_N => "#{ app_name }(#{ app_env })| ",
  101
+    :PROMPT_C => "#{ app_name }(#{ app_env })| ",
  102
+    :PROMPT_S => "#{ app_name }(#{ app_env })%l ",
  103
+    :RETURN => "=> %s\n",
  104
+    :AUTO_INDENT => true,
  105
+  }
  106
+  IRB.conf[:PROMPT_MODE] = :RAILS
12 107
 end
13 108
 
14  
-def show_log
15  
-  change_log(STDOUT)
  109
+# # #
  110
+# per project histories
  111
+history_file = File.join Dir.pwd, '.console_history'
  112
+if ripl?
  113
+  Ripl.config[:history] = history_file
  114
+else
  115
+  if !IRB.conf[:PROMPT][:RVM]
  116
+    IRB.conf[:HISTORY_FILE] = history_file
  117
+  else # RVM workaround, code from ~/.rvm/scripts/irbrc.rb
  118
+    # NOTE: messes up your ~/.irb-history
  119
+    # consider editing the rvm script directly
  120
+    if File.exists?(history_file)
  121
+      lines = IO.readlines(history_file).collect { |line| line.chomp }
  122
+      Readline::HISTORY.clear
  123
+      Readline::HISTORY.push(*lines)
  124
+    end
  125
+
  126
+    Kernel::at_exit do
  127
+      maxhistsize = IRB.conf[:SAVE_HISTORY] || 100
  128
+      history_file = File.join Dir.pwd, ".console_history"
  129
+      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
  130
+      lines = lines[-maxhistsize, maxhistsize] if lines.compact.length > maxhistsize
  131
+      File::open(history_file, "w+") { |io| io.puts lines.join("\n") }
  132
+    end
  133
+  end
16 134
 end
17 135
 
18  
-def hide_log
19  
-  change_log(nil)
  136
+# # #
  137
+# plain sql
  138
+def sql(query)
  139
+  ActiveRecord::Base.connection.select_all(query)
20 140
 end
  141
+
  142
+# # #
  143
+# instead of User.find(...) you can do user(...)
  144
+#   without arguments it only returns the model class
  145
+#   based on http://www.clarkware.com/blog/2007/09/03/console-shortcut
  146
+Dir.glob( File.join(Dir.pwd, *%w<app models ** *.rb>) ).map { |file_name|
  147
+  table_name = File.basename(file_name).split('.')[0..-2].join
  148
+  Object.instance_eval do
  149
+    define_method(table_name) do |*args|
  150
+      table_class = table_name.camelize.constantize
  151
+      if args.empty?
  152
+        table_class
  153
+      else
  154
+        table_class.send(:find, *args)
  155
+      end
  156
+    end
  157
+  end
  158
+}
  159
+
  160
+# # #
  161
+# edit records with vim, emacs...
  162
+# class Url
  163
+#   can_console_update
  164
+# end
  165
+# Url.first.console_update
  166
+# see https://github.com/cldwalker/console_update
  167
+require 'console_update'
  168
+# ConsoleUpdate.editor = 'vim' # not necessary if env var $EDITOR is set
  169
+
  170
+# # #
  171
+# misc db helpers (requires hirb)
  172
+module DatabaseHelpers
  173
+  extend self
  174
+
  175
+  def tables
  176
+    Hirb::Console.render_output ActiveRecord::Base.connection.tables.map{|e|[e]},{
  177
+      :class   => Hirb::Helpers::AutoTable,
  178
+      :headers => %w<tables>,
  179
+    }
  180
+    true
  181
+  end
  182
+
  183
+  def table(which)
  184
+    Hirb::Console.render_output ActiveRecord::Base.connection.columns(which).map{ |e|
  185
+      [e.name, e.type, e.sql_type, e.limit, e.default, e.scale, e.precision, e.primary, e.null]
  186
+    },{
  187
+      :class   => Hirb::Helpers::AutoTable,
  188
+      :headers => %w<name type sql_type limit default scale precision primary null>,
  189
+    }
  190
+    true
  191
+  end
  192
+
  193
+  def counts
  194
+    Hirb::Console.render_output ActiveRecord::Base.connection.tables.map{|e|
  195
+      [e, ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM #{e}")]
  196
+    },{
  197
+      :class   => Hirb::Helpers::AutoTable,
  198
+      :headers => %w<table count>,
  199
+    }
  200
+    true
  201
+  end
  202
+
  203
+  # ...
  204
+end
  205
+def db; DatabaseHelpers; end
  206
+
  207
+# # # 
  208
+# gems & plugin initializers
  209
+
  210
+# initiate authlogic session
  211
+if defined? Authlogic
  212
+  Authlogic::Session::Base.controller =
  213
+      Authlogic::ControllerAdapters::RailsAdapter.new(self)
  214
+end
  215
+
  216
+# ...
  217
+
  218
+
  219
+# # #
  220
+# temp patches
  221
+
  222
+# hirb vs irb vs ripl
  223
+=begin
  224
+class << Hirb::View
  225
+  def enable_output_method
  226
+    if defined?(Ripl) && Ripl.instance_variable_get(:@shell)
  227
+      @output_method = true
  228
+      require 'ripl/hirb'
  229
+    elsif defined? IRB
  230
+      @output_method = true
  231
+      ::IRB::Irb.class_eval do
  232
+        alias_method :non_hirb_view_output, :output_value
  233
+        def output_value #:nodoc:
  234
+          Hirb::View.view_or_page_output(@context.last_value) || non_hirb_view_output
  235
+        end
  236
+      end
  237
+    end
  238
+  end
  239
+end
  240
+#Hirb::View.disable
  241
+#Hirb::View.enable
  242
+=end
  243
+
  244
+# # #
  245
+end # if Rails.application
  246
+# # #
  247
+#
  248
+# J-_-L

0 notes on commit ee0f253

Please sign in to comment.
Something went wrong with that request. Please try again.