public
Description: A Haml Scaffolding Generator for RSpec
Homepage: http://blog.citrusbyte.com/2008/3/11/what-s-that-rspec-haml-scaffolds-yes-it-is
Clone URL: git://github.com/dfischer/rspec-haml-scaffold-generator.git
Daniel Fischer (author)
Mon Mar 10 15:27:18 -0700 2008
commit  5ef8fb6f10af312059fb5c6f87596260ae9811be
tree    913bbef3e767364255aa706f3ba48c7d14ff1caf
rspec-haml-scaffold-generator / generators / rspec_haml_scaffold / rspec_haml_scaffold_generator.rb
100644 225 lines (189 sloc) 8.722 kb
1
2
3
4
5
6
7
8
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
44
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
class RspecHamlScaffoldGenerator < Rails::Generator::NamedBase
  default_options :skip_migration => false
  # include Rails::Generator::Commands::Base
  
  attr_reader :controller_name,
                :controller_class_path,
                :controller_file_path,
                :controller_class_nesting,
                :controller_class_nesting_depth,
                :controller_class_name,
                :controller_singular_name,
                :controller_plural_name,
                :resource_edit_path,
                :default_file_extension
  alias_method :controller_file_name, :controller_singular_name
  alias_method :controller_table_name, :controller_plural_name
 
  def initialize(runtime_args, runtime_options = {})
    super
 
    @controller_name = @name.pluralize
 
    base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
    @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
 
    if @controller_class_nesting.empty?
      @controller_class_name = @controller_class_name_without_nesting
    else
      @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
    end
    
    # ActionView::Base::DEFAULT_TEMPLATE_HANDLER_PREFERENCE.unshift(:haml)
    #
    # if ActionView::Base.const_defined?('DEFAULT_TEMPLATE_HANDLER_PREFERENCE') &&
    # ActionView::Base::DEFAULT_TEMPLATE_HANDLER_PREFERENCE.include?(:haml) then
      @resource_generator = "rspec_haml_scaffold"
      @default_file_extension = "haml"
      @resource_edit_path = "/edit"
    # else
    # @resource_generator = "scaffold_resource"
    # @default_file_extension = "rhtml"
    # @resource_edit_path = ";edit"
    # end
    
 
  end
 
  def manifest
    record do |m|
      #just so you can see what the variables are
      # => "yoda/bob"
      #p @name # yoda/bob
      #p @controller_name # yoda/bobs
      #p @controller_class_name_without_nesting # Bobs
      #p @controller_class_nesting # yoda
      #p @controller_plural_name #bobs
      #p @controller_singular_name #bobs
      #p @controller_file_path #yoda/bobs
      #p @controller_class_path # ["yoda"]
      
      # Check for class naming collisions.
      m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
      m.class_collisions(class_path, "#{class_name}")
 
      # Controller, helper, views, and spec directories.
      m.directory(File.join('app/models'))
      m.directory(File.join('app/controllers', controller_class_path))
      m.directory(File.join('app/helpers', controller_class_path))
      m.directory(File.join('app/views', controller_class_path, controller_file_name))
      m.directory(File.join('spec/controllers', controller_class_path))
      m.directory(File.join('spec/models'))
      m.directory(File.join('spec/helpers', class_path))
      m.directory File.join('spec/fixtures')
      m.directory File.join('spec/views', controller_class_path, controller_file_name)
      
      # Controller spec, class, and helper.
      m.template 'rspec_haml_scaffold:controller_spec.rb',
        File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
 
      m.template "rspec_haml_scaffold:controller.rb",
        File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
 
      m.template 'rspec_haml_scaffold:helper_spec.rb',
        File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb")
      
      m.template "#{@resource_generator}:helper.rb",
        File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb")
 
      for action in scaffold_views
        m.template(
          "rspec_haml_scaffold:view_#{action}_haml.erb",
          File.join('app/views', controller_class_path, controller_file_name, "#{action}.#{default_file_extension}")
        )
      end
      
      # Model class, unit test, and fixtures.
      m.template 'rspec_haml_scaffold:model.rb', File.join('app/models', "#{@controller_singular_name.singularize}.rb")
      m.template 'model:fixtures.yml', File.join('spec/fixtures', "#{@controller_singular_name}.yml")
      m.template 'rspec_haml_scaffold:model_spec.rb', File.join('spec/models', "#{@controller_singular_name}_spec.rb")
 
      # View specs
      m.template "rspec_haml_scaffold:edit_haml_spec.rb",
        File.join('spec/views', controller_class_path, controller_file_name, "edit.#{default_file_extension}_spec.rb")
      m.template "rspec_haml_scaffold:index_haml_spec.rb",
        File.join('spec/views', controller_class_path, controller_file_name, "index.#{default_file_extension}_spec.rb")
      m.template "rspec_haml_scaffold:new_haml_spec.rb",
        File.join('spec/views', controller_class_path, controller_file_name, "new.#{default_file_extension}_spec.rb")
      m.template "rspec_haml_scaffold:show_haml_spec.rb",
        File.join('spec/views', controller_class_path, controller_file_name, "show.#{default_file_extension}_spec.rb")
 
      unless options[:skip_migration]
        m.migration_template(
          'rspec_haml_scaffold:migration.rb', 'db/migrate',
          :assigns => {
            :migration_name => "Create#{singular_name.pluralize.capitalize}",
            :attributes => attributes
          },
          :migration_file_name => "create_#{controller_singular_name.gsub(/\//, '_').pluralize}"
        )
      end
 
      #m.route_resources controller_file_name
      route_resources name
 
    end
  end
 
  protected
    def form_link_for(table_name, singular_name)
      if !@controller_name.split("/")[1].nil?
        return "[:#{@controller_class_nesting.downcase}, @#{singular_name.singularize}]"
      else
        return "@#{singular_name.singularize}"
      end
    end
    
    def path_for(singular, plural, txt)
      case txt
      when "show"
        return "#{table_name.singularize}_path(@#{singular_name.singularize})"
      when "edit"
        return "edit_#{table_name.singularize}_path(@#{singular_name.singularize})"
      when "destroy"
        return "#{table_name.singularize}_path(@#{singular_name.singularize}), :confirm => 'Are you sure?', :method => :delete"
      when "index"
        return "#{table_name}_path"
      end
    end
    
    # Override with your own usage banner.
    def banner
      "Usage: #{$0} rspec_haml_scaffold ModelName [field:type field:type]"
    end
 
    def add_options!(opt)
      opt.separator ''
      opt.separator 'Options:'
      opt.on("--skip-migration",
             "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
    end
 
    def scaffold_views
     %w[ index show new edit ]
    end
 
    def model_name
      class_name.demodulize
    end
 
    def route_resources(resource)
      sentinel = 'ActionController::Routing::Routes.draw do |map|'
      logger.route "map.resources #{resource}"
      unless options[:pretend]
        gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
        
           if !resource.split('/')[1].nil?
             one = resource.split('/')[0]
             two = resource.split('/')[1]
             "#{match}\n map.namespace(:#{one}) do |#{one}|\n #{one}.resources :#{two.pluralize}\n end"
           else
             "#{match}\n map.resources :#{resource.pluralize}\n"
           end
    
        end
      end
    end
        
    
    def gsub_file(relative_destination, regexp, *args, &block)
      path = destination_path(relative_destination)
      content = File.read(path).gsub(regexp, *args, &block)
      File.open(path, 'wb') { |file| file.write(content) }
    end
              
end
 
module Rails
  module Generator
    class GeneratedAttribute
      def default_value
        @default_value ||= case type
          when :int, :integer then "\"1\""
          when :float then "\"1.5\""
          when :decimal then "\"9.99\""
          when :datetime, :timestamp, :time then "Time.now"
          when :date then "Date.today"
          when :string then "\"MyString\""
          when :text then "\"MyText\""
          when :boolean then "false"
          else
            ""
        end
      end
 
      def input_type
        @input_type ||= case type
          when :text then "textarea"
          else
            "input"
        end
      end
    end
  end
end