rails-model-layout.el
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: svn://rubyforge.org/var/svn/emacs-rails/trunk/rails-model-layout.el $ 9 ;; $Id: rails-model-layout.el 173 2007-04-09 15:15:02Z dimaexe $ 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-migration] '(menu-item "Go to Migration" 45 rails-model-layout:switch-to-migration 46 :enable (and (not (eq (rails-core:buffer-type) :migration)) 47 (rails-core:migration-file-by-model (rails-core:current-model))))) 48 ([goto-controller] '(menu-item "Go to Controller" 49 rails-model-layout:switch-to-controller 50 :enable (rails-core:controller-file-by-model (rails-core:current-model)))) 51 ([goto-fixture] '(menu-item "Go to Fixture" 52 rails-model-layout:switch-to-fixture 53 :enable (and (not (eq (rails-core:buffer-type) :fixture)) 54 (rails-core:fixture-exist-p (rails-core:current-model))))) 55 ([goto-mailer] '(menu-item "Go to Mailer" 56 rails-model-layout:switch-to-mailer 57 :enable (rails-core:mailer-exist-p (rails-core:current-mailer))))) 58 (define-keys map 59 ((rails-key "m") 'rails-model-layout:switch-to-model) 60 ((rails-key "u") 'rails-model-layout:switch-to-unit-test) 61 ((rails-key "g") 'rails-model-layout:switch-to-migration) 62 ((rails-key "c") 'rails-model-layout:switch-to-controller) 63 ((rails-key "x") 'rails-model-layout:switch-to-fixture) 64 ((rails-key "n") 'rails-model-layout:switch-to-mailer) 65 ([menu-bar rails-model-layout] (cons name menu)))) 66 map)) 67 68 (defun rails-model-layout:switch-to (type) 69 (let* ((name (capitalize (substring (symbol-name type) 1))) 70 (model (rails-core:current-model)) 71 (controller (rails-core:current-controller)) 72 (mailer (rails-core:current-mailer)) 73 (item (if controller controller model)) 74 (item (case type 75 (:mailer (rails-core:mailer-file mailer)) 76 (:controller (rails-core:controller-file-by-model model)) 77 (:fixture (rails-core:fixture-file model)) 78 (:unit-test (rails-core:unit-test-file item)) 79 (:model (rails-core:model-file model)) 80 (:migration (rails-core:migration-file-by-model model))))) 81 (if item 82 (let ((file (rails-core:file item))) 83 (if (file-exists-p file) 84 (progn 85 (find-file file) 86 (message (format "%s: %s" (substring (symbol-name type) 1) item))) 87 (message "File %s not exists" file))) 88 (message "%s not found" name)))) 89 90 (defun rails-model-layout:switch-to-mailer () (interactive) (rails-model-layout:switch-to :mailer)) 91 (defun rails-model-layout:switch-to-controller () (interactive) (rails-model-layout:switch-to :controller)) 92 (defun rails-model-layout:switch-to-fixture () (interactive) (rails-model-layout:switch-to :fixture)) 93 (defun rails-model-layout:switch-to-unit-test () (interactive) (rails-model-layout:switch-to :unit-test)) 94 (defun rails-model-layout:switch-to-model () (interactive) (rails-model-layout:switch-to :model)) 95 (defun rails-model-layout:switch-to-migration () (interactive) (rails-model-layout:switch-to :migration)) 96 97 (defun rails-model-layout:menu () 98 (interactive) 99 (let* ((item (list)) 100 (type (rails-core:buffer-type)) 101 (title (capitalize (substring (symbol-name type) 1))) 102 (model (rails-core:current-model)) 103 (controller (pluralize-string model)) 104 (mailer (rails-core:current-mailer))) 105 (when model 106 (when (and (not (eq type :migration)) 107 (rails-core:migration-file-by-model model)) 108 (add-to-list 'item (cons "Migration" :migration))) 109 (unless (eq type :fixture) 110 (add-to-list 'item (cons "Fixture" :fixture))) 111 (when (rails-core:controller-exist-p controller) 112 (add-to-list 'item (cons "Controller" :controller))) 113 (unless (eq type :unit-test) 114 (add-to-list 'item (cons "Unit Test" :unit-test))) 115 (unless (eq type :model) 116 (add-to-list 'item (cons "Model" :model)))) 117 (when mailer 118 (setq item (rails-controller-layout:views-menu model)) 119 (add-to-list 'item (rails-core:menu-separator)) 120 (add-to-list 'item (cons "Mailer" :mailer))) 121 (when item 122 (setq item 123 (rails-core:menu 124 (list (concat title " " model) 125 (cons "Please select.." 126 item)))) 127 (typecase item 128 (symbol (rails-model-layout:switch-to item)) 129 (string (rails-core:find-file-if-exist item)))))) 130 131 (provide 'rails-model-layout)