To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 3a / 3abfa86ad3c57ab24eae53ec4cf3ec382c0f6086.svn-base @ 1298:4f746d8966dd
History | View | Annotate | Download (1.62 KB)
| 1 |
class RedminePluginModelGenerator < Rails::Generators::NamedBase |
|---|---|
| 2 |
|
| 3 |
source_root File.expand_path("../templates", __FILE__)
|
| 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" |
| 10 |
|
| 11 |
attr_reader :plugin_path, :plugin_name, :plugin_pretty_name |
| 12 |
|
| 13 |
def initialize(*args) |
| 14 |
super |
| 15 |
@plugin_name = file_name.underscore |
| 16 |
@plugin_pretty_name = plugin_name.titleize |
| 17 |
@plugin_path = "plugins/#{plugin_name}"
|
| 18 |
@model_class = model.camelize |
| 19 |
@table_name = @model_class.tableize |
| 20 |
@migration_filename = "create_#{@table_name}"
|
| 21 |
@migration_class_name = @migration_filename.camelize |
| 22 |
end |
| 23 |
|
| 24 |
def copy_templates |
| 25 |
template 'model.rb.erb', "#{plugin_path}/app/models/#{model.underscore}.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 |
| 40 |
end |
| 41 |
end |