Class: Aureus::Generators::ViewsGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
lib/generators/aureus/views/views_generator.rb

Instance Method Summary collapse

Instance Method Details

#generateObject



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
# File 'lib/generators/aureus/views/views_generator.rb', line 13

def generate
	model_name = resource.camelize
	folder = controller.gsub(/Controller$/,"").underscore
	target = ["app/views",folder].join "/"
	namespace = folder.split("/").slice(0...-1)
	model = model_name.constantize
	controller_test = controller.constantize
	columns = model.column_names
	columns2 = columns - ["id","created_at","updated_at"]
	real_name = model_name.demodulize
	route = folder.gsub("/","_").singularize

	if yes? "generate views for '"+model_name+"' to: "+target+"?"
		directory "views", target
		replacements = {
			"MODEL" => model_name,
			"NAME_SINGULAR" => real_name.downcase,
			"NAME_PLURAL" => real_name.pluralize.downcase,
			"PATH_SINGULAR" => route,
			"PATH_PLURAL" => route.pluralize,
			"TABLE_HEADS" => columns.collect{ |c| '        - h.text t(".column_'+c+'")' }.join("\n"),
			"TABLE_CELLS" => columns.collect{ |c| '        - r.cell '+real_name.downcase+'.'+c }.join("\n"),
			"ENTRIES" => columns.collect{ |c| '        - l.entry t(".entry_'+c+'"), @'+real_name.downcase+'.'+c }.join("\n"),
			"INPUTS" => columns2.collect{ |c| '        = f.input :'+c+', :label => t(".field_'+c+'")' }.join("\n"),
			"FORM_PATH" => namespace.collect{ |n| '"'+n+'"' }.push("@"+real_name.downcase).join(",")
		}
		Dir[target+"/*.haml"].each do |file|
			replacements.each do |key,value|
				gsub_file file, "{{{#{key}}}}", value, :verbose => false
			end
		end
	end

	i18n_file = "config/locales/"+route.gsub("_",".").pluralize+".en.yml"
	if yes? "generate i18n file to: "+i18n_file+"?"
		singular = real_name
		plural = real_name.pluralize
		base_i18n = {
			plural.downcase => {
   				"index" => { "title" => plural, "button_new" => "Add "+singular },
   				"new" => { "title" => "New "+singular, "button_cancel" => "Cancel", "button_save" => "Save" },
   				"edit" => { "title" => "Edit "+singular, "button_cancel" => "Cancel", "button_save" => "Save" },
   				"form" => { "box_title" => "Details" },
   				"list" => { "box_title" => plural+" Listing", "destroy_confirm" => "Really want to delete the "+singular+"?" },
   				"show" => { "title" => singular, "button_edit" => "Edit "+singular, "button_back" => "Back" },
   				"item" => { "box_title" => "Details" }
   			}
		}
   		columns.each do |c|
   			base_i18n[plural.downcase]["item"][("entry_"+c)] = c.titleize
   			base_i18n[plural.downcase]["list"][("column_"+c)] = c.titleize
   			base_i18n[plural.downcase]["form"][("field_"+c)] = c.titleize
   		end
   		namespace.reverse.each do |n|
   			base_i18n = { n => base_i18n }
   		end
   		base_i18n = { "en" => base_i18n }
		create_file i18n_file, base_i18n.to_yaml
	end

end