remvee / emacs-rails forked from tomtt/emacs-rails

Tomtt's version of the minor mode for editing RubyOnRails code in Emacs

emacs-rails / rails-core.el
d762cdec » dimaexe 2006-12-01 empty 1 ;;; rails-core.el --- core helper functions and macros for emacs-rails
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 2
5a95c515 » dimaexe 2007-03-24 update copyright 3 ;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 4
5a95c515 » dimaexe 2007-03-24 update copyright 5 ;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 6 ;; Rezikov Peter <crazypit13 (at) gmail.com>
7
8 ;; Keywords: ruby rails languages oop
9 ;; $URL$
10 ;; $Id$
11
12 ;;; License
13
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License
16 ;; as published by the Free Software Foundation; either version 2
17 ;; of the License, or (at your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; if not, write to the Free Software
26 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
5db8122a » dimaexe 2007-01-22 rails-core.el: add rails-co... 28 (eval-when-compile
29 (require 'rails-lib))
30
c427fa80 » vanicat 2008-09-26 adding some customization 31 (defcustom rails-core:class-dirs
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 32 '("app/controllers"
33 "app/views"
34 "app/models"
35 "app/helpers"
36 "test/unit"
37 "test/functional"
14802127 » remvee 2009-02-27 Modified emacs-rails for RS... 38 "spec/controllers"
39 "spec/fixtures"
40 "spec/lib"
41 "spec/models"
42 "lib")
c427fa80 » vanicat 2008-09-26 adding some customization 43 "Directories with Rails classes"
44 :group 'rails
45 :type '(repeat string))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 46
47 (defun rails-core:class-by-file (filename)
48 "Return the class associated with FILENAME.
14802127 » remvee 2009-02-27 Modified emacs-rails for RS... 49 <rails-root>/(app/models|app/controllers|app/helpers|test/unit|test/functional|lib|spec/controllers|spec/lib|spec/models)/foo/bar_baz
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 50 --> Foo::BarBaz"
51 (let* ((case-fold-search nil)
82cbcc6d » dimaexe 2006-12-03 rails-ruby.el (ruby-indent-... 52 (path (replace-regexp-in-string
53 (format
54 "\\(.*\\(%s\\)/\\)?\\([^\.]+\\)\\(.*\\)?"
55 (strings-join "\\|" rails-core:class-dirs)) "\\3" filename))
025d3a3e » dimaexe 2006-12-17 apply patches from Akira Ikeda 56 (path (replace-regexp-in-string "/" " " path))
82cbcc6d » dimaexe 2006-12-03 rails-ruby.el (ruby-indent-... 57 (path (replace-regexp-in-string "_" " " path)))
58 (replace-regexp-in-string
59 " " ""
025d3a3e » dimaexe 2006-12-17 apply patches from Akira Ikeda 60 (replace-regexp-in-string
61 " " "::"
98714ad5 » dimaexe 2007-04-19 rails-ruby.el (completion-d... 62 (if (string-match "^ *\\([0-9]+ *\\)?[A-Z]" path)
5999a88a » dimaexe 2007-04-11 predictive-prog-mode.el (ac... 63 path
64 (capitalize path))))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 65
66 (defun rails-core:file-by-class (classname &optional do-not-append-ext)
67 "Return the filename associated with CLASSNAME.
68 If the optional parameter DO-NOT-APPEND-EXT is set this function
69 will not append \".rb\" to result."
52c036d1 » dimaexe 2008-02-11 rails-snippets-feature.el (... 70 (concat (decamelize (replace-regexp-in-string "::" "/" classname))
71 (unless do-not-append-ext ".rb")))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 72
73 ;;;;;;;;;; Files ;;;;;;;;;;
74
75 (defun rails-core:file (file-name)
76 "Return the full path for FILE-NAME in a Rails directory."
2546c56e » dimaexe 2007-03-25 rails-scripts.el (rails-scr... 77 (when file-name
78 (if (file-name-absolute-p file-name)
79 file-name
c5770e75 » dimaexe 2007-03-29 rails-project.el: created 80 (rails-project:with-root
81 (root)
2546c56e » dimaexe 2007-03-25 rails-scripts.el (rails-scr... 82 (concat root file-name)))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 83
84 (defun rails-core:quoted-file (file-name)
85 "Return the quoted full path for FILE-NAME in a Rails directory."
86 (concat "\"" (rails-core:file file-name) "\""))
87
88 (defun rails-core:find-file (file-name)
89 "Open the file named FILE_NAME in a Rails directory."
90 (when-bind (file (rails-core:file file-name))
91 (find-file file)))
92
93 (defun rails-core:find-file-if-exist (file-name)
94 "Open the file named FILE-NAME in a Rails directory only if the file exists."
95 (let ((file-name (rails-core:file file-name)))
5edc1c20 » remvee 2009-06-09 allow nil to be passed to r... 96 (when (and file-name (file-exists-p file-name))
60a9d398 » dimaexe 2007-01-25 add rails-nav:goto-plugins 97 (find-file file-name))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 98
99 (defun rails-core:find-or-ask-to-create (question file)
100 "Open the file named FILE in a Rails directory if it exists. If
101 it does not exist, ask to create it using QUESTION as a prompt."
102 (find-or-ask-to-create question (rails-core:file file)))
103
c5ba131c » dimaexe 2007-11-02 rails-core.el (rails-core:m... 104 (defun rails-core:strip-namespace (class-name)
105 "Strip namespace of CLASS-NAME, eg Foo::Bar -> Bar."
106 (let ((name-list (split-string class-name "::")))
107 (car (last name-list))))
108
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 109 ;; Funtions, that retrun Rails objects full pathes
110
111 (defun rails-core:model-file (model-name)
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 112 "Return the model file from the model name."
9b553c01 » dimaexe 2007-04-03 rails-model-layout.el (rail... 113 (when model-name
c5ba131c » dimaexe 2007-11-02 rails-core.el (rails-core:m... 114 (let* ((stripped-model-file
115 (rails-core:file-by-class
116 (rails-core:strip-namespace model-name)))
117 (model-file
118 (rails-core:file-by-class model-name)))
119 (cond
120 ((file-exists-p
121 (rails-core:file (concat "app/models/" model-file)))
122 (concat "app/models/" model-file))
123 ((file-exists-p
124 (rails-core:file (concat "app/models/" stripped-model-file)))
31feec45 » dimaexe 2007-11-03 rails-core.el (rails-core:m... 125 (concat "app/models/" stripped-model-file))
126 (t nil)))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 127
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 128 (defun rails-core:model-exist-p (model-name)
c5ba131c » dimaexe 2007-11-02 rails-core.el (rails-core:m... 129 "Return t if model MODEL-NAME exist."
31feec45 » dimaexe 2007-11-03 rails-core.el (rails-core:m... 130 (let ((model-file (rails-core:model-file model-name)))
131 (when model-file
132 (and (file-exists-p (rails-core:file model-file))
133 (not (rails-core:observer-p model-name))
134 (not (rails-core:mailer-p model-name))))))
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 135
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 136 (defun rails-core:controller-file (controller-name)
137 "Return the path to the controller CONTROLLER-NAME."
9b553c01 » dimaexe 2007-04-03 rails-model-layout.el (rail... 138 (when controller-name
c6a7de02 » remvee 2009-05-28 could not find application ... 139 (let* ((basename (rails-core:file-by-class (rails-core:short-controller-name controller-name) t))
140 (exact (concat "app/controllers/" basename ".rb")))
141 (if (file-exists-p (rails-core:file exact))
142 exact
143 (concat "app/controllers/" basename "_controller.rb")))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 144
81e3f928 » dimaexe 2007-03-23 rails.el (rails-directory<-... 145 (defun rails-core:controller-exist-p (controller-name)
146 "Return t if controller CONTROLLER-NAME exist."
9b553c01 » dimaexe 2007-04-03 rails-model-layout.el (rail... 147 (when controller-name
148 (file-exists-p
149 (rails-core:file
150 (rails-core:controller-file controller-name)))))
81e3f928 » dimaexe 2007-03-23 rails.el (rails-directory<-... 151
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 152 (defun rails-core:controller-file-by-model (model)
153 (when model
52c036d1 » dimaexe 2008-02-11 rails-snippets-feature.el (... 154 (let* ((controller (pluralize-string model)))
155 ;(controller (when controller (capitalize controller))))
c5ba131c » dimaexe 2007-11-02 rails-core.el (rails-core:m... 156 (setq controller
157 (cond
158 ((rails-core:controller-exist-p controller) controller) ;; pluralized
1ff2c51b » dimaexe 2007-11-02 rails-core.el (rails-core:m... 159 ((rails-core:controller-exist-p model) model) ;; singularized
160 (t (let ((controllers (rails-core:controllers t)))
161 (cond
162 ;; with namespace
163 ((find
164 (list controller model)
165 controllers
166 :test #'(lambda(x y)
167 (or
168 (string= (car x) (rails-core:strip-namespace y))
169 (string= (cadr x) (rails-core:strip-namespace y)))))))))))
c5ba131c » dimaexe 2007-11-02 rails-core.el (rails-core:m... 170 (when controller
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 171 (rails-core:controller-file controller)))))
172
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 173 (defun rails-core:observer-file (observer-name)
174 "Return the path to the observer OBSERVER-NAME."
9b553c01 » dimaexe 2007-04-03 rails-model-layout.el (rail... 175 (when observer-name
176 (rails-core:model-file (concat observer-name "Observer"))))
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 177
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 178 (defun rails-core:mailer-file (mailer)
179 (when (and mailer
180 (rails-core:mailer-p mailer))
181 (rails-core:model-file mailer)))
182
183 (defun rails-core:mailer-exist-p (mailer)
184 (when mailer
185 (file-exists-p (rails-core:file (rails-core:mailer-file mailer)))))
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 186
280d7811 » dimaexe 2007-03-26 rails-model-layout.el (rail... 187 (defun rails-core:migration-file (migration-name)
188 "Return the model file from the MIGRATION-NAME."
9b553c01 » dimaexe 2007-04-03 rails-model-layout.el (rail... 189 (when migration-name
190 (let ((dir "db/migrate/")
191 (name (replace-regexp-in-string
192 " " "_"
193 (rails-core:file-by-class migration-name))))
194 (when (string-match "^[^0-9]+[^_]" name) ; try search when the name without migration number
195 (let ((files (directory-files (rails-core:file dir)
196 nil
197 (concat "[0-9]+_" name "$"))))
198 (setq name (if files
199 (car files)
200 nil))))
201 (when name
202 (concat dir name)))))
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 203
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 204 (defun rails-core:migration-file-by-model (model)
205 (when model
206 (rails-core:migration-file
207 (concat "Create" (rails-core:class-by-file (pluralize-string model))))))
208
209 (defun rails-core:model-by-migration-filename (migration-filename)
210 (when migration-filename
211 (let ((model-name (singularize-string
212 (string=~ "[0-9]+_create_\\(\\w+\\)\.rb" (buffer-name) $1))))
213 (when (and model-name
214 (rails-core:model-exist-p model-name))
215 model-name))))
216
154f696c » dimaexe 2007-08-14 rails-compat.el (try-comple... 217 (defun rails-core:configuration-file (file)
218 "Return the path to the configuration FILE."
219 (when file
220 (concat "config/" file)))
221
36f80f4c » dimaexe 2007-01-27 rails.el (rails-directory<-... 222 (defun rails-core:plugin-file (plugin file)
223 "Return the path to the FILE in Rails PLUGIN."
224 (concat "vendor/plugins/" plugin "/" file))
225
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 226 (defun rails-core:layout-file (layout)
227 "Return the path to the layout file named LAYOUT."
16882646 » dimaexe 2007-03-19 rails-core.el (rails-core:l... 228 (let ((its rails-templates-list)
229 filename)
230 (while (and (car its)
231 (not filename))
c5770e75 » dimaexe 2007-03-29 rails-project.el: created 232 (when (file-exists-p (format "%sapp/views/layouts/%s.%s" (rails-project:root) layout (car its)))
16882646 » dimaexe 2007-03-19 rails-core.el (rails-core:l... 233 (setq filename (format "app/views/layouts/%s.%s" layout (car its))))
234 (setq its (cdr its)))
235 filename))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 236
237 (defun rails-core:js-file (js)
238 "Return the path to the JavaScript file named JS."
239 (concat "public/javascripts/" js ".js"))
240
241 (defun rails-core:partial-name (name)
242 "Return the file name of partial NAME."
243 (if (string-match "/" name)
244 (concat "app/views/"
891d41bb » vanicat 2008-09-25 use .html.erb instead of .html 245 (replace-regexp-in-string "\\([^/]*\\)$" "_\\1.html.erb" name))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 246 (concat (rails-core:views-dir (rails-core:current-controller))
891d41bb » vanicat 2008-09-25 use .html.erb instead of .html 247 "_" name ".html.erb")))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 248
249 (defun rails-core:view-name (name)
250 "Return the file name of view NAME."
251 (concat (rails-core:views-dir (rails-core:current-controller))
891d41bb » vanicat 2008-09-25 use .html.erb instead of .html 252 name ".html.erb")) ;; BUG: will fix it
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 253
254 (defun rails-core:helper-file (controller)
255 "Return the helper file name for the controller named
256 CONTROLLER."
e182140b » dimaexe 2007-04-19 rails-speedbar-feature.el (... 257 (if (string= "Test/TestHelper" controller)
258 (rails-core:file (rails-core:file-by-class "Test/TestHelper"))
259 (when controller
260 (format "app/helpers/%s_helper.rb"
261 (replace-regexp-in-string "_controller" ""
262 (rails-core:file-by-class controller t))))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 263
264 (defun rails-core:functional-test-file (controller)
265 "Return the functional test file name for the controller named
266 CONTROLLER."
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 267 (when controller
268 (format "test/functional/%s_test.rb"
269 (rails-core:file-by-class (rails-core:long-controller-name controller) t))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 270
271 (defun rails-core:unit-test-file (model)
272 "Return the unit test file name for the model named MODEL."
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 273 (when model
274 (format "test/unit/%s_test.rb" (rails-core:file-by-class model t))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 275
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 276 (defun rails-core:unit-test-exist-p (model)
277 "Return the unit test file name for the model named MODEL."
278 (let ((test (rails-core:unit-test-file model)))
279 (when test
280 (file-exists-p (rails-core:file test)))))
281
81e3f928 » dimaexe 2007-03-23 rails.el (rails-directory<-... 282 (defun rails-core:fixture-file (model)
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 283 "Return the fixtures file name for the model named MODEL."
9b553c01 » dimaexe 2007-04-03 rails-model-layout.el (rail... 284 (when model
285 (format "test/fixtures/%s.yml" (pluralize-string (rails-core:file-by-class model t)))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 286
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 287 (defun rails-core:fixture-exist-p (model)
288 (when model
289 (file-exists-p
290 (rails-core:file (rails-core:fixture-file model)))))
291
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 292 (defun rails-core:views-dir (controller)
293 "Return the view directory name for the controller named CONTROLLER."
e182140b » dimaexe 2007-04-19 rails-speedbar-feature.el (... 294 (format "app/views/%s/" (replace-regexp-in-string "_controller" "" (rails-core:file-by-class controller t))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 295
296 (defun rails-core:stylesheet-name (name)
297 "Return the file name of the stylesheet named NAME."
298 (concat "public/stylesheets/" name ".css"))
299
25646fda » dimaexe 2007-03-24 rails-core.el (rails-core:a... 300 (defun rails-core:controller-name (controller-file)
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 301 "Return the class name of the controller named CONTROLLER.
302 Bar in Foo dir -> Foo::Bar"
303 (rails-core:class-by-file
25646fda » dimaexe 2007-03-24 rails-core.el (rails-core:a... 304 (if (eq (elt controller-file 0) 47) ;;; 47 == '/'
305 (subseq controller-file 1)
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 306 (let ((current-controller (rails-core:current-controller)))
307 (if (string-match ":" current-controller)
308 (concat (replace-regexp-in-string "[^:]*$" "" current-controller)
25646fda » dimaexe 2007-03-24 rails-core.el (rails-core:a... 309 controller-file)
310 controller-file)))))
311
312 (defun rails-core:short-controller-name (controller)
313 "Convert FooController -> Foo."
314 (remove-postfix controller "Controller" ))
315
316 (defun rails-core:long-controller-name (controller)
317 "Convert Foo/FooController -> FooController."
318 (if (string-match "Controller$" controller)
319 controller
320 (concat controller "Controller")))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 321
14802127 » remvee 2009-02-27 Modified emacs-rails for RS... 322 (defun rails-core:rspec-controller-file (controller)
323 "Return the controller spec file name for the controller named
324 CONTROLLER."
325 (when controller
326 (format "spec/controllers/%s_spec.rb"
327 (rails-core:file-by-class (rails-core:long-controller-name controller) t))))
328
329 (defun rails-core:lib-file (lib-name)
330 "Return the model file from the lib name."
331 (when lib-name
332 (concat "lib/" (rails-core:file-by-class lib-name))))
333
334 (defun rails-core:rspec-lib-file (lib)
335 "Return the lib spec file name for the lib named LIB."
336 (when lib
337 (format "spec/lib/%s_spec.rb" (rails-core:file-by-class lib t))))
338
339 (defun rails-core:rspec-model-file (model)
340 "Return the model spec file name for the model named MODEL."
341 (when model
342 (format "spec/models/%s_spec.rb" (rails-core:file-by-class model t))))
343
344 (defun rails-core:rspec-fixture-file (model)
345 "Return the rspec fixtures file name for the model named MODEL."
346 (when model
347 (format "spec/fixtures/%s.yml" (pluralize-string (rails-core:file-by-class model t)))))
348
349 (defun rails-core:rspec-lib-exist-p (lib)
350 "Return the lib spec file name for the model named MODEL."
351 (let ((spec (rails-core:rspec-lib-file lib)))
352 (when spec
353 (file-exists-p (rails-core:file spec)))))
354
355 (defun rails-core:rspec-model-exist-p (model)
356 "Return the model spec file name for the model named MODEL."
357 (let ((spec (rails-core:rspec-model-file model)))
358 (when spec
359 (file-exists-p (rails-core:file spec)))))
360
361 (defun rails-core:rspec-fixture-exist-p (model)
362 (when model
363 (file-exists-p
364 (rails-core:file (rails-core:rspec-fixture-file model)))))
365
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 366 ;;;;;;;;;; Functions that return collection of Rails objects ;;;;;;;;;;
c41e46fa » dimaexe 2007-01-24 add concerns 367 (defun rails-core:observer-p (name)
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 368 (when name
4b6fba16 » dimaexe 2008-02-15 rails-core.el (rails-core:o... 369 (if (string-match "\\(Observer\\|_observer\\)\\(\\.rb\\)?$" name)
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 370 t nil)))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 371
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 372 (defun rails-core:mailer-p (name)
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 373 (when name
4b6fba16 » dimaexe 2008-02-15 rails-core.el (rails-core:o... 374 (if (string-match "\\(Mailer\\|Notifier\\|_mailer\\|_notifier\\)\\(\\.rb\\)?$" name)
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 375 t nil)))
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 376
4b6fba16 » dimaexe 2008-02-15 rails-core.el (rails-core:o... 377
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 378 (defun rails-core:controllers (&optional cut-contoller-suffix)
379 "Return a list of Rails controllers. Remove the '_controller'
380 suffix if CUT-CONTOLLER-SUFFIX is non nil."
381 (mapcar
c41e46fa » dimaexe 2007-01-24 add concerns 382 #'(lambda (controller)
383 (rails-core:class-by-file
384 (if cut-contoller-suffix
385 (replace-regexp-in-string "_controller\\." "." controller)
386 controller)))
387 (delete-if-not
388 #'(lambda (controller)
389 (string-match "\\(application\\|[a-z0-9_]+_controller\\)\\.rb$"
390 controller))
391 (find-recursive-files "\\.rb$" (rails-core:file "app/controllers/")))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 392
1c148c9d » dimaexe 2007-03-29 rails-test.el (rails-test:p... 393 (defun rails-core:functional-tests ()
394 "Return a list of Rails functional tests."
395 (mapcar
396 #'(lambda(it)
397 (remove-postfix (rails-core:class-by-file it)
398 "ControllerTest"))
399 (find-recursive-files "\\.rb$" (rails-core:file "test/functional/"))))
400
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 401 (defun rails-core:models ()
402 "Return a list of Rails models."
1eb6ccb4 » dimaexe 2007-01-26 rails-for-view.el (rails-fo... 403 (mapcar
404 #'rails-core:class-by-file
405 (delete-if
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 406 #'(lambda (file) (or (rails-core:observer-p file)
407 (rails-core:mailer-p file)))
c41e46fa » dimaexe 2007-01-24 add concerns 408 (find-recursive-files "\\.rb$" (rails-core:file "app/models/")))))
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 409
1c148c9d » dimaexe 2007-03-29 rails-test.el (rails-test:p... 410 (defun rails-core:unit-tests ()
411 "Return a list of Rails functional tests."
412 (mapcar
413 #'(lambda(it)
414 (remove-postfix (rails-core:class-by-file it)
415 "Test"))
416 (find-recursive-files "\\.rb$" (rails-core:file "test/unit/"))))
417
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 418 (defun rails-core:observers ()
419 "Return a list of Rails observers."
420 (mapcar
421 #'(lambda (observer) (replace-regexp-in-string "Observer$" "" observer))
422 (mapcar
423 #'rails-core:class-by-file
424 (find-recursive-files "\\(_observer\\)\\.rb$" (rails-core:file "app/models/")))))
425
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 426 (defun rails-core:mailers ()
427 "Return a list of Rails mailers."
428 (mapcar
429 #'rails-core:class-by-file
430 (find-recursive-files "\\(_mailer\\|_notifier\\)\\.rb$" (rails-core:file "app/models/"))))
431
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 432 (defun rails-core:helpers ()
433 "Return a list of Rails helpers."
e182140b » dimaexe 2007-04-19 rails-speedbar-feature.el (... 434 (append
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 435 (mapcar
e182140b » dimaexe 2007-04-19 rails-speedbar-feature.el (... 436 #'(lambda (helper) (replace-regexp-in-string "Helper$" "" helper))
437 (mapcar
438 #'rails-core:class-by-file
439 (find-recursive-files "_helper\\.rb$" (rails-core:file "app/helpers/"))))
440 (list "Test/TestHelper")))
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 441
2c571b54 » dimaexe 2007-03-26 rails-ui.el: updated 442 (defun rails-core:migrations (&optional strip-numbers)
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 443 "Return a list of Rails migrations."
2c571b54 » dimaexe 2007-03-26 rails-ui.el: updated 444 (let (migrations)
445 (setq
446 migrations
447 (reverse
448 (mapcar
449 #'(lambda (migration)
450 (replace-regexp-in-string "^\\([0-9]+\\)" "\\1 " migration))
451 (mapcar
452 #'rails-core:class-by-file
453 (find-recursive-files "^[0-9]+_.*\\.rb$" (rails-core:file "db/migrate/"))))))
454 (if strip-numbers
455 (mapcar #'(lambda(i) (car (last (split-string i " "))))
456 migrations)
457 migrations)))
458
81c6e435 » dimaexe 2007-03-31 rails-ui.el (rails-minor-mo... 459 (defun rails-core:migration-versions (&optional with-zero)
460 "Return a list of migtaion versions as the list of strings. If
461 second argument WITH-ZERO is present, append the \"000\" version
462 of migration."
463 (let ((ver (mapcar
464 #'(lambda(it) (car (split-string it " ")))
465 (rails-core:migrations))))
466 (if with-zero
467 (append ver '("000"))
468 ver)))
cbc2a30e » dimaexe 2007-01-23 rails-ui.el: create key bin... 469
470 (defun rails-core:plugins ()
471 "Return a list of Rails plugins."
472 (mapcar
473 #'file-name-nondirectory
474 (delete-if-not
475 #'file-directory-p
ea1676f8 » dimaexe 2007-01-26 rails-navigation.el (rails-... 476 (directory-files (rails-core:file "vendor/plugins") t "^[^\\.]"))))
477
36f80f4c » dimaexe 2007-01-27 rails.el (rails-directory<-... 478 (defun rails-core:plugin-files (plugin)
479 "Return a list of files in specific Rails plugin."
480 (find-recursive-files "^[^.]" (rails-core:file (concat "vendor/plugins/" plugin))))
481
ea1676f8 » dimaexe 2007-01-26 rails-navigation.el (rails-... 482 (defun rails-core:layouts ()
483 "Return a list of Rails layouts."
484 (mapcar
485 #'(lambda (l)
486 (replace-regexp-in-string "\\.[^.]+$" "" l))
16882646 » dimaexe 2007-03-19 rails-core.el (rails-core:l... 487 (find-recursive-files (rails-core:regex-for-match-view) (rails-core:file "app/views/layouts"))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 488
32f5ca58 » dimaexe 2007-03-23 rails-navigation.el (rails-... 489 (defun rails-core:fixtures ()
490 "Return a list of Rails fixtures."
491 (mapcar
492 #'(lambda (l)
493 (replace-regexp-in-string "\\.[^.]+$" "" l))
494 (find-recursive-files "\\.yml$" (rails-core:file "test/fixtures/"))))
495
154f696c » dimaexe 2007-08-14 rails-compat.el (try-comple... 496 (defun rails-core:configuration-files ()
497 "Return a files of files from config folder."
498 (find-recursive-files nil (rails-core:file "config/")))
499
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 500 (defun rails-core:regex-for-match-view ()
501 "Return a regex to match Rails view templates.
25646fda » dimaexe 2007-03-24 rails-core.el (rails-core:a... 502 The file extensions used for views are defined in `rails-templates-list'."
503 (format "\\.\\(%s\\)$" (strings-join "\\|" rails-templates-list)))
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 504
505 (defun rails-core:get-view-files (controller-class &optional action)
506 "Retun a list containing the view file for CONTROLLER-CLASS#ACTION.
507 If the action is nil, return all views for the controller."
c5770e75 » dimaexe 2007-03-29 rails-project.el: created 508 (rails-project:with-root
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 509 (root)
510 (directory-files
511 (rails-core:file
512 (rails-core:views-dir
513 (rails-core:short-controller-name controller-class))) t
514 (if action
515 (concat "^" action (rails-core:regex-for-match-view))
516 (rails-core:regex-for-match-view)))))
517
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 518 (defun rails-core:extract-ancestors (classes)
519 "Return the parent classes from a list of classes named CLASSES."
520 (delete ""
521 (uniq-list
522 (mapcar (lambda (class)
523 (replace-regexp-in-string
524 "::[^:]*$" "::"
525 (replace-regexp-in-string "^[^:]*$" "" class)))
526 classes))))
527
528 (defun rails-core:models-ancestors ()
529 "Return the parent classes of models."
530 (rails-core:extract-ancestors (rails-core:models)))
531
532 (defun rails-core:controllers-ancestors ()
533 "Return the parent classes of controllers."
534 (rails-core:extract-ancestors (rails-core:controllers)))
535
14802127 » remvee 2009-02-27 Modified emacs-rails for RS... 536 (defun rails-core:rspec-controllers ()
537 "Return a list of Rails controller specs."
538 (mapcar
539 #'(lambda(it)
540 (remove-postfix (rails-core:class-by-file it)
541 "Spec"))
542 (find-recursive-files "\\.rb$" (rails-core:file "spec/controllers/"))))
543
544 (defun rails-core:rspec-models ()
545 "Return a list of Rails model specs."
546 (mapcar
547 #'(lambda(it)
548 (remove-postfix (rails-core:class-by-file it)
549 "Spec"))
550 (find-recursive-files "\\.rb$" (rails-core:file "spec/models/"))))
551
552 (defun rails-core:rspec-fixtures ()
553 "Return a list of Rails RSpec fixtures."
554 (mapcar
555 #'(lambda (l)
556 (replace-regexp-in-string "\\.[^.]+$" "" l))
557 (find-recursive-files "\\.yml$" (rails-core:file "spec/fixtures/"))))
558
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 559 ;;;;;;;;;; Getting Controllers/Model/Action from current buffer ;;;;;;;;;;
560
561 (defun rails-core:current-controller ()
562 "Return the current Rails controller."
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 563 (let* ((file-class (rails-core:class-by-file (buffer-file-name))))
564 (unless (rails-core:mailer-p file-class)
565 (case (rails-core:buffer-type)
566 (:controller (rails-core:short-controller-name file-class))
567 (:view (rails-core:class-by-file
568 (directory-file-name (directory-of-file (buffer-file-name)))))
569 (:helper (remove-postfix file-class "Helper"))
14802127 » remvee 2009-02-27 Modified emacs-rails for RS... 570 (:functional-test (remove-postfix file-class "ControllerTest"))
571 (:rspec-controller (remove-postfix file-class "Spec"))))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 572
573 (defun rails-core:current-model ()
574 "Return the current Rails model."
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 575 (let* ((file-class (rails-core:class-by-file (buffer-file-name))))
576 (unless (rails-core:mailer-p file-class)
577 (case (rails-core:buffer-type)
578 (:migration (rails-core:model-by-migration-filename (buffer-name)))
579 (:model file-class)
580 (:unit-test (remove-postfix file-class "Test"))
14802127 » remvee 2009-02-27 Modified emacs-rails for RS... 581 (:fixture (singularize-string file-class))
582 (:rspec-fixture (singularize-string file-class))
583 (:rspec-model (remove-postfix file-class "Spec"))))))
584
585 (defun rails-core:current-lib ()
586 "Return the current lib."
587 (let* ((file-class (rails-core:class-by-file (buffer-file-name))))
588 (unless (rails-core:mailer-p file-class)
589 (case (rails-core:buffer-type)
590 (:lib file-class)
591 (:rspec-lib (remove-postfix file-class "Spec"))))))
017d1ca6 » dimaexe 2007-04-03 rails-controller-layout.el ... 592
593 (defun rails-core:current-mailer ()
594 "Return the current Rails Mailer, else return nil."
595 (let* ((file-class (rails-core:class-by-file (buffer-file-name)))
596 (test (remove-postfix file-class "Test")))
597 (when (or (rails-core:mailer-p file-class)
598 (rails-core:mailer-p test))
599 (case (rails-core:buffer-type)
600 (:mailer file-class)
601 (:unit-test test)
602 (:view (rails-core:class-by-file
603 (directory-file-name (directory-of-file (buffer-file-name)))))))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 604
605 (defun rails-core:current-action ()
606 "Return the current action in the current Rails controller."
607 (case (rails-core:buffer-type)
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 608 (:controller (rails-core:current-method-name))
609 (:mailer (rails-core:current-method-name))
a2af2b58 » dimaexe 2007-01-13 apply patch from Ronaldo 610 (:view (string-match "/\\([a-z0-9_]+\\)\.[a-z]+$" (buffer-file-name))
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 611 (match-string 1 (buffer-file-name)))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 612
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 613 (defun rails-core:current-helper ()
614 "Return the current helper"
496a9be5 » dimaexe 2007-01-28 *.el: fix bytecompile warni... 615 (rails-core:current-controller))
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 616
36f80f4c » dimaexe 2007-01-27 rails.el (rails-directory<-... 617 (defun rails-core:current-plugin ()
618 "Return the current plugin name."
619 (let ((name (buffer-file-name)))
620 (when (string-match "vendor\\/plugins\\/\\([^\\/]+\\)" name)
621 (match-string 1 name))))
622
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 623 (defun rails-core:current-method-name ()
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 624 (save-excursion
625 (when (search-backward-regexp "^[ ]*def \\([a-z0-9_]+\\)" nil t)
fe1304c7 » dimaexe 2007-03-27 rails-rake.el (rails-rake:t... 626 (match-string-no-properties 1))))
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 627
0ad15ce2 » crazycode 2009-06-17 merge migration change from... 628 (defun rails-core:current-migration-version ()
629 "Return the current migration version"
630 (let ((name (buffer-file-name)))
631 (when (string-match "db\\/migrate\\/\\([0-9]+\\)[a-z0-9_]+\.[a-z]+$" name)
632 (match-string 1 name))))
633
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 634 ;;;;;;;;;; Determination of buffer type ;;;;;;;;;;
635
636 (defun rails-core:buffer-file-match (regexp)
637 "Match the current buffer file name to RAILS_ROOT + REGEXP."
52c7ab79 » dimaexe 2007-04-03 rails-model-layout.el (rail... 638 (when-bind (file (rails-core:file regexp))
639 (string-match file
640 (buffer-file-name (current-buffer)))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 641
642 (defun rails-core:buffer-type ()
643 "Return the type of the current Rails file or nil if the type
644 cannot be determinated."
6622b059 » dimaexe 2007-03-12 ask to save modified buffer... 645 (loop for (type dir func) in rails-directory<-->types
646 when (and (rails-core:buffer-file-match dir)
54c0d229 » dimaexe 2007-03-23 rails-view-minor-mode.el (r... 647 (if func
648 (apply func (list (buffer-file-name (current-buffer))))
649 t))
a2af2b58 » dimaexe 2007-01-13 apply patch from Ronaldo 650 do (return type)))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 651
6622b059 » dimaexe 2007-03-12 ask to save modified buffer... 652
0a1f9824 » dimaexe 2007-03-25 rails-ws.el: refactored 653 ;;;;;;;;;; Rails minor mode Buttons ;;;;;;;;;;
654
655 (define-button-type 'rails-button
656 'follow-link t
657 'action #'rails-core:button-action)
658
659 (defun rails-core:button-action (button)
660 (let* ((file-name (button-get button :rails:file-name))
661 (line-number (button-get button :rails:line-number))
662 (file (rails-core:file file-name)))
663 (when (and file
664 (file-exists-p file))
665 (find-file-other-window file)
666 (when line-number
667 (goto-line line-number)))))
668
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 669 ;;;;;;;;;; Rails minor mode logs ;;;;;;;;;;
670
671 (defun rails-log-add (message)
672 "Add MESSAGE to the Rails minor mode log in RAILS_ROOT."
c5770e75 » dimaexe 2007-03-29 rails-project.el: created 673 (rails-project:with-root
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 674 (root)
675 (append-string-to-file (rails-core:file "log/rails-minor-mode.log")
676 (format "%s: %s\n"
677 (format-time-string "%Y/%m/%d %H:%M:%S") message))))
678
679 (defun rails-logged-shell-command (command buffer)
680 "Execute a shell command in the buffer and write the results to
681 the Rails minor mode log."
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 682 (shell-command (format "%s %s" rails-ruby-command command) buffer)
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 683 (rails-log-add
c5770e75 » dimaexe 2007-03-29 rails-project.el: created 684 (format "\n%s> %s\n%s" (rails-project:name)
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 685 command (buffer-string-by-name buffer))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 686
687 ;;;;;;;;;; Rails menu ;;;;;;;;;;
688
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 689 (defun rails-core:menu-separator ()
690 (unless (rails-use-text-menu) 'menu (list "--" "--")))
691
c7591eca » dimaexe 2007-04-03 rails-core.el: using comple... 692 (if (fboundp 'completion-posn-at-point-as-event)
693 (defun rails-core:menu-position ()
694 (completion-posn-at-point-as-event nil nil nil (+ (frame-char-height) 2)))
695 (defun rails-core:menu-position ()
696 (list '(300 50) (get-buffer-window (current-buffer)))))
141b75ff » dimaexe 2007-03-23 rails.el (ruby-mode-hook): ... 697
0ff0e180 » Joost Diepenmaat 2008-09-11 fixes for text-only menus 698 ;; fixup emacs-rails menu specs to work with tmm-prompt
699 (defun rails-core:tmm-menu (menu)
700 (symbol-name (tmm-prompt (cons (car menu)
701 (mapcar (lambda (pane)
702 (cons (car pane)
703 (mapcar (lambda (item)
704 (if (symbolp (cdr item))
705 item
706 (cons (car item)
707 (intern (cdr item)))))
708 (cdr pane))))
709 (cdr menu))))))
710
df355c19 » remvee 2009-06-03 default to ido-menu before tmm 711 (defun rails-core:ido-menu (menu)
145c0c05 » remvee 2009-06-08 use proper prompt for ido-menu 712 (let* ((prompt (car (car (cdr menu))))
df355c19 » remvee 2009-06-03 default to ido-menu before tmm 713 (mappings (cdr (car (cdr menu))))
714 (choices (delete-if #'not (mapcar (lambda (item) (car item)) mappings)))
715 (result (ido-completing-read prompt choices)))
716 (or (cdr (assoc result mappings)) result)))
717
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 718 (defun rails-core:menu (menu)
719 "Show a menu."
720 (let ((result
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 721 (if (rails-use-text-menu)
df355c19 » remvee 2009-06-03 default to ido-menu before tmm 722 (funcall (or rails-text-menu-function
723 (and (boundp 'ido-mode) ido-mode #'rails-core:ido-menu)
724 #'rails-core:tmm-menu) menu)
0f908be4 » dimaexe 2007-03-28 rails.el: added forward dec... 725 (x-popup-menu (rails-core:menu-position)
df355c19 » remvee 2009-06-03 default to ido-menu before tmm 726 (rails-core:prepare-menu menu)))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 727 (if (listp result)
728 (first result)
729 result)))
730
e4901f47 » dimaexe 2007-04-20 rails-core.el (rails-core:m... 731 (defvar rails-core:menu-letters-list
732 (let ((res '()))
733 (loop for i from (string-to-char "1") upto (string-to-char "9")
734 do (add-to-list 'res (char-to-string i) t))
735 (loop for i from (string-to-char "a") upto (string-to-char "z")
736 do (add-to-list 'res (char-to-string i) t))
737 res)
738 "List contains 0-9a-z letter")
739
740 (defun rails-core:prepare-menu (menu)
741 "Append a prefix to each label of menu-item from MENU."
742 (let ((title (car menu))
743 (menu (cdr menu))
744 (result '())
745 (result-line '())
746 (letter 0))
747 (dolist (line menu)
748 (setq result-line '())
749 (dolist (it line)
750 (typecase it
751 (cons
752 (if (and (string= (car (rails-core:menu-separator)) (car it))
753 (string= (cadr (rails-core:menu-separator)) (cadr it)))
754 (add-to-list 'result-line it t)
755 (progn
8f168488 » dimaexe 2007-08-04 rails-navigation.el (rails-... 756 (add-to-list 'result-line (cons
757 (format "%s) %s"
758 (nth letter rails-core:menu-letters-list)
759 (car it))
760 (cdr it))
761 t)
e4901f47 » dimaexe 2007-04-20 rails-core.el (rails-core:m... 762 (setq letter (+ 1 letter)))))
763 (t
764 (add-to-list 'result-line it t))))
765 (add-to-list 'result result-line t))
766 (cons title result)))
767
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 768 ;;;;;;;;;; Misc ;;;;;;;;;;
769
770 (defun rails-core:erb-block-string ()
771 "Return the contents of the current ERb block."
772 (save-excursion
773 (save-match-data
774 (let ((start (point)))
c1ba07d0 » dimaexe 2007-01-21 rename rails-for-rhtml to r... 775 (search-backward-regexp "<%[=]?")
776 (let ((from (match-end 0)))
777 (search-forward "%>")
778 (let ((to (match-beginning 0)))
779 (when (>= to start)
780 (buffer-substring-no-properties from to))))))))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 781
782 (defun rails-core:rhtml-buffer-p ()
783 "Return non nil if the current buffer is rhtml file."
891d41bb » vanicat 2008-09-25 use .html.erb instead of .html 784 (string-match "\\.rhtml\\|\\.html\\.erb$" (buffer-file-name)))
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 785
14802127 » remvee 2009-02-27 Modified emacs-rails for RS... 786 (defun rails-core:spec-exist-p ()
787 "Return non nil if spec directory is exist."
788 (file-exists-p (rails-core:file "spec")))
789
11ce6856 » dimaexe 2006-12-01 apply patches from ronaldo@... 790 (provide 'rails-core)