Class: Tenon::Generators::ScaffoldGenerator

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

Direct Known Subclasses

ScaffoldSmallGenerator

Instance Method Summary collapse

Instance Method Details

#add_i18nObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/tenon/scaffold/scaffold_generator.rb', line 68

def add_i18n
  filename = File.join(Rails.root, 'config', 'i18n_fields.yml')

  case behavior
  when :invoke
    # Write a couple of lines to config/i18n_fields.yml
    pattern = 'tables:'
    replacement = "tables:\n  #{plural_table_name}:\n    -\n"
    if File.exist?(filename)
      puts 'Wrote I18n fields config'
    else
      puts 'Skipped I18n fields config'
    end
  when :revoke
    # now remove them
    pattern = /^  #{plural_table_name}:\n(    - *[\w]*\n*)*/
    replacement = ''
  end

  if File.exist?(filename)
    converted_content = File.read(filename).gsub(pattern, replacement)
    File.open(filename, 'w') { |f| f.write converted_content }
  end
end

#add_routesObject



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
# File 'lib/generators/tenon/scaffold/scaffold_generator.rb', line 39

def add_routes
  case behavior
  when :invoke
    # Prepare the routes
    filename = File.join(Rails.root, 'config', 'routes.rb')
    pattern = 'Tenon::Engine.routes.draw do'
    reorder = attributes.select { |a| a.name == 'list_order' }.empty? ? '' : "do \n    post    'reorder', :on => :collection \n    end"
    contents = File.read(filename)

    # Write the initial Tenon routes block if it's not there already
    unless contents.match(pattern)
      new_contents = "#{contents}\n\nTenon::Engine.routes.draw do\nend\n"
      File.open(filename, 'w') do |f|
        f.puts new_contents
      end
      contents = new_contents
    end

    # Then write the new route
    replacement = "Tenon::Engine.routes.draw do\n  resources :#{plural_table_name} #{reorder}"
    converted_content = contents.gsub(pattern, replacement)
    File.open(filename, 'w') do |f|
      f.write converted_content
    end
  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
31
32
33
34
35
36
37
# File 'lib/generators/tenon/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/tenon', plural_table_name))
  empty_directory(File.join('app/assets/javascripts/tenon/templates', plural_table_name))

  actions = %w(
    index.html.haml new.html.haml edit.html.haml _form.html.haml
    index.json.jbuilder create.json.jbuilder update.json.jbuilder
)
  actions.each do |action|
    src_path = File.join(self.class.source_root, "view_#{action}")
    if File.exist?(src_path)
      template("view_#{action}", File.join('app/views/tenon', plural_table_name, "#{action}"))
    end
  end

  template('view_item_row.jst.eco', File.join('app/assets/javascripts/tenon/templates', plural_table_name, "#{file_name}_row.jst.eco"))
  template('view__item.json.jbuilder', File.join('app/views/tenon', plural_table_name, "_#{file_name}.json.jbuilder"))

  template(
    'decorator.rb', File.join('app/decorators', "#{file_name}_decorator.rb")
  )

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