@@ -0,0 +1,15 | |||||
|
1 | class <%= @migration_class_name %> < ActiveRecord::Migration | |||
|
2 | def change | |||
|
3 | create_table :<%= @table_name %> do |t| | |||
|
4 | <% attributes.each do |attribute| -%> | |||
|
5 | t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %> | |||
|
6 | <% end -%> | |||
|
7 | <% if options[:timestamps] %> | |||
|
8 | t.timestamps | |||
|
9 | <% end -%> | |||
|
10 | end | |||
|
11 | <% attributes_with_index.each do |attribute| -%> | |||
|
12 | add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %> | |||
|
13 | <% end -%> | |||
|
14 | end | |||
|
15 | end |
@@ -1,6 +1,12 | |||||
1 | class RedminePluginModelGenerator < Rails::Generators::NamedBase |
|
1 | class RedminePluginModelGenerator < Rails::Generators::NamedBase | |
|
2 | ||||
2 | source_root File.expand_path("../templates", __FILE__) |
|
3 | source_root File.expand_path("../templates", __FILE__) | |
3 | argument :model, :type => :string |
|
4 | argument :model, :type => :string | |
|
5 | argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" | |||
|
6 | class_option :migration, :type => :boolean | |||
|
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" | |||
4 |
|
10 | |||
5 | attr_reader :plugin_path, :plugin_name, :plugin_pretty_name |
|
11 | attr_reader :plugin_path, :plugin_name, :plugin_pretty_name | |
6 |
|
12 | |||
@@ -10,10 +16,26 class RedminePluginModelGenerator < Rails::Generators::NamedBase | |||||
10 | @plugin_pretty_name = plugin_name.titleize |
|
16 | @plugin_pretty_name = plugin_name.titleize | |
11 | @plugin_path = "plugins/#{plugin_name}" |
|
17 | @plugin_path = "plugins/#{plugin_name}" | |
12 | @model_class = model.camelize |
|
18 | @model_class = model.camelize | |
|
19 | @table_name = @model_class.tableize | |||
|
20 | @migration_filename = "create_#{@table_name}" | |||
|
21 | @migration_class_name = @migration_filename.camelize | |||
13 | end |
|
22 | end | |
14 |
|
23 | |||
15 | def copy_templates |
|
24 | def copy_templates | |
16 | template 'model.rb.erb', "#{plugin_path}/app/models/#{model}.rb" |
|
25 | template 'model.rb.erb', "#{plugin_path}/app/models/#{model.underscore}.rb" | |
17 | template 'unit_test.rb.erb', "#{plugin_path}/test/unit/#{model}_test.rb" |
|
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) | |||
|
29 | template "migration.rb", "#{plugin_path}/db/migrate/#{migration_filename}" | |||
|
30 | end | |||
|
31 | ||||
|
32 | def attributes_with_index | |||
|
33 | attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) } | |||
|
34 | end | |||
|
35 | ||||
|
36 | def migration_number | |||
|
37 | current = Dir.glob("#{plugin_path}/db/migrate/*.rb").map do |file| | |||
|
38 | File.basename(file).split("_").first.to_i | |||
|
39 | end.max.to_i | |||
18 | end |
|
40 | end | |
19 | end |
|
41 | end |
General Comments 0
You need to be logged in to leave comments.
Login now