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



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

def generate
  model_name = resource.camelize
  folder = controller.gsub(/Controller$/,'').underscore
  target = ['app/views',folder].join '/'
  namespace = folder.split('/').slice(0...-1)
  model_name.constantize
  controller.constantize
  real_name = model_name.demodulize
  name_singular = real_name.underscore.downcase
  route = folder.gsub('/','_').singularize

  if options[:yes] or yes?("generate views for '#{model_name}' to: '#{target}'?")
    replacements = {
      model: model_name,
      name_singular: name_singular,
      name_plural: real_name.pluralize.underscore.downcase,
      path_singular: route,
      path_plural: route.pluralize,
      table_heads: columns.collect{ |c| "- h.text t('.column_#{c}')" }.join("\n").indent(8),
      table_cells: columns.collect{ |c| "- r.cell #{name_singular}.#{c}" }.join("\n").indent(8),
      entries: columns.collect{ |c| "- l.entry t('.entry_#{c}'), @#{name_singular}.#{c}" }.join("\n").indent(8),
      inputs: columns.collect{ |c| "= f.input :#{c}, label: t('.field_#{c}')" }.join("\n").indent(8),
      form_path: namespace.collect{ |n| "'#{n}'" }.push("@#{name_singular}").join(',')
    }

    directory 'views', target do |content|
      replacements.each do |key,value|
        content.gsub! "{{{#{key}}}}", value
      end
      content
    end
  end

  i18n_file = "config/locales/en/#{route.pluralize}.en.yml"
  if options[:yes] or 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.deep_stringify_keys.to_yaml
  end

end