annotate .svn/pristine/3a/3abfa86ad3c57ab24eae53ec4cf3ec382c0f6086.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 class RedminePluginModelGenerator < Rails::Generators::NamedBase
Chris@1295 2
Chris@1295 3 source_root File.expand_path("../templates", __FILE__)
Chris@1295 4 argument :model, :type => :string
Chris@1295 5 argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
Chris@1295 6 class_option :migration, :type => :boolean
Chris@1295 7 class_option :timestamps, :type => :boolean
Chris@1295 8 class_option :parent, :type => :string, :desc => "The parent class for the generated model"
Chris@1295 9 class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
Chris@1295 10
Chris@1295 11 attr_reader :plugin_path, :plugin_name, :plugin_pretty_name
Chris@1295 12
Chris@1295 13 def initialize(*args)
Chris@1295 14 super
Chris@1295 15 @plugin_name = file_name.underscore
Chris@1295 16 @plugin_pretty_name = plugin_name.titleize
Chris@1295 17 @plugin_path = "plugins/#{plugin_name}"
Chris@1295 18 @model_class = model.camelize
Chris@1295 19 @table_name = @model_class.tableize
Chris@1295 20 @migration_filename = "create_#{@table_name}"
Chris@1295 21 @migration_class_name = @migration_filename.camelize
Chris@1295 22 end
Chris@1295 23
Chris@1295 24 def copy_templates
Chris@1295 25 template 'model.rb.erb', "#{plugin_path}/app/models/#{model.underscore}.rb"
Chris@1295 26 template 'unit_test.rb.erb', "#{plugin_path}/test/unit/#{model.underscore}_test.rb"
Chris@1295 27
Chris@1295 28 migration_filename = "%03i_#{@migration_filename}.rb" % (migration_number + 1)
Chris@1295 29 template "migration.rb", "#{plugin_path}/db/migrate/#{migration_filename}"
Chris@1295 30 end
Chris@1295 31
Chris@1295 32 def attributes_with_index
Chris@1295 33 attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
Chris@1295 34 end
Chris@1295 35
Chris@1295 36 def migration_number
Chris@1295 37 current = Dir.glob("#{plugin_path}/db/migrate/*.rb").map do |file|
Chris@1295 38 File.basename(file).split("_").first.to_i
Chris@1295 39 end.max.to_i
Chris@1295 40 end
Chris@1295 41 end