forked from apotonick/apotomo
/
widget_generator.rb
105 lines (82 loc) · 2.77 KB
/
widget_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
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
require 'generators/cells/base'
module Apotomo
module Generators
module BasePathMethods
private
def base_path
File.join('app/widgets', class_path)
end
def view_base_path
File.join('app/widgets', class_path, file_name)
end
def js_path
File.join('app/assets/javascripts/widgets', class_path, file_name)
end
def css_path
File.join('app/assets/stylesheets/widgets', class_path, file_name)
end
end
module Views
extend ActiveSupport::Concern
included do
class_option :parent, :type => :boolean, :default => false, :desc => 'Parent widget'
source_root File.expand_path('../../templates', __FILE__)
end
def create_views
for state in actions do
@state = state
# only make views container for top level widgets, since they are most likely to have
# child widgets
@path = if parent_widget?
File.join(view_base_path, 'views', "#{state}.html.#{handler}") #base_path defined in Cells::Generators::Base.
else
File.join(view_base_path, "#{state}.html.#{handler}") #base_path defined in Cells::Generators::Base.
end
template "view.#{handler}", @path
end
end
protected
def parent_widget?
options[:parent]
end
end
class WidgetGenerator < ::Cells::Generators::Base
include BasePathMethods
source_root File.expand_path('../../templates', __FILE__)
hook_for(:template_engine)
hook_for(:test_framework) # TODO: implement rspec-apotomo.
check_class_collision :suffix => "Widget"
class_option :js, :type => :boolean, :default => false, :desc => 'Generate javascript asset file'
def create_cell_file
template 'widget.rb', File.join(base_path, "#{file_name}_widget.rb")
end
def create_stylesheet_file
template 'widget.css', "#{css_path}_widget.css"
end
def create_script_file
puts "create_script_file"
if !javascript?
# raise "coffee: #{js_path}_widget.js.coffee"
template 'widget.js.coffee', "#{js_path}_widget.js.coffee"
else
puts "js: #{js_path}_widget.js"
template 'widget.js', "#{js_path}_widget.js"
end
end
protected
def ns_name
names = class_name.split('::')
ns = names[0..-2].map {|name| js_camelize name }.join('.')
return names.last if ns.blank?
ns << ".#{names.last}"
end
def js_camelize str
str = str.to_s
str.camelize.sub(/^\w/, str[0].downcase)
end
def javascript?
options[:js] == true
end
end
end
end