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
|
# File 'lib/generators/admin/admin_generator.rb', line 6
def make_app_files
Dir.glob("app/models/**/*.rb").sort.each do |file|
model_path = file[0..-4].split('/')[2..-1]
begin
klass = model_path.map { |path| path.camelize }.join('::').constantize
if defined? Mongoid
puts "Mongoid defined"
if klass.ancestors.include?(Mongoid::Document) && !klass.embedded
@@models[:mongoid] = [] if @@models[:mongoid].nil?
@@models[:mongoid] << klass
end
elsif defined? ActiveRecord
puts "ActiveRecord defined"
if klass.ancestors.include?(ActiveRecord::Base)
@@models[:activerecord] = [] if @@models[:activerecord].nil?
@@models[:activerecord] << klass
end
end
rescue => e
end
end
copy_file 'app/controllers/admin/admin_controller.rb'
copy_file 'app/controllers/admin/auth_controller.rb'
@@models.keys.each do |k|
@@models[k].each do |model|
@orm = k
@model = model
@reserved_title_names = @@reserved_title_names
if @orm == :activerecord
@fields = @model.new.attributes.keys - @model.protected_attributes.to_a
elsif @orm == :mongoid
@fields = @model.fields.keys - %w(_id _type)
end
template 'app/controllers/admin/model_controller.rb.erb',
"app/controllers/admin/#{@model.name.underscore.pluralize}_controller.rb"
template 'app/views/admin/model/index.html.erb',
"app/views/admin/#{@model.name.underscore.pluralize}/index.html.erb"
template 'app/views/admin/model/new.html.erb',
"app/views/admin/#{@model.name.underscore.pluralize}/new.html.erb"
template 'app/views/admin/model/show.html.erb',
"app/views/admin/#{@model.name.underscore.pluralize}/show.html.erb"
template 'app/views/admin/model/_form.html.erb',
"app/views/admin/#{@model.name.underscore.pluralize}/_form.html.erb"
template 'app/views/admin/model/edit.html.erb',
"app/views/admin/#{@model.name.underscore.pluralize}/edit.html.erb"
end
end
directory 'app/views/admin/admin'
directory 'app/views/admin/auth'
@models = @@models
template 'app/views/admin/admin/_sidebar.html.erb',
"app/views/admin/admin/_sidebar.html.erb"
template 'app/views/layouts/admin.html.erb', 'app/views/layouts/admin.html.erb'
directory 'app/assets'
end
|