forked from dcrec1/blue-ridge
/
javascript_spec_generator.rb
47 lines (38 loc) · 1.37 KB
/
javascript_spec_generator.rb
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
require 'blue_ridge'
module BlueRidge
module Generators
class JavascriptSpecGenerator < Rails::Generators::NamedBase
def execute
base_dir = BlueRidge.javascript_spec_dir
empty_directory base_dir
empty_directory "#{base_dir}/fixtures"
options = {:class_name_without_spec => class_name_without_spec, :file_path_without_spec => file_path_without_spec}
template 'javascript_spec.js.erb', "#{base_dir}/#{file_path_with_spec}.js", :assigns => options
template 'fixture.html.erb', "#{base_dir}/fixtures/#{file_path_without_spec}.html", :assigns => options
end
def self.gem_root
File.expand_path("../../../../../", __FILE__)
end
def self.source_root
File.join(gem_root, 'templates', 'javascript_spec')
end
protected
def file_path_with_and_without_spec
if (file_path =~ /_spec$/i)
[file_path, file_path.gsub(/_spec$/, "")]
else
[file_path + "_spec", file_path]
end
end
def file_path_without_spec
file_path_with_and_without_spec.last
end
def file_path_with_spec
file_path_with_and_without_spec.first
end
def class_name_without_spec
(class_name =~ /Spec$/) ? class_name.gsub(/Spec$/, "") : class_name
end
end
end
end