1
;;; rails-model-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
(defun rails-model-layout:keymap (type)
30
  (let* ((name (capitalize (substring (symbol-name type) 1)))
31
         (map (make-sparse-keymap))
32
         (menu (make-sparse-keymap)))
33
    (when type
34
      (define-keys menu
35
        ([goto-model]      '(menu-item "Go to Model"
36
                                       rails-model-layout:switch-to-model
37
                                       :enable (and (not (eq (rails-core:buffer-type) :model))
38
                                                    (rails-core:model-exist-p (rails-core:current-model)))))
39
        ([goto-utest]      '(menu-item "Go to Unit Test"
40
                                       rails-model-layout:switch-to-unit-test
41
                                       :enable (and (not (eq (rails-core:buffer-type) :unit-test))
42
                                                    (rails-core:unit-test-exist-p (or (rails-core:current-model)
43
                                                                                      (rails-core:current-mailer))))))
44
        ([goto-rspec]      '(menu-item "Go to RSpec"
45
                                       rails-model-layout:switch-to-rspec-model
46
                                       :enable (and (not (eq (rails-core:buffer-type) :rspec-model))
47
                                                    (rails-core:rspec-model-exist-p (or (rails-core:current-model)
48
                                                                                      (rails-core:current-mailer))))))
49
        ([goto-migration]  '(menu-item "Go to Migration"
50
                                       rails-model-layout:switch-to-migration
51
                                       :enable (and (not (eq (rails-core:buffer-type) :migration))
52
                                                    (rails-core:migration-file-by-model (rails-core:current-model)))))
53
        ([goto-controller] '(menu-item "Go to Controller"
54
                                       rails-model-layout:switch-to-controller
55
                                       :enable (rails-core:controller-file-by-model (rails-core:current-model))))
56
        ([goto-fixture]    '(menu-item "Go to Fixture"
57
                                       rails-model-layout:switch-to-fixture
58
                                       :enable (and (not (eq (rails-core:buffer-type) :fixture))
59
                                                    (rails-core:fixture-exist-p (rails-core:current-model)))))
60
        ([goto-rspec-fixture]    '(menu-item "Go to RSpec Fixture"
61
                                       rails-model-layout:switch-to-rspec-fixture
62
                                       :enable (and (not (eq (rails-core:buffer-type) :rspec-fixture))
63
                                                    (rails-core:rspec-fixture-exist-p (rails-core:current-model)))))
64
        ([goto-mailer]     '(menu-item "Go to Mailer"
65
                                       rails-model-layout:switch-to-mailer
66
                                       :enable (rails-core:mailer-exist-p (rails-core:current-mailer)))))
67
      (define-keys map
68
        ((rails-key "m")         'rails-model-layout:switch-to-model)
69
        ((rails-key "u")         'rails-model-layout:switch-to-unit-test)
70
        ((rails-key "r")         'rails-model-layout:switch-to-rspec-model)
71
        ((rails-key "g")         'rails-model-layout:switch-to-migration)
72
        ((rails-key "c")         'rails-model-layout:switch-to-controller)
73
        ((rails-key "x")         'rails-model-layout:switch-to-fixture)
74
        ((rails-key "z")         'rails-model-layout:switch-to-rspec-fixture)
75
        ((rails-key "n")         'rails-model-layout:switch-to-mailer)
76
        ([menu-bar rails-model-layout] (cons name menu))))
77
    map))
78
79
(defun rails-model-layout:switch-to (type)
80
  (let* ((name (capitalize (substring (symbol-name type) 1)))
81
         (model (rails-core:current-model))
82
         (controller (rails-core:current-controller))
83
         (mailer (rails-core:current-mailer))
84
         (item (if controller controller model))
85
         (item (case type
86
                 (:mailer (rails-core:mailer-file mailer))
87
                 (:controller (rails-core:controller-file-by-model model))
88
                 (:fixture (rails-core:fixture-file model))
89
                 (:rspec-fixture (rails-core:rspec-fixture-file model))
90
                 (:unit-test (rails-core:unit-test-file item))
91
                 (:rspec-model (rails-core:rspec-model-file item))
92
                 (:model (rails-core:model-file model))
93
                 (:migration (rails-core:migration-file-by-model model)))))
94
    (if item
95
      (find-or-ask-to-create (format "%s does not exists do you want to create it? " item)
96
                             (rails-core:file item))
97
      (message "%s not found" name))))
98
99
(defun rails-model-layout:switch-to-mailer () (interactive) (rails-model-layout:switch-to :mailer))
100
(defun rails-model-layout:switch-to-controller () (interactive) (rails-model-layout:switch-to :controller))
101
(defun rails-model-layout:switch-to-fixture () (interactive) (rails-model-layout:switch-to :fixture))
102
(defun rails-model-layout:switch-to-rspec-fixture () (interactive) (rails-model-layout:switch-to :rspec-fixture))
103
(defun rails-model-layout:switch-to-unit-test () (interactive) (rails-model-layout:switch-to :unit-test))
104
(defun rails-model-layout:switch-to-rspec-model () (interactive) (rails-model-layout:switch-to :rspec-model))
105
(defun rails-model-layout:switch-to-model () (interactive) (rails-model-layout:switch-to :model))
106
(defun rails-model-layout:switch-to-migration () (interactive) (rails-model-layout:switch-to :migration))
107
108
(defun rails-model-layout:menu ()
109
  (interactive)
110
  (let* ((item (list))
111
         (type (rails-core:buffer-type))
112
         (title (capitalize (substring (symbol-name type) 1)))
113
         (model (rails-core:current-model))
114
         (controller (pluralize-string model))
115
         (mailer (rails-core:current-mailer)))
116
    (when model
117
      (when (and (not (eq type :migration))
118
                 (rails-core:migration-file-by-model model))
119
        (add-to-list 'item (cons "Migration" :migration)))
120
      (unless (eq type :fixture)
121
        (add-to-list 'item (cons "Fixture" :fixture)))
122
      (unless (eq type :rspec-fixture)
123
        (add-to-list 'item (cons "RSpec Fixture" :rspec-fixture)))
124
      (when (rails-core:controller-exist-p controller)
125
        (add-to-list 'item (cons "Controller" :controller)))
126
      (unless (eq type :unit-test)
127
        (add-to-list 'item (cons "Unit Test" :unit-test)))
128
      (unless (eq type :rspec-model)
129
        (add-to-list 'item (cons "RSpec" :rspec-model)))
130
      (unless (eq type :model)
131
        (add-to-list 'item (cons "Model" :model))))
132
    (when mailer
133
        (setq item (rails-controller-layout:views-menu model))
134
        (add-to-list 'item (rails-core:menu-separator))
135
        (add-to-list 'item (cons "Mailer" :mailer)))
136
    (when item
137
      (setq item
138
            (rails-core:menu
139
             (list (concat title " " model)
140
                   (cons "Please select.."
141
                         item))))
142
      (typecase item
143
        (symbol (rails-model-layout:switch-to item))
144
        (string (rails-core:find-file-if-exist item))))))
145
146
(provide 'rails-model-layout)