From 3a4a708d51a099afd153b4cc3e09b1037d901eb3 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 26 May 2012 08:50:32 +0000 Subject: [PATCH] Fixed that plugin model generator does not generate the migration (#11024). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9718 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- .../redmine_plugin_model_generator.rb | 26 +++++++++++++++++-- .../templates/migration.rb | 15 +++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 lib/generators/redmine_plugin_model/templates/migration.rb diff --git a/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb b/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb index 01ce31b1d..2e5f03108 100644 --- a/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb +++ b/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb @@ -1,6 +1,12 @@ class RedminePluginModelGenerator < Rails::Generators::NamedBase + source_root File.expand_path("../templates", __FILE__) argument :model, :type => :string + argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" + class_option :migration, :type => :boolean + class_option :timestamps, :type => :boolean + class_option :parent, :type => :string, :desc => "The parent class for the generated model" + class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns" attr_reader :plugin_path, :plugin_name, :plugin_pretty_name @@ -10,10 +16,26 @@ class RedminePluginModelGenerator < Rails::Generators::NamedBase @plugin_pretty_name = plugin_name.titleize @plugin_path = "plugins/#{plugin_name}" @model_class = model.camelize + @table_name = @model_class.tableize + @migration_filename = "create_#{@table_name}" + @migration_class_name = @migration_filename.camelize end def copy_templates - template 'model.rb.erb', "#{plugin_path}/app/models/#{model}.rb" - template 'unit_test.rb.erb', "#{plugin_path}/test/unit/#{model}_test.rb" + template 'model.rb.erb', "#{plugin_path}/app/models/#{model.underscore}.rb" + template 'unit_test.rb.erb', "#{plugin_path}/test/unit/#{model.underscore}_test.rb" + + migration_filename = "%03i_#{@migration_filename}.rb" % (migration_number + 1) + template "migration.rb", "#{plugin_path}/db/migrate/#{migration_filename}" + end + + def attributes_with_index + attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) } + end + + def migration_number + current = Dir.glob("#{plugin_path}/db/migrate/*.rb").map do |file| + File.basename(file).split("_").first.to_i + end.max.to_i end end diff --git a/lib/generators/redmine_plugin_model/templates/migration.rb b/lib/generators/redmine_plugin_model/templates/migration.rb new file mode 100644 index 000000000..f5026aebf --- /dev/null +++ b/lib/generators/redmine_plugin_model/templates/migration.rb @@ -0,0 +1,15 @@ +class <%= @migration_class_name %> < ActiveRecord::Migration + def change + create_table :<%= @table_name %> do |t| +<% attributes.each do |attribute| -%> + t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %> +<% end -%> +<% if options[:timestamps] %> + t.timestamps +<% end -%> + end +<% attributes_with_index.each do |attribute| -%> + add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> +<% end -%> + end +end