1
;;; rails-controller-layout.el ---
2
3
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
4
5
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>
6
7
;; Keywords: ruby rails languages oop
8
;; $URL$
9
;; $Id$
10
11
;;; License
12
13
;; This program is free software; you can redistribute it and/or
14
;; modify it under the terms of the GNU General Public License
15
;; as published by the Free Software Foundation; either version 2
16
;; of the License, or (at your option) any later version.
17
18
;; This program is distributed in the hope that it will be useful,
19
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
;; GNU General Public License for more details.
22
23
;; You should have received a copy of the GNU General Public License
24
;; along with this program; if not, write to the Free Software
25
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26
27
;;; Code:
28
29
(defvar rails-controller-layout:recent-template-type nil)
30
31
(defun rails-controller-layout:switch-to-action-in-controller (controller-name action-name)
32
  "Open CONTROLLER-NAME and go to ACTION-NAME."
33
  (if (or (rails-core:find-file-if-exist (rails-core:controller-file controller-name))
34
          (rails-core:find-file-if-exist (rails-core:mailer-file controller-name)))
35
      (progn
36
        (goto-char (point-min))
37
        (when action-name
38
          (if (search-forward-regexp (concat "^[ ]*def[ ]*" action-name) nil t)
39
              (recenter))
40
          (message (format "%s: %s" (substring (symbol-name (rails-core:buffer-type)) 1) controller-name))))))
41
42
(defun rails-controller-layout:switch-to-view (controller-name action-name)
43
  "Open the ACTION-NAME file for CONTROLLER-NAME in the views directory."
44
  (when action-name
45
    (let ((views (rails-controller-layout:view-files controller-name action-name))
46
          (title (substring (symbol-name (rails-core:buffer-type)) 1)))
47
      (cond
48
       ((= (length views) 1)
49
        (find-file (first views))
50
        (message "%s: %s#%s" title controller-name action-name))
51
       ((= (length views) 0)
52
        (rails-controller-layout:create-view-for-action controller-name action-name))))))
53
54
(defun rails-controller-layout:toggle-action-view ()
55
  (interactive)
56
  (let ((controller-name (or (rails-core:current-controller) (rails-core:current-mailer)))
57
        (action-name (rails-core:current-action)))
58
    (case (rails-core:buffer-type)
59
      (:view
60
       (rails-controller-layout:switch-to-action-in-controller controller-name action-name))
61
      (:mailer
62
       (rails-controller-layout:switch-to-view controller-name action-name))
63
      (:controller
64
       (if action-name
65
           (rails-controller-layout:switch-to-view controller-name action-name)
66
        (if (rails-core:spec-exist-p) (rails-controller-layout:switch-to :rspec-controller)
67
           (rails-controller-layout:switch-to :functional-test)))))))
68
69
(defun rails-controller-layout:create-view-for-action (controller-name action-name)
70
  (let ((type
71
         (if rails-controller-layout:recent-template-type
72
             rails-controller-layout:recent-template-type
73
           (car rails-templates-list))))
74
    (setq type
75
          (completing-read (format "View for %s#%s not found, create %s.[%s]? "
76
                                   controller-name action-name action-name type)
77
                           rails-templates-list
78
                           nil nil type))
79
    (setq rails-controller-layout:recent-template-type type)
80
    (let ((file (rails-core:file (concat "app/views/"
81
                                         (replace-regexp-in-string "_controller" ""
82
                                                                   (rails-core:file-by-class controller-name t))))))
83
        (make-directory file t)
84
        (find-file (format "%s/%s.%s" file action-name type)))))
85
86
(defun rails-controller-layout:view-files (controller-name &optional action)
87
  "Retun a list containing the view file for CONTROLLER-NAME#ACTION.
88
If the action is nil, return all views for the controller."
89
  (rails-project:with-root
90
   (root)
91
   (let ((dir (rails-core:file
92
               (rails-core:views-dir
93
                (rails-core:short-controller-name controller-name)))))
94
     (if (file-directory-p dir)
95
         (directory-files dir t
96
          (if action
97
              (concat "^" action (rails-core:regex-for-match-view))
98
            (rails-core:regex-for-match-view)))))))
99
100
(defun rails-controller-layout:views-menu (controller-name)
101
  "Make menu of view for CONTROLLER-NAME."
102
  (let (menu)
103
    (setq menu
104
          (mapcar (lambda(i) (cons (file-name-nondirectory i) i))
105
                  (rails-controller-layout:view-files controller-name nil)))
106
    (when (zerop (length menu))
107
      (setq menu (list)))
108
    menu))
109
110
(defun rails-controller-layout:keymap (type)
111
  (let* ((name (capitalize (substring (symbol-name type) 1)))
112
         (map (make-sparse-keymap))
113
         (menu (make-sparse-keymap)))
114
    (when type
115
      (define-keys menu
116
        ([goto-migration]  '(menu-item "Go to Migration"
117
                                       rails-controller-layout:switch-to-migration
118
                                       :enable (and (not (rails-core:current-mailer))
119
                                                    (rails-core:migration-file-by-model
120
                                                     (singularize-string (rails-core:current-controller))))))
121
        ([goto-model]      '(menu-item "Go to Model"
122
                                       rails-controller-layout:switch-to-model
123
                                       :enable (and (not (rails-core:current-mailer))
124
                                                    (rails-core:model-exist-p
125
                                                     (singularize-string (rails-core:current-controller))))))
126
        ([goto-helper]     '(menu-item "Go to Helper"
127
                                       rails-controller-layout:switch-to-helper
128
                                       :enable (and (not (rails-core:current-mailer))
129
                                                    (not (eq (rails-core:buffer-type) :helper)))))
130
        ([goto-ftest]      '(menu-item "Go to Functional Test"
131
                                       rails-controller-layout:switch-to-functional-test
132
                                       :enable (and (not (rails-core:current-mailer))
133
                                                    (not (eq (rails-core:buffer-type) :functional-test)))))
