Class: Tenon::Generators::ScaffoldGenerator
- Inherits:
-
Rails::Generators::NamedBase
- Object
- Rails::Generators::NamedBase
- Tenon::Generators::ScaffoldGenerator
- Defined in:
- lib/generators/tenon/scaffold/scaffold_generator.rb
Instance Method Summary collapse
Instance Method Details
#add_i18n ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/generators/tenon/scaffold/scaffold_generator.rb', line 74 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_menu_item ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/generators/tenon/scaffold/scaffold_generator.rb', line 99 def case behavior when :invoke say 'Add the following line to app/views/tenon/shared/_main_nav.html.haml:' say " = nav_item '#{class_name.pluralize.titleize}', #{plural_name}_path, 'star'" when :revoke say %(Don't forget to remove the nav_item entry from app/views/tenon/shared/_main_nav.html.haml) end end |
#add_routes ⇒ Object
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 |
# File 'lib/generators/tenon/scaffold/scaffold_generator.rb', line 45 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_files ⇒ Object
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 |
# File 'lib/generators/tenon/scaffold/scaffold_generator.rb', line 9 def copy_files # Controller, helper, views, test and stylesheets directories. empty_directory(File.join('app/views/tenon', plural_table_name)) actions = %w( index.html.haml new.html.haml edit.html.haml _form.html.haml ) actions.each do |action| src_path = File.join(self.class.source_root, "view_#{action}") next unless File.exist?(src_path) template( "view_#{action}", File.join('app/views/tenon', plural_table_name, action) ) end %w( decorator policy serializer ).each do |file_type| template( "#{file_type}.rb", File.join( "app/#{file_type.pluralize}", "#{file_name}_#{file_type}.rb" ) ) end template( 'controller.rb', File.join( 'app/controllers/tenon', "#{file_name.pluralize}_controller.rb" ) ) end |