Class: Forge::Generators::ScaffoldGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/forge/lib/generators/forge/scaffold/scaffold_generator.rb

Direct Known Subclasses

ScaffoldSmallGenerator

Instance Method Summary collapse

Instance Method Details

#add_i18nObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/forge/lib/generators/forge/scaffold/scaffold_generator.rb', line 47

def add_i18n
  case self.behavior
  when :invoke
    # Write a couple of lines to config/i18n_fields.yml
    pattern = "tables:"
    replacement = "tables:\n  #{plural_table_name}:\n    -\n"
  when :revoke
    # now remove them
    pattern = /^  #{plural_table_name}:\n(    - *[\w]*\n*)*/
    replacement = ""
  end

  filename = File.join(Rails.root, 'config', 'i18n_fields.yml')
  converted_content = File.read(filename).gsub(pattern, replacement)
  File.open(filename, 'w'){ |f| f.write converted_content }
end

#add_routesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/forge/lib/generators/forge/scaffold/scaffold_generator.rb', line 32

def add_routes
  case self.behavior
  when :invoke
    # Write the routes
    filename = File.join(Rails.root, 'config', 'routes.rb')
    pattern = "namespace :forge do"
    reorder = attributes.select{|a| a.name == "list_order" }.empty? ? "" : "do \n      post    'reorder', :on => :collection \n    end"
    replacement = "namespace :forge do \n    resources :#{plural_table_name} #{reorder}"
    converted_content = File.read(filename).gsub(pattern, replacement)
    File.open(filename, 'w'){ |f| f.write converted_content }
  when :revoke
    say "Be sure to clean up your routes!", :red
  end
end

#copy_filesObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/forge/lib/generators/forge/scaffold/scaffold_generator.rb', line 9

def copy_files
  style = options.small? ? 'small' : 'full'

  # Controller, helper, views, test and stylesheets directories.
  empty_directory(File.join('app/views/forge', plural_table_name))

  for action in %w{index new edit _form}
    template("view_#{action}.html.haml", File.join('app/views/forge', plural_table_name, "#{action}.html.haml"))
  end

  template("view__item.html.haml", File.join('app/views/forge', plural_table_name, "_#{file_name}.html.haml"))

  template("menu_section.html.haml", File.join('app/views/forge/shared/menu_items', "_#{file_name.pluralize}.html.haml"))

  template(
    'controller.rb', File.join('app/controllers/forge', "#{file_name.pluralize}_controller.rb")
  )

  template('controller_spec.rb', File.join('spec/controllers/forge', "#{file_name.pluralize}_controller_spec.rb"))

  say "CAUTION: your new model is open to mass assignment for all attributes due to 'attr_protected' at top of file. Update accordingly.", :red
end