diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2019-06-20 14:16:58 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2019-06-20 14:16:58 +0000 |
commit | 7b06ccef21d943d5f9fa57547e43c23e7753b766 (patch) | |
tree | 8722d860d709c10b1496ca10f61baefe8c1a46ef /lib/generators/redmine_plugin_model | |
parent | c4a9d4cd4df130fa84e5c1948fc93d1f2b0bba5a (diff) | |
download | redmine-7b06ccef21d943d5f9fa57547e43c23e7753b766.tar.gz redmine-7b06ccef21d943d5f9fa57547e43c23e7753b766.zip |
redmine_plugin_model_generator improvements (#27659).
Patch by Javier Menéndez Rizo.
git-svn-id: http://svn.redmine.org/redmine/trunk@18298 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/generators/redmine_plugin_model')
-rw-r--r-- | lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb | 13 | ||||
-rw-r--r-- | lib/generators/redmine_plugin_model/templates/model.rb.erb | 2 |
2 files changed, 12 insertions, 3 deletions
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 66daaf792..ed9eb859c 100644 --- a/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb +++ b/lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb @@ -22,7 +22,7 @@ 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 :migration, :type => :boolean, :default => true 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" @@ -44,10 +44,13 @@ class RedminePluginModelGenerator < Rails::Generators::NamedBase 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) + return unless options[:migration] + migration_filename = "%.14d_#{@migration_filename}.rb" % migration_number template "migration.rb", "#{plugin_path}/db/migrate/#{migration_filename}" end + private + def attributes_with_index attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) } end @@ -56,5 +59,11 @@ class RedminePluginModelGenerator < Rails::Generators::NamedBase current = Dir.glob("#{plugin_path}/db/migrate/*.rb").map do |file| File.basename(file).split("_").first.to_i end.max.to_i + + [current + 1, Time.now.utc.strftime("%Y%m%d%H%M%S").to_i].max + end + + def parent_class_name + options[:parent] || "ActiveRecord::Base" end end diff --git a/lib/generators/redmine_plugin_model/templates/model.rb.erb b/lib/generators/redmine_plugin_model/templates/model.rb.erb index cb6f31411..cf9edbbda 100644 --- a/lib/generators/redmine_plugin_model/templates/model.rb.erb +++ b/lib/generators/redmine_plugin_model/templates/model.rb.erb @@ -1,2 +1,2 @@ -class <%= @model_class %> < ActiveRecord::Base +class <%= @model_class %> < <%= parent_class_name.classify %> end |