+class RspecHamlScaffoldGenerator < Rails::Generator::NamedBase
0
+ default_options :skip_migration => false
0
+ # include Rails::Generator::Commands::Base
0
+ attr_reader :controller_name,
0
+ :controller_class_path,
0
+ :controller_file_path,
0
+ :controller_class_nesting,
0
+ :controller_class_nesting_depth,
0
+ :controller_class_name,
0
+ :controller_singular_name,
0
+ :controller_plural_name,
0
+ :default_file_extension
0
+ alias_method :controller_file_name, :controller_singular_name
0
+ alias_method :controller_table_name, :controller_plural_name
0
+ def initialize(runtime_args, runtime_options = {})
0
+ @controller_name = @name.pluralize
0
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
0
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
0
+ if @controller_class_nesting.empty?
0
+ @controller_class_name = @controller_class_name_without_nesting
0
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
0
+ # ActionView::Base::DEFAULT_TEMPLATE_HANDLER_PREFERENCE.unshift(:haml)
0
+ # if ActionView::Base.const_defined?('DEFAULT_TEMPLATE_HANDLER_PREFERENCE') &&
0
+ # ActionView::Base::DEFAULT_TEMPLATE_HANDLER_PREFERENCE.include?(:haml) then
0
+ @resource_generator = "rspec_haml_scaffold"
0
+ @default_file_extension = "haml"
0
+ @resource_edit_path = "/edit"
0
+ # @resource_generator = "scaffold_resource"
0
+ # @default_file_extension = "rhtml"
0
+ # @resource_edit_path = ";edit"
0
+ #just so you can see what the variables are
0
+ #p @controller_name # yoda/bobs
0
+ #p @controller_class_name_without_nesting # Bobs
0
+ #p @controller_class_nesting # yoda
0
+ #p @controller_plural_name #bobs
0
+ #p @controller_singular_name #bobs
0
+ #p @controller_file_path #yoda/bobs
0
+ #p @controller_class_path # ["yoda"]
0
+ # Check for class naming collisions.
0
+ m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
0
+ m.class_collisions(class_path, "#{class_name}")
0
+ # Controller, helper, views, and spec directories.
0
+ m.directory(File.join('app/models'))
0
+ m.directory(File.join('app/controllers', controller_class_path))
0
+ m.directory(File.join('app/helpers', controller_class_path))
0
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
0
+ m.directory(File.join('spec/controllers', controller_class_path))
0
+ m.directory(File.join('spec/models'))
0
+ m.directory(File.join('spec/helpers', class_path))
0
+ m.directory File.join('spec/fixtures')
0
+ m.directory File.join('spec/views', controller_class_path, controller_file_name)
0
+ # Controller spec, class, and helper.
0
+ m.template 'rspec_haml_scaffold:controller_spec.rb',
0
+ File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
0
+ m.template "rspec_haml_scaffold:controller.rb",
0
+ File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
0
+ m.template 'rspec_haml_scaffold:helper_spec.rb',
0
+ File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb")
0
+ m.template "#{@resource_generator}:helper.rb",
0
+ File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb")
0
+ for action in scaffold_views
0
+ "rspec_haml_scaffold:view_#{action}_haml.erb",
0
+ File.join('app/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}")
0
+ # Model class, unit test, and fixtures.
0
+ m.template 'rspec_haml_scaffold:model.rb', File.join('app/models', "#{@controller_singular_name.singularize}.rb")
0
+ m.template 'model:fixtures.yml', File.join('spec/fixtures', "#{@controller_singular_name}.yml")
0
+ m.template 'rspec_haml_scaffold:model_spec.rb', File.join('spec/models', "#{@controller_singular_name}_spec.rb")
0
+ m.template "rspec_haml_scaffold:edit_haml_spec.rb",
0
+ File.join('spec/views', controller_class_path, controller_file_name, "edit.#{default_file_extension}_spec.rb")
0
+ m.template "rspec_haml_scaffold:index_haml_spec.rb",
0
+ File.join('spec/views', controller_class_path, controller_file_name, "index.#{default_file_extension}_spec.rb")
0
+ m.template "rspec_haml_scaffold:new_haml_spec.rb",
0
+ File.join('spec/views', controller_class_path, controller_file_name, "new.#{default_file_extension}_spec.rb")
0
+ m.template "rspec_haml_scaffold:show_haml_spec.rb",
0
+ File.join('spec/views', controller_class_path, controller_file_name, "show.#{default_file_extension}_spec.rb")
0
+ unless options[:skip_migration]
0
+ 'rspec_haml_scaffold:migration.rb', 'db/migrate',
0
+ :migration_name => "Create#{singular_name.pluralize.capitalize}",
0
+ :attributes => attributes
0
+ :migration_file_name => "create_#{controller_singular_name.gsub(/\//, '_').pluralize}"
0
+ #m.route_resources controller_file_name
0
+ def form_link_for(table_name, singular_name)
0
+ if !@controller_name.split("/")[1].nil?
0
+ return "[:#{@controller_class_nesting.downcase}, @#{singular_name.singularize}]"
0
+ return "@#{singular_name.singularize}"
0
+ def path_for(singular, plural, txt)
0
+ return "#{table_name.singularize}_path(@#{singular_name.singularize})"
0
+ return "edit_#{table_name.singularize}_path(@#{singular_name.singularize})"
0
+ return "#{table_name.singularize}_path(@#{singular_name.singularize}), :confirm => 'Are you sure?', :method => :delete"
0
+ return "#{table_name}_path"
0
+ # Override with your own usage banner.
0
+ "Usage: #{$0} rspec_haml_scaffold ModelName [field:type field:type]"
0
+ opt.separator 'Options:'
0
+ opt.on("--skip-migration",
0
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
0
+ %w[ index show new edit ]
0
+ def route_resources(resource)
0
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
0
+ logger.route "map.resources #{resource}"
0
+ unless options[:pretend]
0
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
0
+ if !resource.split('/')[1].nil?
0
+ one = resource.split('/')[0]
0
+ two = resource.split('/')[1]
0
+ "#{match}\n map.namespace(:#{one}) do |#{one}|\n #{one}.resources :#{two.pluralize}\n end"
0
+ "#{match}\n map.resources :#{resource.pluralize}\n"
0
+ def gsub_file(relative_destination, regexp, *args, &block)
0
+ path = destination_path(relative_destination)
0
+ content = File.read(path).gsub(regexp, *args, &block)
0
+ File.open(path, 'wb') { |file| file.write(content) }
0
+ class GeneratedAttribute
0
+ @default_value ||= case type
0
+ when :int, :integer then "\"1\""
0
+ when :float then "\"1.5\""
0
+ when :decimal then "\"9.99\""
0
+ when :datetime, :timestamp, :time then "Time.now"
0
+ when :date then "Date.today"
0
+ when :string then "\"MyString\""
0
+ when :text then "\"MyText\""
0
+ when :boolean then "false"
0
+ @input_type ||= case type
0
+ when :text then "textarea"