require 'generators/rspec' require 'generators/my_scaffold_generator' require 'rails/generators/resource_helpers' module MySpec module Generators class ScaffoldGenerator < Rails::Generators::NamedBase include Rails::Generators::ResourceHelpers include MyGenerators::Generators::MyScaffoldGenerator argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" def generate_controller_spec return unless options[:controller_specs] template 'inherited_resources_stubs.rb', 'spec/support/inherited_resources_stubs.rb' template 'controller_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb") end def generate_view_specs return unless options[:view_specs] copy_view :edit copy_view :index unless options[:singleton] copy_view :new copy_view :show end # Invoke the helper using the controller name (pluralized) hook_for :helper, :as => :scaffold do |invoked| invoke invoked, [ controller_name ] end def generate_routing_spec return unless options[:routing_specs] template 'routing_spec.rb', File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb") end hook_for :integration_tool, :as => :integration def banner self.class.banner end def self.source_root @source_root ||= File.expand_path("templates", File.dirname(__FILE__)) end protected def redirect_url_helper(str=nil) if str belongs_to ? "#{parent_name}_#{model_name}_url(john_q(:#{parent_name})#{', '+str})" : "#{model_name}_url(#{str})" else belongs_to ? "#{parent_name}_#{table_name}_url(john_q(:#{parent_name}))" : "#{table_name}_url" end end def mock(str=nil) "john_q(:#{str || model_name})" end def stub_finder(idstr, retstr, method="stub") if child_model? "john_q(:#{parent_name}).#{table_name}.#{method}(:find).with(#{idstr}) { #{retstr} }" else "#{class_name}.#{method}(:find).with(#{idstr}) { retstr }" end end def stub_index_finder(retstr, method="stub") if child_model? "john_q(:#{parent_name}).#{table_name}.#{method}(:all) { #{retstr} }" else "#{class_name}.#{method}(:find).with(:all) { retstr }" end end def base_url (belongs_to ? "/#{parents_name}/24" : "") + "/#{singleton ? model_name : table_name}" end def stub_builder(retstr) if child_model? "john_q(:#{parent_name}).#{table_name}.stub(:build) { #{retstr} }" else "#{class_name}.stub(:new) { retstr }" end end end end end