Class: VisitCard::Generators::VcardGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/visit_card/vcard/vcard_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(path) ⇒ Object



18
19
20
# File 'lib/generators/visit_card/vcard/vcard_generator.rb', line 18

def self.next_migration_number(path)
  Time.now.utc.strftime("%Y%m%d%H%M%S")
end

.orm_has_migration?Boolean

TODO routes??

Returns:

  • (Boolean)


14
15
16
# File 'lib/generators/visit_card/vcard/vcard_generator.rb', line 14

def self.orm_has_migration?
  Rails::Generators.options[:rails][:orm] == :active_record
end

Instance Method Details

#add_vcard_routesObject



68
69
70
# File 'lib/generators/visit_card/vcard/vcard_generator.rb', line 68

def add_vcard_routes
  # route "vcard_for :#{table_name}"
end

#copy_migration_templateObject



63
64
65
66
# File 'lib/generators/visit_card/vcard/vcard_generator.rb', line 63

def copy_migration_template
  return unless options.migration?
  migration_template "migration.rb", "db/migrate/create_#{table_name}_tables"
end

#inject_vcard_config_into_modelObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/visit_card/vcard/vcard_generator.rb', line 43

def inject_vcard_config_into_model
  vcard_class_setup = <<-CONTENT

  include VisitCard::Models::Vcard

CONTENT

  case options[:orm].to_s
  when "mongoid"
    inject_into_file model_path, vcard_class_setup, :after => "include Mongoid::Document\n"
  when "data_mapper"
    inject_into_file model_path, vcard_class_setup, :after => "include DataMapper::Resource\n"
  when "active_record"
    inject_into_class model_path, class_name, vcard_class_setup + <<-CONTENT
  # Setup accessible (or protected) attributes for your model TODO
  # attr_accessible :given_name, :last_name, :additional_name
CONTENT
  end
end

#invoke_orm_modelObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/visit_card/vcard/vcard_generator.rb', line 25

def invoke_orm_model
  return unless behavior == :invoke

  if model_exists?
    say "* Model already exists. Adding Vcard behavior."
  elsif options[:orm].present?
    invoke "model", [name], :migration => false, :orm => options[:orm]

    unless model_exists?
      abort "Tried to invoke the model generator for '#{options[:orm]}' but could not find it.\n" <<
        "Please create your model by hand before calling `rails g vcard #{name}`."
    end
  else
    abort "Cannot create a vcard model because config.generators.orm is blank.\n" <<
      "Please create your model by hand or configure your generators orm before calling `rails g vcard #{name}`."
  end
end