Class: ScaffyGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/scaffy/scaffy_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



93
94
95
# File 'lib/generators/scaffy/scaffy_generator.rb', line 93

def next_migration_number(dirname)
  ActiveRecord::Generators::Base.next_migration_number(dirname)
end

Instance Method Details

#add_gemsObject



83
84
85
86
87
# File 'lib/generators/scaffy/scaffy_generator.rb', line 83

def add_gems
  gem 'haml'
  gem 'shoulda', :env => :test
  gem 'factory_girl_rails', :env => :test
end

#add_routesObject



30
31
32
33
34
35
36
37
38
# File 'lib/generators/scaffy/scaffy_generator.rb', line 30

def add_routes
  if namespace.present?
    # add namespace route
    route "namespace :#{namespace.downcase} do \n resources :#{object_plural} \nend"
  else
    # add standard route
    route "resources :#{object_plural}"
  end
end

#remove_indexObject



79
80
81
# File 'lib/generators/scaffy/scaffy_generator.rb', line 79

def remove_index
  remove_file "public/index.html"
end

#render_controllerObject



16
17
18
# File 'lib/generators/scaffy/scaffy_generator.rb', line 16

def render_controller
  template "controller.rb", "app/controllers" + namespace_directory + "/#{controller_filename}.rb"
end

#render_helperObject



26
27
28
# File 'lib/generators/scaffy/scaffy_generator.rb', line 26

def render_helper
  template "helper.rb", "app/helpers/#{object_plural}_helper.rb"
end

#render_layoutObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/scaffy/scaffy_generator.rb', line 55

def render_layout
  if options[:layout]
    copy_file "views/haml/layout.html.haml", "app/views/layouts/application.html.haml"
    copy_file "assets/stylesheets/scaffy.css", "public/stylesheets/scaffy.css"
    copy_file "assets/images/alertbad_icon.gif", "public/images/scaffy/alertbad_icon.gif"
    copy_file "assets/images/alertgood_icon.gif", "public/images/scaffy/alertgood_icon.gif"
    copy_file "assets/images/glossy-error-icon.jpg", "public/images/scaffy/glossy-error-icon.gif"
    
    inject_into_module "app/helpers/application_helper.rb", ApplicationHelper do
      %(
      # Returns a div for each key passed if there's a flash
      # with that key
      def flash_div *keys
        divs = keys.select { |k| flash[k] }.collect do |k|
          content_tag :div, flash[k], :class => "flash " + k.to_s
        end
        divs.join.html_safe
      end
      )
    end
    
  end
end

#render_migrationObject



40
41
42
# File 'lib/generators/scaffy/scaffy_generator.rb', line 40

def render_migration
  migration_template "migration.rb", "db/migrate/create_#{object_plural}.rb"
end

#render_modelObject



44
45
46
# File 'lib/generators/scaffy/scaffy_generator.rb', line 44

def render_model
  template "model.rb", "app/models/#{object_singular}.rb"
end

#render_testsObject



48
49
50
51
52
53
# File 'lib/generators/scaffy/scaffy_generator.rb', line 48

def render_tests
  # need to create factories directory if it doesnt exist?
  template "test/factory_girl/factory.rb", "test/factories/#{object_singular}.rb"
  template "test/shoulda/model_test.rb", "test/unit/#{object_singular}_test.rb"
  template "test/shoulda/controller_test.rb", "test/functional" +  namespace_directory +  "/#{object_plural}_controller_test.rb"
end

#render_viewsObject



20
21
22
23
24
# File 'lib/generators/scaffy/scaffy_generator.rb', line 20

def render_views
  %w(_form index new show).each do |t|
    template "views/haml/#{t}.html.haml", "app/views" + namespace_directory + "/#{object_plural}/#{t}.html.haml"
  end
end