Class: RailsLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/hilfer/rails_locator.rb

Overview

This handles various keypresses that jump to specific locations in a Rails project. If a specific file isn’t selected, the jump will go to the directory. Otherwise it will go to a specific file.

For example, if user_controller.rb is currently selected and Ctrl-Shift-T is pressed, user_controller_test.rb will be selected.

TODO fix multiple selections

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(treeviewer) ⇒ RailsLocator

Returns a new instance of RailsLocator.



19
20
21
22
23
24
25
26
27
28
# File 'lib/hilfer/rails_locator.rb', line 19

def initialize( treeviewer )
  @treeviewer = treeviewer

  @controller_re =       %r|^(.*?)/app/controllers/(.*?)/?(.*?)_controller.rb$|
  @view_re =             %r|^(.*?)/app/views/(.*?)/?([^/.]*?\..*?)?$|
  @model_re =            %r|^(.*?)/app/models/(.*?/?)([^/.]*?).rb$|
  @unit_test_re =        %r|^(.*?)/test/unit/(.*?/?)(.*?)_test.rb$|
  @functional_test_re =  %r|^(.*?)/test/functional/(.*?/?)(.*?)_controller_test.rb$|
  @fixture_re =          %r|^(.*?)/test/fixtures/(.*?/?)(.*?).yml$|
end

Instance Attribute Details

#treeviewerObject

Returns the value of attribute treeviewer.



17
18
19
# File 'lib/hilfer/rails_locator.rb', line 17

def treeviewer
  @treeviewer
end

Instance Method Details

#goto_rails_path(fs_path) ⇒ Object



53
54
55
# File 'lib/hilfer/rails_locator.rb', line 53

def goto_rails_path( fs_path )
  treeviewer.select_fs_path( rails_root + fs_path, true )
end

#handle_keypress(widget, event) ⇒ Object



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
# File 'lib/hilfer/rails_locator.rb', line 57

def handle_keypress( widget, event )
  puts "RailsLocator::event: #{event.inspect}" if $options[:debug]
  puts "RailsLocator::event.hardware_keycode: #{event.hardware_keycode}" if $options[:debug]
  retval = true
  case
  # Shift-Ctrl-V - go to view, or view dir
  when event.hardware_keycode == 55 && event.state.control_mask?  && event.state.shift_mask?
    select_related_paths( widget, event ) do |item|
      case
        when @controller_re.match( item.path )
          '/app/views/' + $2 + $3
        when @functional_test_re.match( item.path )
          '/app/views/' + $2 + $3
        else
          '/app/views'
      end
    end

  # Shift-Ctrl-C - go to controller, or controller dir
  when event.hardware_keycode == 54 && event.state.control_mask?  && event.state.shift_mask?
    select_related_paths( widget, event ) do |item|
      case
        when md = @view_re.match( item.path )
          '/app/controllers/' + $2 + '_controller.rb'
        when md = @functional_test_re.match( item.path )
          '/app/controllers/' + $2 + $3 + '_controller.rb'
        else
          '/app/controllers'
      end
    end

  # Shift-Ctrl-M - go to model, or model dir
  when event.hardware_keycode == 58 && event.state.control_mask? && event.state.shift_mask?
    select_related_paths( widget, event ) do |item|
      case
      when md = @unit_test_re.match( item.path )
        '/app/models' + $2 + '/' + $3 + '.rb'
      when md = @fixture_re.match( item.path )
        '/app/models' + $2 + '/' + $3.singularize + '.rb'
      else
        '/app/models'
      end
    end

  # Shift-Ctrl-T - go to test, or test dir
  when event.hardware_keycode == 28 && event.state.control_mask?  && event.state.shift_mask?
    select_related_paths( widget, event ) do |item|
      case
        when md = @model_re.match( item.path )
          '/test/unit/' + $2 + $3 + '_test.rb'
        when md = @view_re.match( item.path )
          '/test/functional/' + $2 + '_controller_test.rb'
        when md = @controller_re.match( item.path )
          '/test/functional/' + $2 + $3 + '_controller_test.rb'
        when md = @fixture_re.match( item.path )
          '/test/unit/' + $2 + $3.singularize + '_test.rb'
        else
          rp = Pathname.new( rails_root )
          if ( rp + 'test' ).exist?
            '/test'
          elsif (rp + 'spec').exist?
            '/spec'
          end
      end
    end

  # Shift-Ctrl-H - go to helpers dir
  when event.hardware_keycode == 43 && event.state.control_mask? && event.state.shift_mask?
    goto_rails_path '/app/helpers'

  # Shift-Ctrl-L - go to layouts dir
  when event.hardware_keycode == 46 && event.state.control_mask? && event.state.shift_mask?
    goto_rails_path '/app/views/layouts'

  # Shift-Ctrl-B - go to lib dir
  when event.hardware_keycode == 56 && event.state.control_mask? && event.state.shift_mask?
    goto_rails_path '/lib'

  # Shift-Ctrl-I - go to migrations
  when event.hardware_keycode == 31 && event.state.control_mask?  && event.state.shift_mask?
    goto_rails_path '/db/migrate'

  # Shift-Ctrl-O - go to config
  when event.hardware_keycode == 32 && event.state.control_mask?  && event.state.shift_mask?
    goto_rails_path '/config'

  # Shift-Ctrl-U - go to routes.rb
  when event.hardware_keycode == 30 && event.state.control_mask?  && event.state.shift_mask?
    goto_rails_path '/config/routes.rb'

  # Shift-Ctrl-S - go to stylesheets
  when event.hardware_keycode == 39 && event.state.control_mask?  && event.state.shift_mask?
    goto_rails_path '/public/stylesheets'

  # Shift-Ctrl-F - go to fixtures
  when event.hardware_keycode == 41 && event.state.control_mask?  && event.state.shift_mask?
    select_related_paths( widget, event ) do |item|
      case
        when md = @model_re.match( item.path )
          '/test/fixtures' + $2 + '/' + $3.pluralize + '.yml'
        when md = @unit_test_re.match( item.path )
          '/test/fixtures' + $2 + '/' + $3.pluralize + '.yml'
        else
          '/test/fixtures'
      end
    end

  else
    retval = false
  end
  retval
end

#rails_rootObject



49
50
51
# File 'lib/hilfer/rails_locator.rb', line 49

def rails_root
  treeviewer.root_item.path
end

this does the common code for each keypress the block should return a related path for the particular keypress on the currently selected item



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hilfer/rails_locator.rb', line 34

def select_related_paths( widget, event, &block )
  items = []
  paths_to_select = []
  widget.selection.selected_each do
    |model, path, iter|
    item = model.get_value( iter, 0 )
    puts "item: #{item.path}" if $options[:debug]
    new_rails_path = yield( item )
    puts "new_rails_path #{new_rails_path}" if $options[:debug]
    paths_to_select << rails_root + new_rails_path
  end

  treeviewer.select_fs_paths( paths_to_select, true )
end