You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redmine_plugin_model_generator.rb 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class RedminePluginModelGenerator < Rails::Generators::NamedBase
  19. source_root File.expand_path("../templates", __FILE__)
  20. argument :model, :type => :string
  21. argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
  22. class_option :migration, :type => :boolean, :default => true
  23. class_option :timestamps, :type => :boolean
  24. class_option :parent, :type => :string, :desc => "The parent class for the generated model"
  25. class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
  26. attr_reader :plugin_path, :plugin_name, :plugin_pretty_name
  27. def initialize(*args)
  28. super
  29. @plugin_name = file_name.underscore
  30. @plugin_pretty_name = plugin_name.titleize
  31. @plugin_path = File.join(Redmine::Plugin.directory, plugin_name)
  32. @model_class = model.camelize
  33. @table_name = @model_class.tableize
  34. @migration_filename = "create_#{@table_name}"
  35. @migration_class_name = @migration_filename.camelize
  36. end
  37. def copy_templates
  38. template 'model.rb.erb', "#{plugin_path}/app/models/#{model.underscore}.rb"
  39. template 'unit_test.rb.erb', "#{plugin_path}/test/unit/#{model.underscore}_test.rb"
  40. return unless options[:migration]
  41. migration_filename = "%.14d_#{@migration_filename}.rb" % migration_number
  42. template "migration.rb", "#{plugin_path}/db/migrate/#{migration_filename}"
  43. end
  44. private
  45. def attributes_with_index
  46. attributes.select {|a| a.has_index? || (a.reference? && options[:indexes])}
  47. end
  48. def migration_number
  49. current = Dir.glob("#{plugin_path}/db/migrate/*.rb").map do |file|
  50. File.basename(file).split("_").first.to_i
  51. end.max.to_i
  52. [current + 1, Time.now.utc.strftime("%Y%m%d%H%M%S").to_i].max
  53. end
  54. def parent_class_name
  55. options[:parent] || "ApplicationRecord"
  56. end
  57. end