Sha256: 2b88a5cd24b22486c568f732ddb3f1c0fc992f9583bbf7ddc677db4a4c1c7a83

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

class JfsModelGenerator < Rails::Generator::Base

  attr_accessor :name, :attributes
  
  def initialize(runtime_args, runtime_options = {})
    super
    usage if @args.empty?
    
    @name = @args.first
    @attributes = []

    @args[1..-1].each do |arg|
      @attributes << Rails::Generator::GeneratedAttribute.new(*arg.split(":"))
    end
    @attributes.uniq!
  end
 
  def manifest
    record do |m|
      m.directory "app/models"
      m.template "model.rb", "app/models/#{singular_name}.rb"
      m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{plural_name}"
    end
  end
  
  def singular_name
    name.underscore.singularize
  end
  
  def plural_name
    name.underscore.pluralize
  end
  
  def class_name
    singular_name.camelize
  end
  
  def plural_class_name
    plural_name.camelize
  end
  
  def gen_attr_accessible(attribute)
    case attribute.type
    when :belongs_to
      "attr_accessible :#{attribute.name}_ids"
    else
      "attr_accessible :#{attribute.name}"
    end
  end

  protected

  def banner
    "Usage: #{$0} #{spec.name} ModelName [field:type, field:type]"
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jfs-generators-0.2.0 rails_generators/jfs_model/jfs_model_generator.rb
jfs-generators-0.2.4 rails_generators/jfs_model/jfs_model_generator.rb
jfs-generators-0.2.3 rails_generators/jfs_model/jfs_model_generator.rb