Class: RedminePluginModelGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb

Overview

Since:

  • 0.8.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RedminePluginModelGenerator

Returns a new instance of RedminePluginModelGenerator



13
14
15
16
17
18
19
20
21
22
# File 'lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb', line 13

def initialize(*args)
  super
  @plugin_name = file_name.underscore
  @plugin_pretty_name = plugin_name.titleize
  @plugin_path = File.join(Redmine::Plugin.directory, plugin_name)
  @model_class = model.camelize
  @table_name = @model_class.tableize
  @migration_filename = "create_#{@table_name}"
  @migration_class_name = @migration_filename.camelize
end

Instance Attribute Details

#plugin_nameObject (readonly)

Returns the value of attribute plugin_name



11
12
13
# File 'lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb', line 11

def plugin_name
  @plugin_name
end

#plugin_pathObject (readonly)

Returns the value of attribute plugin_path



11
12
13
# File 'lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb', line 11

def plugin_path
  @plugin_path
end

#plugin_pretty_nameObject (readonly)

Returns the value of attribute plugin_pretty_name



11
12
13
# File 'lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb', line 11

def plugin_pretty_name
  @plugin_pretty_name
end

Instance Method Details

#attributes_with_indexObject

Since:

  • 2.0.1



32
33
34
# File 'lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb', line 32

def attributes_with_index
  attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
end

#copy_templatesObject

Since:

  • 2.0.0



24
25
26
27
28
29
30
# File 'lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb', line 24

def copy_templates
  template 'model.rb.erb', "#{plugin_path}/app/models/#{model.underscore}.rb"
  template 'unit_test.rb.erb', "#{plugin_path}/test/unit/#{model.underscore}_test.rb"
  
  migration_filename = "%03i_#{@migration_filename}.rb" % (migration_number + 1)
  template "migration.rb", "#{plugin_path}/db/migrate/#{migration_filename}"
end

#migration_numberObject

Since:

  • 2.0.1



36
37
38
39
40
# File 'lib/generators/redmine_plugin_model/redmine_plugin_model_generator.rb', line 36

def migration_number
  current = Dir.glob("#{plugin_path}/db/migrate/*.rb").map do |file|
    File.basename(file).split("_").first.to_i
  end.max.to_i
end