134
        ([goto-rspec-controller] '(menu-item "Go to RSpec"
135
                                       rails-controller-layout:switch-to-rspec-controller
136
                                       :enable (not (eq (rails-core:buffer-type) :rspec-controller))))
137
        ([goto-controller] '(menu-item "Go to Controller"
138
                                       rails-controller-layout:switch-to-controller
139
                                       :enable (and (not (rails-core:current-mailer))
140
                                                    (not (eq (rails-core:buffer-type) :controller)))))
141
        ([goto-utest]      '(menu-item "Go to Unit Test"
142
                                       rails-controller-layout:switch-to-unit-test
143
                                       :enable (rails-core:current-mailer))))
144
      (define-keys map
145
        ((rails-key "g") 'rails-controller-layout:switch-to-migration)
146
        ((rails-key "m") 'rails-controller-layout:switch-to-model)
147
        ((rails-key "h") 'rails-controller-layout:switch-to-helper)
148
        ((rails-key "f") 'rails-controller-layout:switch-to-functional-test)
149
        ((rails-key "c") 'rails-controller-layout:switch-to-controller)
150
        ((rails-key "u") 'rails-controller-layout:switch-to-unit-test)
151
        ((rails-key "r") 'rails-controller-layout:switch-to-rspec-controller)
152
        ([menu-bar rails-controller-layout] (cons name menu))))
153
    map))
154
155
(defun rails-controller-layout:switch-to (type)
156
  (let* ((name (capitalize (substring (symbol-name type) 1)))
157
         (controller (rails-core:current-controller))
158
         (model (singularize-string controller))
159
         (mailer (rails-core:current-mailer))
160
         (item (case type
161
                 (:helper (rails-core:helper-file controller))
162
                 (:functional-test (rails-core:functional-test-file controller))
163
                 (:rspec-controller (rails-core:rspec-controller-file controller))
164
                 (:controller (rails-core:controller-file controller))
165
                 (:model (rails-core:model-file model))
166
                 (:unit-test (rails-core:unit-test-file mailer))
167
                 (:migration (rails-core:migration-file-by-model model)))))
168
    (if item
169
      (find-or-ask-to-create (format "%s does not exists do you want to create it? " item)
170
                             (rails-core:file item))
171
      (message "%s not found" name))))
172
173
(defun rails-controller-layout:switch-to-helper () (interactive) (rails-controller-layout:switch-to :helper))
174
(defun rails-controller-layout:switch-to-functional-test () (interactive) (rails-controller-layout:switch-to :functional-test))
175
(defun rails-controller-layout:switch-to-rspec-controller () (interactive) (rails-controller-layout:switch-to :rspec-controller))
176
(defun rails-controller-layout:switch-to-controller () (interactive) (rails-controller-layout:switch-to :controller))
177
(defun rails-controller-layout:switch-to-model () (interactive) (rails-controller-layout:switch-to :model))
178
(defun rails-controller-layout:switch-to-migration () (interactive) (rails-controller-layout:switch-to :migration))
179
(defun rails-controller-layout:switch-to-unit-test () (interactive) (rails-controller-layout:switch-to :unit-test))
180
181
(defun rails-controller-layout:menu ()
182
  (interactive)
183
  (let* ((type (rails-core:buffer-type))
184
         (title (capitalize (substring (symbol-name type) 1)))
185
         (controller (rails-core:current-controller))
186
         (action (rails-core:current-action))
187
         (model (singularize-string controller))
188
         (mailer (rails-core:current-mailer))
189
         (item (rails-controller-layout:views-menu (or controller mailer))))
190
    (add-to-list 'item (rails-core:menu-separator))
191
    (when controller
192
      (when (rails-core:model-exist-p model)
193
        (when (rails-core:migration-file-by-model model)
194
          (add-to-list 'item (cons "Migration" :migration)))
195
        (add-to-list 'item (cons "Model" :model)))
196
      (unless (eq type :helper)
197
        (add-to-list 'item (cons "Helper" :helper)))
198
      (unless (eq type :functional-test)
199
        (add-to-list 'item (cons "Functional Test" :functional-test)))
200
      (unless (eq type :rspec-controller)
201
        (add-to-list 'item (cons "RSpec" :rspec-controller)))
202
      (unless (eq type :controller)
203
        (add-to-list 'item (cons "Controller" :controller))))
204
    (when mailer
205
      (add-to-list 'item (cons "Unit Test" (rails-core:unit-test-file mailer)))
206
      (when (eq type :view)
207
        (add-to-list 'item (cons "Mailer" (rails-core:mailer-file mailer)))))
208
    (setq item
209
          (rails-core:menu
210
           (list (concat title " " controller
211
                         (when action (format " (%s)" action)))
212
                 (cons "Please select.."
213
                       item))))
214
    (typecase item
215
      (symbol (rails-controller-layout:switch-to item))
216
      (string (rails-core:find-file-if-exist item)))))
217
218
(provide 'rails-controller-layout)