Class: AdminGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/admin/admin_generator.rb

Constant Summary collapse

@@models =

argument :model_names, :type => :array

{}
@@reserved_title_names =
%W(title name)

Instance Method Summary collapse

Instance Method Details

#make_admin_configObject



98
99
100
# File 'lib/generators/admin/admin_generator.rb', line 98

def make_admin_config
  directory 'config'
end

#make_app_filesObject



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
		# puts "model path : #{model_path}"
		# puts "error : #{e.message}"
		# Just for non-mongoid objects that dont have the embedded
		# attribute at the class level.
	end
end

  #copy default controllers
  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

  #copy views & layouts
  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'

  #copy asset
  directory 'app/assets'
end

#make_routingObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/admin/admin_generator.rb', line 80

def make_routing
  admin_routes = <<EOF
namespace :admin do
match '/' => 'admin#index'
match 'login' => 'auth#login'
match 'logout' => 'auth#logout'
match 'authorize' => 'auth#authorize'
	#{@@models.keys.map {|k| @@models[k].map {|model| "resources :#{model.name.underscore.pluralize}" }.join("\n")}.join("\n")}
end
EOF
  route admin_routes
end

#make_scriptObject



93
94
95
96
# File 'lib/generators/admin/admin_generator.rb', line 93

def make_script
  directory 'script'
  chmod 'script/new_admin', 0754
end