redmine_plugin_model_generator.patch

Javier Menéndez Rizo, 2017-11-27 22:11

Download (2.89 KB)

View differences:

lib/generators/redmine_plugin_model/templates/model.rb.erb (revision )
1
class <%= @model_class %> < ActiveRecord::Base
1
class <%= @model_class %> < <%= parent_class_name.classify %>
2 2
  unloadable
3 3
end
lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb (revision )
1 1
class RedminePluginModelGenerator < Rails::Generators::NamedBase
2
  
2

  
3 3
  source_root File.expand_path("../templates", __FILE__)
4 4
  argument :model, :type => :string
5 5
  argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
6
  class_option :migration,  :type => :boolean
6
  class_option :migration, :type => :boolean, :default => true
7 7
  class_option :timestamps, :type => :boolean
8
  class_option :parent,     :type => :string, :desc => "The parent class for the generated model"
9
  class_option :indexes,    :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
8
  class_option :parent, :type => :string, :desc => "The parent class for the generated model"
9
  class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
10 10

  
11 11
  attr_reader :plugin_path, :plugin_name, :plugin_pretty_name
12 12

  
......
24 24
  def copy_templates
25 25
    template 'model.rb.erb', "#{plugin_path}/app/models/#{model.underscore}.rb"
26 26
    template 'unit_test.rb.erb', "#{plugin_path}/test/unit/#{model.underscore}_test.rb"
27
    
28
    migration_filename = "%03i_#{@migration_filename}.rb" % (migration_number + 1)
27

  
28
    return unless options[:migration]
29
    migration_filename = "%.14d_#{@migration_filename}.rb" % migration_number
29 30
    template "migration.rb", "#{plugin_path}/db/migrate/#{migration_filename}"
30 31
  end
31 32

  
33
  private
34

  
32 35
  def attributes_with_index
33
    attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
36
    attributes.select {|a| a.has_index? || (a.reference? && options[:indexes])}
34 37
  end
35 38

  
36 39
  def migration_number
37 40
    current = Dir.glob("#{plugin_path}/db/migrate/*.rb").map do |file|
38 41
      File.basename(file).split("_").first.to_i
39 42
    end.max.to_i
43

  
44
    [current + 1, Time.now.utc.strftime("%Y%m%d%H%M%S").to_i].max
45
  end
46

  
47
  def parent_class_name
48
    options[:parent] || "ActiveRecord::Base"
40 49
  end
41 50
end