Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 52 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. class CommandGoToFile
  2.   def self.alternate(args)
  3.     current_file = RailsPath.new
  4.  
  5.     choice = args.empty? ? current_file.best_match : args.shift
  6.  
  7.     if choice.nil?
  8.       puts "This file is not associated with any other files"
  9.     elsif rails_path = current_file.rails_path_for(choice.to_sym)    
  10.       if !rails_path.exists?
  11.         rails_path, openatline, openatcol = create_file(rails_path, choice.to_sym)
  12.         if rails_path.nil?
  13.           TextMate.exit_discard
  14.         end
  15.         TextMate.rescan_project
  16.       end
  17.  
  18.       TextMate.open rails_path, openatline, openatcol
  19.     else
  20.       puts "#{current_file.basename} does not have a #{choice}"
  21.     end
  22.   end  
  23.  
  24.   def self.on_current_line
  25.     current_file = RailsPath.new
  26.  
  27.     # If the current line contains "render :partial", then open the partial.
  28.     case TextMate.current_line
  29.  
  30.       # Example: render :partial => 'account/login'
  31.       when /render[\s\(].*:partial\s*=>\s*['"](.+?)['"]/
  32.         partial_name = $1
  33.         modules = current_file.modules + [current_file.controller_name]
  34.  
  35.         # Check for absolute path to partial
  36.         if partial_name.include?('/')
  37.           pieces = partial_name.split('/')
  38.           partial_name = pieces.pop
  39.           modules = pieces
  40.         end
  41.  
  42.         partial = File.join(current_file.rails_root, 'app', 'views', modules, "_#{partial_name}.html.erb")
  43.         TextMate.open(partial)
  44.  
  45.       # Example: render :action => 'login'
  46.       when /render[\s\(].*:action\s*=>\s*['"](.+?)['"]/
  47.         action = $1
  48.         if current_file.file_type == :controller
  49.           current_file.buffer.line_number = 0
  50.           if search = current_file.buffer.find { /def\s+#{action}\b/ }
  51.             TextMate.open(current_file, search[0])
  52.           end
  53.         else
  54.           puts "Don't know where to go when rendering an action from outside a controller"
  55.           exit
  56.         end
  57.  
  58.       # Example: redirect_to :action => 'login'
  59.       when /(redirect_to|redirect_back_or_default)[\s\(]/
  60.         controller = action = nil
  61.         controller = $1 if TextMate.current_line =~ /.*:controller\s*=>\s*['"](.+?)['"]/
  62.         action = $1 if TextMate.current_line =~ /.*:action\s*=>\s*['"](.+?)['"]/
  63.  
  64.         unless current_file.file_type == :controller
  65.           puts "Don't know where to go when redirecting from outside a controller"
  66.           exit
  67.         end
  68.  
  69.         if controller.nil?
  70.           controller_file = current_file
  71.         else
  72.           # Check for modules
  73.           if controller.include?('/')
  74.             pieces = controller.split('/')
  75.             controller = pieces.pop
  76.             modules = pieces
  77.           end
  78.           other_path = File.join(current_file.rails_root, 'app', 'controllers', modules, "#{controller}_controller.rb")
  79.           controller_file = RailsPath.new(other_path)
  80.         end
  81.  
  82.         if search = controller_file.buffer.find(:direction => :backward) { /def\s+#{action}\b/ }
  83.           TextMate.open(controller_file, search[0])
  84.         else
  85.           puts "Couldn't find the #{action} action inside '#{controller_file.basename}'"
  86.           exit
  87.         end
  88.  
  89.       # Example: <script src="/javascripts/controls.js">
  90.       when /\<script.+src=['"](.+\.js)['"]/
  91.         javascript = $1
  92.         if javascript =~ %r{^https?://}
  93.           TextMate.open_url javascript
  94.         else
  95.           full_path = File.join(current_file.rails_root, 'public', javascript)
  96.           TextMate.open full_path
  97.         end
  98.  
  99.       # Example: <%= javascript_include_tag 'general' %>
  100.       # require_javascript is used by bundled_resource plugin
  101.       when /(require_javascript|javascript_include_tag)\b/
  102.         if match = TextMate.current_line.unstringify_hash_arguments.find_nearest_string_or_symbol(TextMate.column_number)
  103.           javascript = match[0]
  104.           javascript += '.js' if not javascript =~ /\.js$/
  105.           # If there is no leading slash, assume it's a js from the public/javascripts dir
  106.           public_file = javascript[0..0] == "/" ? javascript[1..-1] : "javascripts/#{javascript}"
  107.           TextMate.open File.join(current_file.rails_root, 'public', public_file)
  108.         else
  109.           puts "No javascript identified"
  110.         end
  111.  
  112.       # Example: <link href="/stylesheets/application.css">
  113.       # Example: @import url(/stylesheets/conferences.css);
  114.       when /\<link.+href=['"](.+\.css)['"]/, /\@import.+url\((.+\.css)\)/
  115.         stylesheet = $1
  116.         if stylesheet =~ %r{^https?://}
  117.           TextMate.open_url stylesheet
  118.         else
  119.           full_path = File.join(current_file.rails_root, 'public', stylesheet[1..-1])
  120.           TextMate.open full_path
  121.         end
  122.  
  123.       # Example: <%= stylesheet_link_tag 'application' %>
  124.       when /(require_stylesheet|stylesheet_link_tag)\b/
  125.         if match = TextMate.current_line.unstringify_hash_arguments.find_nearest_string_or_symbol(TextMate.column_number)
  126.           stylesheet = match[0]
  127.           stylesheet += '.css' if not stylesheet =~ /\.css$/
  128.           # If there is no leading slash, assume it's a js from the public/javascripts dir
  129.           public_file = stylesheet[0..0] == "/" ? stylesheet[1..-1] : "stylesheets/#{stylesheet}"
  130.           TextMate.open File.join(current_file.rails_root, 'public', public_file)
  131.         else
  132.           puts "No stylesheet identified"
  133.         end
  134.  
  135.       else
  136.         puts "No 'go to file' directives found on this line."
  137.         # Do nothing -- beep?
  138.     end    
  139.   end
  140.  
  141.   protected
  142.  
  143.   # Returns the rails_path of the newly created file plus the position
  144.   # (zero based) in the file where to place the caret after opening the
  145.   # new file. Returns nil when no new file is created.
  146.   def self.create_file(rails_path, choice)      
  147.     return nil if rails_path.exists?
  148.     if choice == :view
  149.       filename = TextMate::UI.request_string(
  150.         :title => "View File Not Found",
  151.         :default => rails_path.basename,
  152.         :prompt => "Enter the name of the new view file:",
  153.         :button1 => 'Create'
  154.       )
  155.       return nil if !filename
  156.       rails_path = RailsPath.new(File.join(rails_path.dirname, filename))
  157.       rails_path.touch
  158.       return [rails_path, 0, 0]
  159.     end
  160.    
  161.     unless TextMate::UI.request_confirmation(
  162.       :button1 => "Create",
  163.       :button2 => "Cancel",
  164.       :title => "Missing #{rails_path.basename}",
  165.       :prompt => "Create missing #{rails_path.basename}?"
  166.     )
  167.       return nil
  168.     end
  169.  
  170.     generated_code, openatline, openatcol = case choice
  171.     when :model
  172.       ["class #{Inflector.singularize rails_path.controller_name.camelize} < ActiveRecord::Base\n\nend", 1, 0]
  173.     when :controller
  174.       ["class #{rails_path.controller_name.camelize}Controller < ApplicationController\n\nend", 1, 0]
  175.     when :helper
  176.       ["module #{rails_path.controller_name.camelize}Helper\n\nend", 1, 0]
  177.     when :unit_test
  178.       ["require File.dirname(__FILE__) + '/../test_helper'
  179.  
  180. class #{Inflector.singularize(rails_path.controller_name).camelize}Test < ActiveSupport::TestCase
  181.  # Replace this with your real tests.
  182.  def test_truth
  183.    assert true
  184.  end
  185. end", 3, 0]  
  186.     when :functional_test
  187.       ["require File.dirname(__FILE__) + '/../test_helper'
  188.  
  189. class #{rails_path.controller_name.camelize}ControllerTest < ActionController::TestCase    
  190.  
  191. end", 3, 0]
  192.     end
  193.  
  194.     rails_path.touch
  195.     rails_path.append generated_code if generated_code
  196.     return [rails_path, openatline, openatcol]
  197.   end
  198.    
  199. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top