-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin_ka_generator.rb
145 lines (114 loc) · 5.47 KB
/
admin_ka_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
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
require File.dirname(__FILE__) + '/routes_helper'
class AdminKaGenerator < Rails::Generator::NamedBase
# include RoutesHelper
attr_reader :controller_name,
:controller_class_path,
:controller_file_path,
:controller_class_name,
:controller_singular_name,
:controller_underscore_name,
:controller_plural_name,
:reflection
alias_method :controller_file_name, :controller_underscore_name
def initialize(runtime_args, runtime_options = {})
super
@controller_name = @name.pluralize
base_name, @controller_class_path, @controller_file_path = extract_modules(@controller_name)
@controller_plural_name, @controller_underscore_name = inflect_names(base_name)
@controller_singular_name = base_name.singularize
@controller_class_name = @controller_name
# raise name
unless name == 'Home'
clazz = Kernel.const_get(@class_name)
unless clazz.reflections.find_all{|k,r| r.macro == :has_many}.empty?
@reflection = clazz.reflections.find{|k,r| r.macro == :has_many}[0].to_s
else
@reflection = nil
end
end
end
def manifest
if name == 'Home'
base_manifest
else
model_manifest
end
end
protected
def base_manifest
record do |m|
# Controller, helper, views, and test directories.
m.directory(File.join('app/views/layouts', controller_class_path))
m.directory('vendor/plugins/admin_ka/lib/admin_ka')
m.directory('app/views/admin/general')
m.directory('app/views/admin/home')
m.directory('public/javascripts/admin')
m.directory('public/stylesheets/admin')
m.directory('vendor/plugins/active_record_mandatory/lib')
m.file 'init.rb', 'vendor/plugins/admin_ka/init.rb'
m.file 'admin_ka.rb', 'vendor/plugins/admin_ka/lib/admin_ka/admin_ka.rb'
m.file 'entity.rb', 'vendor/plugins/admin_ka/lib/admin_ka/entity.rb'
m.file 'field.rb', 'vendor/plugins/admin_ka/lib/admin_ka/field.rb'
m.file 'reflection.rb', 'vendor/plugins/admin_ka/lib/admin_ka/reflection.rb'
m.file 'query.rb', 'app/models/query.rb'
m.file 'reference_data.rb', 'app/models/reference_data.rb'
m.file 'init_active_record_mandatory.rb', 'vendor/plugins/active_record_mandatory/init.rb'
m.file 'active_record_mandatory.rb', 'vendor/plugins/active_record_mandatory/lib/active_record_mandatory.rb'
m.template "_filters.html.erb", File.join('app/views/admin/general', "_filters.html.erb")
m.template "home.html.erb", File.join('app/views/admin/home', "index.html.erb")
# Layout and stylesheet.
m.template('admin_application.rhtml', File.join('app/views/layouts', controller_class_path, "admin_application.html.erb"))
m.template('application.css', 'public/stylesheets/admin/application.css')
m.template('context_menu.css', 'public/stylesheets/admin/context_menu.css')
m.template('form.css', 'public/stylesheets/admin/form.css')
m.template('structure.css', 'public/stylesheets/admin/structure.css')
m.template('theme.css', 'public/stylesheets/admin/theme.css')
m.file('context_menu.js', 'public/javascripts/admin/context_menu.js')
m.file('wufoo.js', 'public/javascripts/admin/wufoo.js')
# Controllers
m.file 'secure_controller.rb', 'app/controllers/admin/secure_controller.rb'
m.file 'home_controller.rb', 'app/controllers/admin/home_controller.rb'
# Helpers
m.file "custom_tag_helper.rb", "/app/helpers/admin/custom_tag_helper.rb"
m.file "sort_helper.rb", "/app/helpers/admin/sort_helper.rb"
m.file "queries_helper.rb", "/app/helpers/admin/queries_helper.rb"
m.route_admin_home
end
end
def model_manifest
record do |m|
# Check for class naming collisions.
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
# Controller, helper, views, and test directories.
m.directory(File.join('app/controllers/admin', controller_class_path))
m.directory(File.join('app/helpers/admin', controller_class_path))
m.directory(File.join('app/views/admin', controller_class_path, controller_file_name))
m.template "index.html.erb", File.join('app/views/admin', controller_class_path, controller_file_name, "index.html.erb")
m.template "new.html.erb", File.join('app/views/admin', controller_class_path, controller_file_name, "new.html.erb")
m.template "edit.html.erb", File.join('app/views/admin', controller_class_path, controller_file_name, "edit.html.erb")
m.template "_form.html.erb", File.join('app/views/admin', controller_class_path, controller_file_name, "_form.html.erb")
m.template(
'controller.rb', File.join('app/controllers/admin', controller_class_path, "#{controller_file_name}_controller.rb")
)
m.route_admin_resources controller_file_name
end
end
# Override with your own usage banner.
def banner
"Usage: #{$0} admin_ka ModelName"
end
def add_options!(opt)
opt.separator ''
opt.separator 'Options:'
opt.on("--skip-timestamps",
"Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
opt.on("--skip-migration",
"Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
end
def scaffold_views
%w[ index new edit ]
end
def model_name
class_name.demodulize
end
end