forked from tomtt/emacs-rails
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrails-core.el
955 lines (820 loc) · 35 KB
/
rails-core.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
;;; rails-core.el --- core helper functions and macros for emacs-rails
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
;; Rezikov Peter <crazypit13 (at) gmail.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.
(eval-when-compile
(require 'rails-lib))
(defcustom rails-core:class-dirs
'("app/controllers"
"app/views"
"app/models"
"app/decorators"
"app/helpers"
"app/mailers"
"app/assets"
"app/concepts"
"test/fixtures"
"test/controllers"
"test/mailers"
"test/integration"
"test/models"
"test/decorators"
"test/concepts"
"test/system"
"lib")
"Directories with Rails classes"
:group 'rails
:type '(repeat string))
(defun rails-core:class-by-file (filename)
"Return the class associated with FILENAME.
<rails-root>/(app/decorators|app/assets|app/mailers|app/models|app/controllers|app/helpers|test/models|test/decorators|test/controllers|test/mailers|test/integration|lib|test/concepts|test/system)/foo/bar_baz
--> Foo::BarBaz"
(let* ((case-fold-search nil)
(path (replace-regexp-in-string
(format
"\\(.*\\(%s\\)/\\)?\\([^\.]+\\)\\(.*\\)?"
(strings-join "\\|" rails-core:class-dirs)) "\\3" filename))
(path (replace-regexp-in-string "/" " " path))
(path (replace-regexp-in-string "_" " " path)))
(replace-regexp-in-string
" " ""
(replace-regexp-in-string
" " "::"
(if (string-match "^ *\\([0-9]+ *\\)?[A-Z]" path)
path
(capitalize path))))))
(defun rails-core:file-by-class (classname &optional do-not-append-ext)
"Return the filename associated with CLASSNAME.
If the optional parameter DO-NOT-APPEND-EXT is set this function
will not append \".rb\" to result."
(concat (decamelize (replace-regexp-in-string "::" "/" classname))
(unless do-not-append-ext ".rb")))
;;;;;;;;;; Files ;;;;;;;;;;
(defun rails-core:file (file-name)
"Return the full path for FILE-NAME in a Rails directory."
(when file-name
(if (file-name-absolute-p file-name)
file-name
(rails-project:with-root
(root)
(concat root file-name)))))
(defun rails-core:quoted-file (file-name)
"Return the quoted full path for FILE-NAME in a Rails directory."
(concat "\"" (rails-core:file file-name) "\""))
(defun rails-core:find-file (file-name)
"Open the file named FILE_NAME in a Rails directory."
(when-bind (file (rails-core:file file-name))
(find-file file)))
(defun rails-core:find-file-if-exist (file-name)
"Open the file named FILE-NAME in a Rails directory only if the file exists."
(let ((file-name (rails-core:file file-name)))
(when (and file-name (file-exists-p file-name))
(find-file file-name))))
(defun rails-core:find-or-ask-to-create (question file)
"Open the file named FILE in a Rails directory if it exists. If
it does not exist, ask to create it using QUESTION as a prompt."
(find-or-ask-to-create question (rails-core:file file)))
(defun rails-core:strip-namespace (class-name)
"Strip namespace of CLASS-NAME, eg Foo::Bar -> Bar."
(let ((name-list (split-string class-name "::")))
(car (last name-list))))
;; Funtions, that retrun Rails objects full pathes
(defun rails-core:model-file (model-name)
"Return the model file from the model name."
(when model-name
(let* ((stripped-model-file
(rails-core:file-by-class
(rails-core:strip-namespace model-name)))
(model-file
(rails-core:file-by-class model-name)))
(cond
((file-exists-p
(rails-core:file (concat "app/models/" model-file)))
(concat "app/models/" model-file))
((file-exists-p
(rails-core:file (concat "app/models/" stripped-model-file)))
(concat "app/models/" stripped-model-file))
(t
(concat "app/models/" model-file))))))
(defun rails-core:model-exist-p (model-name)
"Return t if model MODEL-NAME exist."
(let ((model-file (rails-core:model-file model-name)))
(when model-file
(and (file-exists-p (rails-core:file model-file))
(not (rails-core:observer-p model-name))
(not (rails-core:mailer-p model-name))))))
(defun rails-core:controller-file (controller-name)
"Return the path to the controller CONTROLLER-NAME."
(when controller-name
(let* ((basename (rails-core:file-by-class (rails-core:short-controller-name controller-name) t))
(exact (concat "app/controllers/" basename ".rb")))
(if (file-exists-p (rails-core:file exact))
exact
(concat "app/controllers/" basename "_controller.rb")))))
(defun rails-core:controller-exist-p (controller-name)
"Return t if controller CONTROLLER-NAME exist."
(when controller-name
(file-exists-p
(rails-core:file
(rails-core:controller-file controller-name)))))
(defun rails-core:decorator-file (decorator-name)
"Return the path to the decorator DECORATOR-NAME."
(when decorator-name
(let* ((basename (rails-core:file-by-class (rails-core:short-decorator-name decorator-name) t))
(exact (concat "app/decorators/" basename ".rb")))
(if (file-exists-p (rails-core:file exact))
exact
(concat "app/decorators/" basename "_decorator.rb")))))
(defun rails-core:decorator-exist-p (decorator-name)
"Return t if decorator DECORATOR-NAME exist."
(when decorator-name
(file-exists-p
(rails-core:file
(rails-core:decorator-file decorator-name)))))
(defun rails-core:controller-file-by-model (model)
(when model
(let* ((controller (pluralize-string model)))
;(controller (when controller (capitalize controller))))
(setq controller
(cond
((rails-core:controller-exist-p controller) controller) ;; pluralized
((rails-core:controller-exist-p model) model) ;; singularized
(t (let ((controllers (rails-core:controllers t)))
(cond
;; with namespace
((find
(list controller model)
controllers
:test #'(lambda(x y)
(or
(string= (car x) (rails-core:strip-namespace y))
(string= (cadr x) (rails-core:strip-namespace y)))))))))))
(when controller
(rails-core:controller-file controller)))))
(defun rails-core:decorator-file-by-model (model)
(when model
(rails-core:decorator-file (rails-core:long-decorator-name (rails-core:strip-namespace model)))))
(defun rails-core:observer-file (observer-name)
"Return the path to the observer OBSERVER-NAME."
(when observer-name
(rails-core:model-file (concat observer-name "Observer"))))
(defun rails-core:mailer-file (mailer)
(when (and mailer
(rails-core:mailer-p mailer))
(rails-core:model-file mailer)))
(defun rails-core:mailer-exist-p (mailer)
(when mailer
(file-exists-p (rails-core:file (rails-core:mailer-file mailer)))))
(defun rails-core:migration-file (migration-name)
"Return the model file from the MIGRATION-NAME."
(when migration-name
(let ((dir "db/migrate/")
(name (replace-regexp-in-string
" " "_"
(rails-core:file-by-class migration-name))))
(when (string-match "^[^0-9]+[^_]" name) ; try search when the name without migration number
(let ((files (directory-files (rails-core:file dir)
nil
(concat "[0-9]+_" name "$"))))
(setq name (if files
(car files)
nil))))
(when name
(concat dir name)))))
(defun rails-core:migration-file-by-model (model)
(when model
(rails-core:migration-file
(concat "Create" (rails-core:class-by-file (pluralize-string model))))))
(defun rails-core:model-by-migration-filename (migration-filename)
(when migration-filename
(let ((model-name (singularize-string
(string=~ "[0-9]+_create_\\(\\w+\\)\.rb" (buffer-name) $1))))
(when (and model-name
(rails-core:model-exist-p model-name))
model-name))))
(defun rails-core:configuration-file (file)
"Return the path to the configuration FILE."
(when file
(concat "config/" file)))
(defun rails-core:plugin-file (plugin file)
"Return the path to the FILE in Rails PLUGIN."
(concat "vendor/plugins/" plugin "/" file))
(defun rails-core:layout-file (layout)
"Return the path to the layout file named LAYOUT."
(let ((its rails-templates-list)
filename)
(while (and (car its)
(not filename))
(when (file-exists-p (format "%sapp/views/layouts/%s.%s" (rails-project:root) layout (car its)))
(setq filename (format "app/views/layouts/%s.%s" layout (car its))))
(setq its (cdr its)))
filename))
(defun rails-core:js-file (js)
"Return the path to the JavaScript file named JS."
(concat "app/assets/javascripts/" js ".js"))
(defun rails-core:partial-name (name)
"Return the file name of partial NAME."
(if (string-match "/" name)
(concat "app/views/"
(replace-regexp-in-string "\\([^/]*\\)$" "_\\1.html.erb" name))
(concat (rails-core:views-dir (rails-core:current-controller))
"_" name ".html.erb")))
(defun rails-core:view-name (name)
"Return the file name of view NAME."
(concat (rails-core:views-dir (rails-core:current-controller))
name ".html.erb")) ;; BUG: will fix it
(defun rails-core:helper-file (controller)
"Return the helper file name for the controller named
CONTROLLER."
(if (string= "Test/TestHelper" controller)
(rails-core:file (rails-core:file-by-class "Test/TestHelper"))
(when controller
(format "app/helpers/%s_helper.rb"
(replace-regexp-in-string "_controller" ""
(rails-core:file-by-class controller t))))))
(defun rails-core:helper-test-file (controller)
(when controller
(format "test/helpers/%s_helper_test.rb" (rails-core:file-by-class controller t))))
(assert (string= "test/helpers/foo/bar_quux_helper_test.rb" (rails-core:helper-test-file "Foo::BarQuux")))
(defun rails-core:controllers-test-file (controller)
"Return the controllers test file name for the controller named
CONTROLLER."
(when controller
(format "test/controllers/%s_test.rb"
(rails-core:file-by-class (rails-core:long-controller-name controller) t))))
(defun rails-core:decorators-test-file (decorator)
"Return the decorators test file name for the decorator named
DECORATOR."
(when decorator
(format "test/decorators/%s_test.rb"
(rails-core:file-by-class (rails-core:long-decorator-name decorator) t))))
(defun rails-core:integration-test-file (integration)
"Return the integration test file name for the controller named
CONTROLLER."
(when integration
(format "test/integration/%s_test.rb"
(rails-core:file-by-class integration t))))
(defun rails-core:mailer-file (mailer)
"Return the mailer file name for the controller named
CONTROLLER."
(when mailer
(format "app/mailers/%s.rb"
(rails-core:file-by-class mailer t))))
(defun rails-core:models-test-file (model)
"Return the models test file name for the model named MODEL."
(when model
(format "test/models/%s_test.rb" (rails-core:file-by-class model t))))
(defun rails-core:models-test-exist-p (model)
"Return the models test file name for the model named MODEL."
(let ((test (rails-core:models-test-file model)))
(when test
(file-exists-p (rails-core:file test)))))
(defun rails-core:operations-test-file (model)
"Return the operations test file name for the model named MODEL."
(when model
(format "test/concepts/%s_test.rb" (rails-core:file-by-class model t))))
(defun rails-core:operations-test-exist-p (model)
"Return the operations test file name for the model named MODEL."
(let ((test (rails-core:operations-test-file model)))
(when test
(file-exists-p (rails-core:file test)))))
(defun rails-core:system-test-file (model)
"Return the system test file name for the model named MODEL."
(when model
(format "test/system/%s_test.rb" (rails-core:file-by-class model t))))
(defun rails-core:system-test-exist-p (model)
"Return the system test file name for the model named MODEL."
(let ((test (rails-core:system-test-file model)))
(when test
(file-exists-p (rails-core:file test)))))
(defun rails-core:fixture-file (model)
"Return the fixtures file name for the model named MODEL."
(when model
(format "test/fixtures/%s.yml" (pluralize-string (rails-core:file-by-class model t)))))
(defun rails-core:fixture-exist-p (model)
(when model
(file-exists-p
(rails-core:file (rails-core:fixture-file model)))))
(defun rails-core:views-dir (controller)
"Return the view directory name for the controller named CONTROLLER."
(format "app/views/%s/" (replace-regexp-in-string "_controller" "" (rails-core:file-by-class controller t))))
(defun rails-core:stylesheet-name (name)
"Return the file name of the stylesheet named NAME."
(concat "app/assets/stylesheets/" name ".css"))
(defun rails-core:controller-name (controller-file)
"Return the class name of the controller named CONTROLLER.
Bar in Foo dir -> Foo::Bar"
(rails-core:class-by-file
(if (eq (elt controller-file 0) 47) ;;; 47 == '/'
(subseq controller-file 1)
(let ((current-controller (rails-core:current-controller)))
(if (string-match ":" current-controller)
(concat (replace-regexp-in-string "[^:]*$" "" current-controller)
controller-file)
controller-file)))))
(defun rails-core:short-controller-name (controller)
"Convert FooController -> Foo."
(remove-postfix controller "Controller" ))
(defun rails-core:long-controller-name (controller)
"Convert Foo/FooController -> FooController."
(if (string-match "Controller$" controller)
controller
(concat controller "Controller")))
(defun rails-core:decorator-name (decorator-file)
"Return the class name of the decorator named DECORATOR.
Bar in Foo dir -> Foo::Bar"
(rails-core:class-by-file
(if (eq (elt decorator-file 0) 47) ;;; 47 == '/'
(subseq decorator-file 1)
(let ((current-decorator (rails-core:current-decorator)))
(if (string-match ":" current-decorator)
(concat (replace-regexp-in-string "[^:]*$" "" current-decorator)
decorator-file)
decorator-file)))))
(defun rails-core:short-decorator-name (decorator)
"Convert FooDecorator -> Foo."
(remove-postfix decorator "Decorator" ))
(defun rails-core:long-decorator-name (decorator)
"Convert Foo/FooDecorator -> FooDecorator."
(if (string-match "Decorator$" decorator)
decorator
(concat decorator "Decorator")))
(defun rails-core:lib-file (lib-name)
"Return the model file from the lib name."
(when lib-name
(concat "lib/" (rails-core:file-by-class lib-name))))
;;;;;;;;;; Functions that return collection of Rails objects ;;;;;;;;;;
(defun rails-core:observer-p (name)
(when name
(if (string-match "\\(Observer\\|_observer\\)\\(\\.rb\\)?$" name)
t nil)))
(defun rails-core:mailer-p (name)
(when name
(if (string-match "\\(Mailer\\|Notifier\\|_mailer\\|_notifier\\)\\(\\.rb\\)?$" name)
t nil)))
(defun rails-core:controllers (&optional cut-contoller-suffix)
"Return a list of Rails controllers. Remove the '_controller'
suffix if CUT-CONTOLLER-SUFFIX is non nil."
(mapcar
#'(lambda (controller)
(rails-core:class-by-file
(if cut-contoller-suffix
(replace-regexp-in-string "_controller\\." "." controller)
controller)))
(delete-if-not
#'(lambda (controller)
(string-match "\\(application\\|[a-z0-9_]+_controller\\)\\.rb$"
controller))
(directory-files-recursive (rails-core:file "app/controllers/") nil "\\.rb$"))))
(defun rails-core:controllers-tests ()
"Return a list of Rails controllers tests."
(mapcar
#'(lambda(it)
(remove-postfix (rails-core:class-by-file it)
"ControllerTest"))
(directory-files-recursive (rails-core:file "test/controllers/") nil "\\.rb$")))
(defun rails-core:decorators-tests ()
"Return a list of Rails decorators tests."
(mapcar
#'(lambda(it)
(remove-postfix (rails-core:class-by-file it)
"DecoratorTest"))
(directory-files-recursive (rails-core:file "test/decorators/") nil "\\.rb$")))
(defun rails-core:integration-tests ()
"Return a list of Rails integration tests."
(mapcar
#'(lambda(it)
(remove-postfix (rails-core:class-by-file it)
"Test"))
(directory-files-recursive (rails-core:file "test/integration/") nil "\\.rb$")))
(defun rails-core:models ()
"Return a list of Rails models."
(mapcar
#'rails-core:class-by-file
(directory-files-recursive (rails-core:file "app/models/") nil "\\.rb$")))
(defun rails-core:libs ()
"Return a list of files in libs."
(mapcar
#'rails-core:class-by-file
(directory-files-recursive (rails-core:file "lib/") nil "\\.rb$")))
(defun rails-core:lib-file (filename)
"todo: document that."
(when filename
(format "lib/%s.rb"
(rails-core:file-by-class filename t))))
(defun rails-core:decorators ()
"Return a list of files in app/decorators."
(mapcar
#'rails-core:class-by-file
(directory-files-recursive (rails-core:file "app/decorators/") nil "\\.rb$")))
(defun rails-core:decorator-file (filename)
"Find decorator file."
(when filename
(format "app/decorators/%s.rb"
(rails-core:file-by-class filename t))))
(defun rails-core:mailer-file (filename)
"Find Mailer file."
(when filename
(format "app/mailers/%s.rb"
(rails-core:file-by-class filename t))))
(defun rails-core:models-tests ()
"Return a list of Rails model tests."
(mapcar
#'(lambda(it)
(remove-postfix (rails-core:class-by-file it)
"Test"))
(directory-files-recursive (rails-core:file "test/models/") nil "\\.rb$")))
(defun rails-core:observers ()
"Return a list of Rails observers."
(mapcar
#'(lambda (observer) (replace-regexp-in-string "Observer$" "" observer))
(mapcar
#'rails-core:class-by-file
(directory-files-recursive (rails-core:file "app/models/") nil "\\(_observer\\)\\.rb$"))))
(defun rails-core:mailers ()
"Return a list of Rails mailers."
(mapcar
#'rails-core:class-by-file
(directory-files-recursive (rails-core:file "app/mailers/") nil "\\.rb$")))
(defun rails-core:helpers ()
"Return a list of Rails helpers."
(append
(mapcar
#'(lambda (helper) (replace-regexp-in-string "Helper$" "" helper))
(mapcar
#'rails-core:class-by-file
(directory-files-recursive (rails-core:file "app/helpers/") nil "_helper\\.rb$")))
(list "Test/TestHelper")))
(defun rails-core:migrations (&optional strip-numbers)
"Return a list of Rails migrations."
(let (migrations)
(setq
migrations
(reverse
(mapcar
#'(lambda (migration)
(replace-regexp-in-string "^\\([0-9]+\\)" "\\1 " migration))
(mapcar
#'rails-core:class-by-file
(directory-files-recursive (rails-core:file "db/migrate") nil "^[0-9]+_.*\\.rb$")))))
(if strip-numbers
(mapcar #'(lambda(i) (car (last (split-string i " "))))
migrations)
migrations)))
(defun rails-core:migration-versions (&optional with-zero)
"Return a list of migtaion versions as the list of strings. If
second argument WITH-ZERO is present, append the \"000\" version
of migration."
(let ((ver (mapcar
#'(lambda(it) (car (split-string it " ")))
(rails-core:migrations))))
(if with-zero
(append ver '("000"))
ver)))
(defun rails-core:plugins ()
"Return a list of Rails plugins."
(mapcar
#'file-name-nondirectory
(delete-if-not
#'file-directory-p
(directory-files (rails-core:file "vendor/plugins") t "^[^\\.]"))))
(defun rails-core:plugin-files (plugin)
"Return a list of files in specific Rails plugin."
(directory-files-recursive (rails-core:file (concat "vendor/plugins/" plugin)) nil "^[^.]"))
(defun rails-core:layouts ()
"Return a list of Rails layouts."
(mapcar
#'(lambda (l)
(replace-regexp-in-string "\\.[^.]+$" "" l))
(directory-files-recursive (rails-core:file "app/views/layouts") nil (rails-core:regex-for-match-view))))
(defun rails-core:fixtures ()
"Return a list of Rails fixtures."
(mapcar
#'(lambda (l)
(replace-regexp-in-string "\\.[^.]+$" "" l))
(directory-files-recursive (rails-core:file "test/fixtures/") nil "\\.yml$")))
(defun rails-core:configuration-files ()
"Return a files of files from config folder."
(directory-files-recursive (rails-core:file "config/")))
(defun rails-core:regex-for-match-view ()
"Return a regex to match Rails view templates.
The file extensions used for views are defined in `rails-templates-list'."
(format "\\.\\(%s\\)$" (strings-join "\\|" rails-templates-list)))
(defun rails-core:get-view-files (controller-class &optional action)
"Retun a list containing the view file for CONTROLLER-CLASS#ACTION.
If the action is nil, return all views for the controller."
(rails-project:with-root
(root)
(directory-files
(rails-core:file
(rails-core:views-dir
(rails-core:short-controller-name controller-class))) t
(if action
(concat "^" action (rails-core:regex-for-match-view))
(rails-core:regex-for-match-view)))))
(defun rails-core:extract-ancestors (classes)
"Return the parent classes from a list of classes named CLASSES."
(delete ""
(uniq-list
(mapcar (lambda (class)
(replace-regexp-in-string
"::[^:]*$" "::"
(replace-regexp-in-string "^[^:]*$" "" class)))
classes))))
(defun rails-core:models-ancestors ()
"Return the parent classes of models."
(rails-core:extract-ancestors (rails-core:models)))
(defun rails-core:controllers-ancestors ()
"Return the parent classes of controllers."
(rails-core:extract-ancestors (rails-core:controllers)))
;;;;;;;;;; Getting Controllers/Decorators/Model/Action from current buffer ;;;;;;;;;;
(defun rails-core:current-controller ()
"Return the current Rails controller."
(let* ((file-class (rails-core:class-by-file (buffer-file-name))))
(unless (rails-core:mailer-p file-class)
(case (rails-core:buffer-type)
(:controller (rails-core:short-controller-name file-class))
(:view (rails-core:class-by-file
(directory-file-name (directory-of-file (buffer-file-name)))))
(:helper (remove-postfix file-class "Helper"))
(:controllers-test (remove-postfix file-class "ControllerTest"))))))
(defun rails-core:current-model ()
"Return the current Rails model."
(let* ((file-class (rails-core:class-by-file (buffer-file-name))))
(unless (rails-core:mailer-p file-class)
(case (rails-core:buffer-type)
(:migration (rails-core:model-by-migration-filename (buffer-name)))
(:model file-class)
(:models-test (remove-postfix file-class "Test"))
(:fixture (singularize-string file-class))))))
(defun rails-core:current-operation ()
"Return the current Rails operation."
(let* ((file-class (rails-core:class-by-file (buffer-file-name))))
(unless (rails-core:mailer-p file-class)
(case (rails-core:buffer-type)
(:operation file-class)
(:operations-test (remove-postfix file-class "Test"))
(:fixture (singularize-string file-class))))))
(defun rails-core:current-system ()
"Return the current Rails operation."
(let* ((file-class (rails-core:class-by-file (buffer-file-name))))
(unless (rails-core:mailer-p file-class)
(case (rails-core:buffer-type)
(:system file-class)
(:system-test (remove-postfix file-class "Test"))
(:fixture (singularize-string file-class))))))
(defun rails-core:current-decorator ()
"Return the current Rails decorator."
(let* ((file-class (rails-core:class-by-file (buffer-file-name))))
(unless (rails-core:mailer-p file-class)
(case (rails-core:buffer-type)
(:decorator (rails-core:short-decorator-name file-class))
(:decorators-test (remove-postfix file-class "DecoratorTest"))))))
(defun rails-core:current-lib ()
"Return the current lib."
(let* ((file-class (rails-core:class-by-file (buffer-file-name))))
(unless (rails-core:mailer-p file-class)
(case (rails-core:buffer-type)
(:lib file-class)))))
(defun rails-core:current-mailer ()
"Return the current Rails Mailer, else return nil."
(let* ((file-class (rails-core:class-by-file (buffer-file-name)))
(test (remove-postfix file-class "Test"))
(mailer-class (case (rails-core:buffer-type)
(:mailer file-class)
(:models-test test)
(:view (rails-core:class-by-file
(directory-file-name (directory-of-file (buffer-file-name))))))))
(and (rails-core:mailer-p mailer-class) mailer-class)))
(defun rails-core:current-action ()
"Return the current action in the current Rails controller."
(case (rails-core:buffer-type)
(:controller (rails-core:current-method-name))
(:mailer (rails-core:current-method-name))
(:view (string-match "/\\([a-z0-9_]+\\)\.[a-z]+$" (buffer-file-name))
(match-string 1 (buffer-file-name)))))
(defun rails-core:current-helper ()
"Return the current helper"
(rails-core:current-controller))
(defun rails-core:current-plugin ()
"Return the current plugin name."
(let ((name (buffer-file-name)))
(when (string-match "vendor\\/plugins\\/\\([^\\/]+\\)" name)
(match-string 1 name))))
(defun rails-core:current-method-name ()
(save-excursion
(when (search-backward-regexp "^[ ]*def \\([a-z0-9_]+\\)" nil t)
(match-string-no-properties 1))))
(defun rails-core:current-migration-version ()
"Return the current migration version"
(let ((name (buffer-file-name)))
(when (string-match "db\\/migrate\\/\\([0-9]+\\)[a-z0-9_]+\.[a-z]+$" name)
(match-string 1 name))))
(defun rails-core:grep-from-file (file regexp replacement)
(and file
(file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-min))
(and (re-search-forward regexp nil t)
(match-substitute-replacement replacement)))))
(defun rails-core:grep-from-runner (stmt regexp replacement)
(and (rails-core:file "script/runner")
(with-temp-buffer
(let ((default-directory (rails-project:root)))
(shell-command (concat rails-ruby-command " script/runner '" stmt "'")
(current-buffer)
nil)
(goto-char (point-min))
(and (re-search-forward regexp)
(match-substitute-replacement replacement))))))
(defun rails-core:current-rails-version ()
"Return the rails version of the current project"
(let* ((version-rb-re (concat "MAJOR[[:space:]]+=[[:space:]]+\\([[:digit:]]+\\)"
"[[:space:]]+MINOR[[:space:]]+=[[:space:]]\\([[:digit:]]+\\)"
"[[:space:]]+TINY[[:space:]]+=[[:space:]]\\([[:digit:]]+\\)"))
(fns
(list
(lambda ()
(rails-core:grep-from-file (rails-core:file "Gemfile.lock")
"^[[:space:]]+rails[[:space:]]+(\\([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\\))"
"\\1"))
(lambda ()
(rails-core:grep-from-file (rails-core:file "config/environment.rb")
"^RAILS_GEM_VERSION[[:space:]]+=[[:space:]]\\(['\"]\\)\\(.*?\\)\\1"
"\\2"))
(lambda ()
(rails-core:grep-from-file (rails-core:file "vendor/rails/railties/lib/rails/version.rb")
version-rb-re
"\\1.\\2.\\3"))
(lambda ()
(rails-core:grep-from-file (rails-core:file "vendor/rails/railties/lib/rails_version.rb")
version-rb-re
"\\1.\\2.\\3"))
(lambda ()
(rails-core:grep-from-runner "puts Rails::VERSION::STRING"
"^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$"
"\\&"))))
(version nil))
(while (and (not version) fns)
(setq version (funcall (car fns))
fns (cdr fns)))
version))
(defun rails-core:current-rails-major-version ()
"Return project major version of rails."
(let ((version (rails-core:current-rails-version)))
(when (string-match "^[0-9]+" version)
(string-to-number (match-string 0 version)))))
;;;;;;;;;; Determination of buffer type ;;;;;;;;;;
(defun rails-core:buffer-file-match (regexp)
"Match the current buffer file name to RAILS_ROOT + REGEXP."
(when-bind (file (rails-core:file regexp))
(string-match file
(buffer-file-name (current-buffer)))))
(defun rails-core:buffer-type ()
"Return the type of the current Rails file or nil if the type
cannot be determinated."
(loop for (type dir func) in rails-directory<-->types
when (and (rails-core:buffer-file-match dir)
(if func
(apply func (list (buffer-file-name (current-buffer))))
t))
do (return type)))
;;;;;;;;;; Rails minor mode Buttons ;;;;;;;;;;
(define-button-type 'rails-button
'follow-link t
'action #'rails-core:button-action)
(defun rails-core:button-action (button)
(let* ((file-name (button-get button :rails:file-name))
(line-number (button-get button :rails:line-number))
(file (rails-core:file file-name)))
(when (and file
(file-exists-p file))
(find-file-other-window file)
(when line-number
(goto-line line-number)))))
;;;;;;;;;; Rails minor mode logs ;;;;;;;;;;
(defun rails-log-add (message)
"Add MESSAGE to the Rails minor mode log in RAILS_ROOT."
(rails-project:with-root
(root)
(append-string-to-file (rails-core:file "log/rails-minor-mode.log")
(format "%s: %s\n"
(format-time-string "%Y/%m/%d %H:%M:%S") message))))
(defun rails-logged-shell-command (command buffer)
"Execute a shell command in the buffer and write the results to
the Rails minor mode log."
(shell-command (format "%s %s" rails-ruby-command command) buffer)
(rails-log-add
(format "\n%s> %s\n%s" (rails-project:name)
command (buffer-string-by-name buffer))))
;;;;;;;;;; Rails menu ;;;;;;;;;;
(defun rails-core:menu-separator ()
(unless (rails-use-text-menu) 'menu (list "--" "--")))
(if (fboundp 'completion-posn-at-point-as-event)
(defun rails-core:menu-position ()
(completion-posn-at-point-as-event nil nil nil (+ (frame-char-height) 2)))
(defun rails-core:menu-position ()
(list '(300 50) (get-buffer-window (current-buffer)))))
;; fixup emacs-rails menu specs to work with tmm-prompt
(defun rails-core:tmm-menu (menu)
(symbol-name (tmm-prompt (cons (car menu)
(mapcar (lambda (pane)
(cons (car pane)
(mapcar (lambda (item)
(if (symbolp (cdr item))
item
(cons (car item)
(intern (cdr item)))))
(cdr pane))))
(cdr menu))))))
(defun rails-core:ido-menu (menu)
(let* ((prompt (car (car (cdr menu))))
(mappings (cdr (car (cdr menu))))
(choices (delete-if #'not (mapcar (lambda (item) (car item)) mappings)))
(default (if (find-if (lambda (val) (string= (word-at-point) val)) choices)
(word-at-point)))
(result (ido-completing-read prompt choices nil nil default)))
(or (cdr (assoc result mappings)) result)))
(defun rails-core:menu (menu)
"Show a menu."
(let ((result
(if (rails-use-text-menu)
(funcall (or rails-text-menu-function
(and (boundp 'ido-mode) ido-mode #'rails-core:ido-menu)
#'rails-core:tmm-menu) menu)
(x-popup-menu (rails-core:menu-position)
(rails-core:prepare-menu menu)))))
(if (listp result)
(first result)
result)))
(defvar rails-core:menu-letters-list
(let ((res '()))
(loop for i from (string-to-char "1") upto (string-to-char "9")
do (add-to-list 'res (char-to-string i) t))
(loop for i from (string-to-char "a") upto (string-to-char "z")
do (add-to-list 'res (char-to-string i) t))
res)
"List contains 0-9a-z letter")
(defun rails-core:prepare-menu (menu)
"Append a prefix to each label of menu-item from MENU."
(let ((title (car menu))
(menu (cdr menu))
(result '())
(result-line '())
(letter 0))
(dolist (line menu)
(setq result-line '())
(dolist (it line)
(typecase it
(cons
(if (and (string= (car (rails-core:menu-separator)) (car it))
(string= (cadr (rails-core:menu-separator)) (cadr it)))
(add-to-list 'result-line it t)
(progn
(add-to-list 'result-line (cons
(format "%s) %s"
(nth letter rails-core:menu-letters-list)
(car it))
(cdr it))
t)
(setq letter (+ 1 letter)))))
(t
(add-to-list 'result-line it t))))
(add-to-list 'result result-line t))
(cons title result)))
;;;;;;;;;; Misc ;;;;;;;;;;
(defun rails-core:erb-block-string ()
"Return the contents of the current ERb block."
(save-excursion
(save-match-data
(let ((start (point)))
(search-backward-regexp "<%[=]?")
(let ((from (match-end 0)))
(search-forward "%>")
(let ((to (match-beginning 0)))
(when (>= to start)
(buffer-substring-no-properties from to))))))))
(defun rails-core:rhtml-buffer-p ()
"Return non nil if the current buffer is rhtml file."
(string-match "\\.rhtml\\|\\.html\\.erb$" (buffer-file-name)))
(defun rails-core:prepare-command (command)
(if (and rails-rake-use-bundler-when-possible (file-exists-p (rails-core:file "Gemfile")))
(concat "bundle exec " command)
command))
(provide 'rails-core)