forked from tomtt/emacs-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails-model-layout.el
150 lines (137 loc) · 8.25 KB
/
rails-model-layout.el
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
;;; rails-model-layout.el ---
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>
;; Keywords: ruby rails languages oop
;; $URL$
;; $Id$
;;; License
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Code:
(defun rails-model-layout:keymap (type)
(let* ((name (capitalize (substring (symbol-name type) 1)))
(map (make-sparse-keymap))
(menu (make-sparse-keymap)))
(when type
(define-keys menu
([goto-model] '(menu-item "Go to Model"
rails-model-layout:switch-to-model
:enable (and (not (eq (rails-core:buffer-type) :model))
(rails-core:model-exist-p (rails-core:current-model)))))
([goto-utest] '(menu-item "Go to Unit Test"
rails-model-layout:switch-to-unit-test
:enable (and (not (eq (rails-core:buffer-type) :unit-test))
(rails-core:unit-test-exist-p (or (rails-core:current-model)
(rails-core:current-mailer))))))
([goto-rspec] '(menu-item "Go to RSpec"
rails-model-layout:switch-to-rspec-model
:enable (and (not (eq (rails-core:buffer-type) :rspec-model))
(rails-core:rspec-model-exist-p (or (rails-core:current-model)
(rails-core:current-mailer))))))
([goto-migration] '(menu-item "Go to Migration"
rails-model-layout:switch-to-migration
:enable (and (not (eq (rails-core:buffer-type) :migration))
(rails-core:migration-file-by-model (rails-core:current-model)))))
([goto-controller] '(menu-item "Go to Controller"
rails-model-layout:switch-to-controller
:enable (rails-core:controller-file-by-model (rails-core:current-model))))
([goto-fixture] '(menu-item "Go to Fixture"
rails-model-layout:switch-to-fixture
:enable (and (not (eq (rails-core:buffer-type) :fixture))
(rails-core:fixture-exist-p (rails-core:current-model)))))
([goto-rspec-fixture] '(menu-item "Go to RSpec Fixture"
rails-model-layout:switch-to-rspec-fixture
:enable (and (not (eq (rails-core:buffer-type) :rspec-fixture))
(rails-core:rspec-fixture-exist-p (rails-core:current-model)))))
([goto-mailer] '(menu-item "Go to Mailer"
rails-model-layout:switch-to-mailer
:enable (rails-core:mailer-exist-p (rails-core:current-mailer)))))
(define-keys map
((rails-key "m") 'rails-model-layout:switch-to-model)
((rails-key "u") 'rails-model-layout:switch-to-unit-test)
((rails-key "r") 'rails-model-layout:switch-to-rspec-model)
((rails-key "g") 'rails-model-layout:switch-to-migration)
((rails-key "c") 'rails-model-layout:switch-to-controller)
((rails-key "x") 'rails-model-layout:switch-to-fixture)
((rails-key "z") 'rails-model-layout:switch-to-rspec-fixture)
((rails-key "n") 'rails-model-layout:switch-to-mailer)
([menu-bar rails-model-layout] (cons name menu))))
map))
(defun rails-model-layout:switch-to (type)
(let* ((name (capitalize (substring (symbol-name type) 1)))
(model (rails-core:current-model))
(controller (rails-core:current-controller))
(mailer (rails-core:current-mailer))
(item (if controller controller model))
(item (case type
(:mailer (rails-core:mailer-file mailer))
(:controller (rails-core:controller-file-by-model model))
(:fixture (rails-core:fixture-file model))
(:rspec-fixture (rails-core:rspec-fixture-file model))
(:unit-test (rails-core:unit-test-file item))
(:rspec-model (rails-core:rspec-model-file item))
(:model (rails-core:model-file model))
(:migration (rails-core:migration-file-by-model model)))))
(if item
(let ((file (rails-core:file item)))
(if (file-exists-p file)
(progn
(find-file file)
(message (format "%s: %s" (substring (symbol-name type) 1) item)))
(message "File %s does not exist" file)))
(message "%s not found" name))))
(defun rails-model-layout:switch-to-mailer () (interactive) (rails-model-layout:switch-to :mailer))
(defun rails-model-layout:switch-to-controller () (interactive) (rails-model-layout:switch-to :controller))
(defun rails-model-layout:switch-to-fixture () (interactive) (rails-model-layout:switch-to :fixture))
(defun rails-model-layout:switch-to-rspec-fixture () (interactive) (rails-model-layout:switch-to :rspec-fixture))
(defun rails-model-layout:switch-to-unit-test () (interactive) (rails-model-layout:switch-to :unit-test))
(defun rails-model-layout:switch-to-rspec-model () (interactive) (rails-model-layout:switch-to :rspec-model))
(defun rails-model-layout:switch-to-model () (interactive) (rails-model-layout:switch-to :model))
(defun rails-model-layout:switch-to-migration () (interactive) (rails-model-layout:switch-to :migration))
(defun rails-model-layout:menu ()
(interactive)
(let* ((item (list))
(type (rails-core:buffer-type))
(title (capitalize (substring (symbol-name type) 1)))
(model (rails-core:current-model))
(controller (pluralize-string model))
(mailer (rails-core:current-mailer)))
(when model
(when (and (not (eq type :migration))
(rails-core:migration-file-by-model model))
(add-to-list 'item (cons "Migration" :migration)))
(unless (eq type :fixture)
(add-to-list 'item (cons "Fixture" :fixture)))
(unless (eq type :rspec-fixture)
(add-to-list 'item (cons "RSpec Fixture" :rspec-fixture)))
(when (rails-core:controller-exist-p controller)
(add-to-list 'item (cons "Controller" :controller)))
(unless (eq type :unit-test)
(add-to-list 'item (cons "Unit Test" :unit-test)))
(unless (eq type :rspec-model)
(add-to-list 'item (cons "RSpec" :rspec-model)))
(unless (eq type :model)
(add-to-list 'item (cons "Model" :model))))
(when mailer
(setq item (rails-controller-layout:views-menu model))
(add-to-list 'item (rails-core:menu-separator))
(add-to-list 'item (cons "Mailer" :mailer)))
(when item
(setq item
(rails-core:menu
(list (concat title " " model)
(cons "Please select.."
item))))
(typecase item
(symbol (rails-model-layout:switch-to item))
(string (rails-core:find-file-if-exist item))))))
(provide 'rails-model-layout)