Awesome
Projectile Rails
Synopsis
Projectile Rails is a minor mode for working with Ruby on Rails applications and engines in GNU Emacs. Internally it is based on Projectile.
It means that you can use Projectile's commands for greping (or acking) files, run tests, switch between projects, etc.
With Projectile Rails you are able to:
- navigate through rails resources (controllers, views, helpers and so on)
- jump to ruby classes and template files
- run
rake
- run
rails console
- run
rails dbconsole
- run
rails generate
- run
rails server
- open log files with
auto-revert-mode
on - see rails keywords highlighted
- take advantage of zeus and spring preloaders
It can be a replacement for rinari.
Setup
Installation
MELPA
Once you have setup Melpa you can use package-install
command to install Projectile Rails. The package name is projectile-rails
.
el-get
The package name for el-get is projectile-rails
.
Usage
The global mode
Use the package as a global mode:
(projectile-rails-global-mode)
That will turn on the local projectile-rails-mode
for the buffers which belong to a Rails project (either application or an engine).
Probably you should read Projectile's README on setting up the completion system, caching and indexing files. Although the default settings are quite sensible and you should be ready to go without much tweaking.
Customizing
Keymap prefix
Prior to version 0.20.0 the keymap prefix used to be setup. Following the convention we have stopped doing this automatically. Now you need to attach the mode's map to the desired key yourself. For example to get the old key you can do:
(define-key projectile-rails-mode-map (kbd "C-c r") 'projectile-rails-command-map)
Keywords
The mode's buffers will have the Rails keywords highlighted. To turn it off:
(setq projectile-rails-add-keywords nil)
Snippets
If you have yas-minor-mode or yas-global-mode and auto-insert-mode
enabled and you open a new file it will be filled with a skeleton class. To turn it off:
(setq projectile-rails-expand-snippet nil)
Note: this variable controls whether auto-insert-mode
is configured, that is if you set this variable to nil after you opened any rails file the snippets will still be inserted. In order to disable this feature in such scenarios just disable auto-insert-mode
instead.
ANSI Colors
By default the buffer of the projectile-rails-server-mode
is applying the ansi colors. If you find it slow you can disable it with:
(setq projectile-rails-server-mode-ansi-colors nil)
External commands
You can customize the way the rails
, spring
and zeus
commands are invoked. For example if you want to use binstubs:
(setq projectile-rails-vanilla-command "bin/rails"
projectile-rails-spring-command "bin/spring"
projectile-rails-zeus-command "bin/zeus")
Interactive commands
The keymap is unbound by default. The following keybinding assume that you've bound it to C-c r
. See Keymap prefix section for details.
Command | Keybinding | Description |
---|---|---|
projectile-rails-find-model | <kbd>C-c r m</kbd> | Find a model using projectile-completion-system . |
projectile-rails-find-current-model | <kbd>C-c r M</kbd> | Go to a model connected with the current resource. |
projectile-rails-find-controller | <kbd>C-c r c</kbd> | Find a controller using projectile-completion-system . |
projectile-rails-find-current-controller | <kbd>C-c r C</kbd> | Go to a controller connected with the current resource. |
projectile-rails-find-view | <kbd>C-c r v</kbd> | Find a template or partial using projectile-completion-system . |
projectile-rails-find-current-view | <kbd>C-c r V</kbd> | Go to a view connected with the current resource. |
projectile-rails-find-helper | <kbd>C-c r h</kbd> | Find a helper using projectile-completion-system . |
projectile-rails-find-current-helper | <kbd>C-c r H</kbd> | Go to a helper connected with the current resource. |
projectile-rails-find-lib | <kbd>C-c r l</kbd> | Find a lib using projectile-completion-system . |
projectile-rails-find-feature | <kbd>C-c r f</kbd> | Find a feature using projectile-completion-system . |
projectile-rails-find-spec | <kbd>C-c r p</kbd> | Find a spec using projectile-completion-system . |
projectile-rails-find-current-spec | <kbd>C-c r P</kbd> | Go to a spec connected with the current resource. |
projectile-rails-find-test | <kbd>C-c r t</kbd> | Find a test using projectile-completion-system . |
projectile-rails-find-current-test | <kbd>C-c r T</kbd> | Go to a test connected with the current resource. |
projectile-rails-find-migration | <kbd>C-c r n</kbd> | Find a migration using projectile-completion-system . |
projectile-rails-find-current-migration | <kbd>C-c r N</kbd> | Go to a migration connected with the current resource. |
projectile-rails-find-fixture | <kbd>C-c r u</kbd> | Find a fixture using projectile-completion-system . |
projectile-rails-find-current-fixture | <kbd>C-c r U</kbd> | Go to a fixture connected with the current resource. |
projectile-rails-find-javascript | <kbd>C-c r j</kbd> | Find a javascript using projectile-completion-system . |
projectile-rails-find-stylesheet | <kbd>C-c r s</kbd> | Find a stylesheet using projectile-completion-system . |
projectile-rails-find-log | <kbd>C-c r o</kbd> | Find a log file and enable auto-revert-tail-mode in its buffer. |
projectile-rails-find-initializer | <kbd>C-c r i</kbd> | Find an initializer file using projectile-completion-system . |
projectile-rails-find-environment | <kbd>C-c r e</kbd> | Find an environment file using projectile-completion-system . |
projectile-rails-find-locale | <kbd>C-c r a</kbd> | Find a locale file using projectile-completion-system . |
projectile-rails-find-mailer | <kbd>C-c r @</kbd> | Find a mailer file using projectile-completion-system . |
projectile-rails-find-validator | <kbd>C-c r !</kbd> | Find a validator file using projectile-completion-system . |
projectile-rails-find-layout | <kbd>C-c r y</kbd> | Find a layout file using projectile-completion-system . |
projectile-rails-find-rake-task | <kbd>C-c r k</kbd> | Find a rake task file using rake-completion-system . |
projectile-rails-find-job | <kbd>C-c r b</kbd> | Find a job file using projectile-completion-system . |
projectile-rails-dbconsole | <kbd>C-c r ! b</kbd> | Run rails dbconsole command in sql-interactive-mode . |
projectile-rails-console | <kbd>C-c r ! c</kbd>, <kbd>C-c r r</kbd> | Run rails console command in inf-ruby buffer. |
projectile-rails-server | <kbd>C-c r ! s</kbd>, <kbd>C-c r R</kbd> | Run rails server . |
projectile-rails-rake | <kbd>C-c r ! r</kbd> | Select a rake task to run using rake-completion-system . |
projectile-rails-generate | <kbd>C-c r ! g</kbd> | Run rails generate command. |
projectile-rails-extract-region | <kbd>C-c r x</kbd> | Extract the selected region to a partial. |
projectile-rails-goto-file-at-point | <kbd>C-c r RET</kbd>, <kbd>C-c r g f</kbd> | Go to a file at point. Depending on the context that might be a constant, template or partial, or a gem. |
projectile-rails-goto-gemfile | <kbd>C-c r g g</kbd> | Go to Gemfile file. |
projectile-rails-goto-routes | <kbd>C-c r g r</kbd> | Go to config/routes.rb file. |
projectile-rails-goto-schema | <kbd>C-c r g d</kbd> | Go to db/schema.rb file. |
projectile-rails-goto-seeds | <kbd>C-c r g s</kbd> | Go to db/seeds.rb file. |
projectile-rails-goto-spec-helper | <kbd>C-c r g h</kbd> | Go to spec/spec_helper.rb file. |
You might want to create your own keybinding for your favorite commands. For example:
(define-key projectile-rails-mode-map (kbd "s-m") 'projectile-rails-find-model)
(define-key projectile-rails-mode-map (kbd "s-c") 'projectile-rails-find-controller)
(define-key projectile-rails-mode-map (kbd "s-v") 'projectile-rails-find-view)
(define-key projectile-rails-mode-map (kbd "s-RET") 'projectile-rails-goto-file-at-point)
(define-key projectile-rails-mode-map (kbd "C-c g") projectile-rails-mode-goto-map)
Discover
There's also integration with discover.el. The key that trigger the menu is s-r
(the "s" stands for Win/Command key).
Hydra
There's also integration with hydra.
The name of the hydra hydra-projectile-rails
. In order to bind it you can something like this:
(define-key projectile-rails-mode-map (kbd "s-r") 'hydra-projectile-rails/body)
Beyond
Consider installing other Emacs packages that can help you working specifically with Rails projects.
Templates
Extension | Alternatives |
---|---|
erb | web-mode, mmm-mode, rhtml-mode |
haml | haml-mode |
slim | emacs-slim |
yaml | yaml-mode |
Running ruby gems
Some of the Projectile Rails functions run rake
or rails
executables. If you are using a ruby version manager you might need to configure your Emacs to play nicely with it.
OS X users might want to look at exec-path-from-shell.
Miscellaneous
- bundler.el to interact with Bundler.
- rspec-mode to run and edit spec files.
- ruby-test-mode to run and edit test files.
- feature-mode to edit feature files.
- robe to view gems documentation and jump to methods and classes definitions.
- magit to interact with git.
Caveat
Running pry instead of irb
-
Pry's paging is not working in emacs. It should be disabled with
Pry.config.pager = false if ENV["INSIDE_EMACS"]
. Reference. -
When
projectile-rails-console
runs rails console using a pre-loader (zeus or spring) and pry's indent correction is enabled then pry will insert some ansi codes that are misinterpreted bycomint-mode
. A workaround is to disable the indentation correction withPry.config.correct_indent = false
. Reference. Issue.
Debugging
To use binding.pry
or byebug
, install inf-ruby
and add this to
your init file:
(add-hook 'after-init-hook 'inf-ruby-switch-setup)
Contributors
Here's a list of the people that contributed to the projects. Many thanks! :)
Contribution
Install cask if you haven't already, then:
$ cd /path/to/projectile-rails
$ cask
Run all tests with:
$ make test
For all of them to pass you will need the bundle
executable in your path.