module Casein class ScaffoldGenerator < Rails::Generators::NamedBase include Casein::CaseinHelper include Rails::Generators::Migration source_root File.expand_path('../templates', __FILE__) argument :attributes, :type => :array, :required => true, :desc => "attribute list required" class_options :create_model_and_migration => false, :read_only => false, :no_index => false def self.next_migration_number dirname if ActiveRecord::Base.timestamped_migrations Time.now.utc.strftime("%Y%m%d%H%M%S") else "%.3d" % (current_migration_number(dirname) + 1) end end def generate_files @plural_route = (plural_name != singular_name) ? plural_name : "#{plural_name}_index" @read_only = options[:read_only] @no_index = options[:no_index] template 'controller.rb', "app/controllers/casein/#{plural_name}_controller.rb" template 'views/index.html.erb', "app/views/casein/#{plural_name}/index.html.erb" unless @no_index template 'views/show.html.erb', "app/views/casein/#{plural_name}/show.html.erb" template 'views/new.html.erb', "app/views/casein/#{plural_name}/new.html.erb" unless @read_only template 'views/_form.html.erb', "app/views/casein/#{plural_name}/_form.html.erb" template 'views/_table.html.erb', "app/views/casein/#{plural_name}/_table.html.erb" add_namespace_to_routes add_to_routes add_to_navigation unless @no_index if options[:create_model_and_migration] template 'model.rb', "app/models/#{singular_name}.rb" migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb" end end protected #replacements for standard Rails generator route. This one only adds once def add_namespace_to_routes puts " casein adding namespace to routes.rb" file_to_update = Rails.root + 'config/routes.rb' line_to_add = "namespace :casein do" insert_sentinel = 'Application.routes.draw do' if File.read(file_to_update).scan(/(#{Regexp.escape("#{line_to_add}")})/mi).blank? gsub_add_once plural_name, file_to_update, "\n\t#Casein routes\n\t" + line_to_add + "\n\tend\n", insert_sentinel end end def add_to_routes puts " casein adding #{plural_name} resources to routes.rb" file_to_update = Rails.root + 'config/routes.rb' if @no_index && @read_only line_to_add = "resources :#{plural_name}, :only => [:show]" elsif @no_index line_to_add = "resources :#{plural_name}, :except => [:index]" elsif @read_only line_to_add = "resources :#{plural_name}, :only => [:index, :show]" else line_to_add = "resources :#{plural_name}" end insert_sentinel = 'namespace :casein do' gsub_add_once plural_name, file_to_update, "\t\t" + line_to_add, insert_sentinel end def add_to_navigation puts " casein adding #{plural_name} to left navigation bar" file_to_update = Rails.root + 'app/views/casein/layouts/_tab_navigation.html.erb' line_to_add = "