Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3098

Show
Ignore:
Timestamp:
11/20/05 04:21:06 (3 years ago)
Author:
bitsweat
Message:

Introducing the session_migration generator. Creates an add_session_table migration. References #2958.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r3091 r3098  
    11*SVN* 
     2 
     3* Introducing the session_migration generator.  Creates an add_session_table migration.  #2958 [Rick Olson] 
    24 
    35* Update to Prototype 1.4.0_rc4. Closes #2943 (old Array.prototype.reverse behavior can be obtained by passing false as an argument). [Sam Stephenson] 
  • trunk/railties/lib/rails_generator/commands.rb

    r3081 r3098  
    5656        end 
    5757 
     58        protected 
     59          def existing_migrations(file_name) 
     60            Dir.glob("db/migrate/[0-9]*_#{file_name}.rb") 
     61          end 
     62 
     63          def migration_exists?(file_name) 
     64            not existing_migrations(file_name).empty? 
     65          end 
     66 
     67          def current_migration_number 
     68            Dir.glob('db/migrate/[0-9]*.rb').inject(0) do |max, file_path| 
     69              n = File.basename(file_path).split('_', 2).first.to_i 
     70              if n > max then n else max end 
     71            end 
     72          end 
     73 
     74          def next_migration_number 
     75            current_migration_number + 1 
     76          end 
     77 
     78          def next_migration_string(padding = 3) 
     79            "%.#{padding}d" % next_migration_number 
     80          end 
     81 
    5882        private 
    5983          # Ask the user interactively whether to force collision. 
     
    85109            end_mark = template_part_mark(template_options[:end_mark], template_options[:mark_id]) 
    86110            begin_mark + rendered_part + end_mark 
    87          end 
    88  
    89          def template_part_mark(name, id) 
    90            "<!--[#{name}:#{id}]-->\n" 
     111          end 
     112 
     113          def template_part_mark(name, id) 
     114            "<!--[#{name}:#{id}]-->\n" 
    91115          end 
    92116      end 
     
    278302            puts File.read(source_path(relative_source)) unless options[:pretend] 
    279303          end 
     304        end 
     305 
     306        # When creating a migration, it knows to find the first available file in db/migrate and use the migration.rb template. 
     307        def migration_template(relative_source, relative_destination, template_options = {}) 
     308          raise "Another migration is already named #{file_name}: #{existing_migrations(file_name).first}" if migration_exists?(file_name) 
     309          template(relative_source, "#{relative_destination}/#{next_migration_string}_#{file_name}.rb", template_options) 
    280310        end 
    281311 
     
    386416          # nothing should be done here 
    387417        end 
     418 
     419        # When deleting a migration, it knows to delete every file named "[0-9]*_#{file_name}". 
     420        def migration_template(relative_source, relative_destination, template_options = {}) 
     421          raise "There is no migration named #{file_name}" unless migration_exists?(file_name) 
     422          existing_migrations(file_name).each do |file_path| 
     423            file(relative_source, file_path, template_options) 
     424          end 
     425        end 
    388426      end 
    389427 
     
    417455        def readme(*args) 
    418456          logger.readme args.join(', ') 
     457        end 
     458         
     459        def migration_template(relative_source, relative_destination, options = {}) 
     460          logger.migration_template file_name 
    419461        end 
    420462      end 
  • trunk/railties/lib/rails_generator/generators/components/migration/migration_generator.rb

    r2799 r3098  
    66    end 
    77  end 
    8  
    9   protected 
    10     def existing_migrations(file_name) 
    11       Dir.glob("db/migrate/[0-9]*_#{file_name}.rb") 
    12     end 
    13  
    14     def migration_exists?(file_name) 
    15       not existing_migrations(file_name).empty? 
    16     end 
    17  
    18     def current_migration_number 
    19       Dir.glob('db/migrate/[0-9]*.rb').inject(0) do |max, file_path| 
    20         n = File.basename(file_path).split('_', 2).first.to_i 
    21         if n > max then n else max end 
    22       end 
    23     end 
    24  
    25     def next_migration_number 
    26       current_migration_number + 1 
    27     end 
    28  
    29     def next_migration_string(padding = 3) 
    30       "%.#{padding}d" % next_migration_number 
    31     end 
    328end 
    33  
    34 module Rails::Generator::Commands 
    35   # When creating, it knows to find the first available file in db/migrate and use the migration.rb template. 
    36   class Create 
    37     def migration_template(relative_source, relative_destination, template_options = {}) 
    38       raise "Another migration is already named #{file_name}: #{existing_migrations(file_name).first}" if migration_exists?(file_name) 
    39       template(relative_source, "#{relative_destination}/#{next_migration_string}_#{file_name}.rb", template_options) 
    40     end 
    41   end 
    42  
    43   # When deleting, it knows to delete every file named "[0-9]*_#{file_name}". 
    44   class Destroy 
    45     def migration_template(relative_source, relative_destination, template_options = {}) 
    46       raise "There is no migration named #{file_name}" unless migration_exists?(file_name) 
    47       existing_migrations(file_name).each do |file_path| 
    48         file(relative_source, file_path, template_options) 
    49       end 
    50     end 
    51   end 
    52  
    53   class List 
    54     def migration_template(relative_source, relative_destination, options = {}) 
    55       logger.migration_template file_name 
    56     end 
    57   end 
    58 end