Class: RtextileGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/rails/generators/rtextile/rtextile_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

:nodoc:



11
12
13
14
15
16
17
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 11

def self.next_migration_number(dirname) #:nodoc:
  if ActiveRecord::Base.timestamped_migrations
    Time.now.utc.strftime("%Y%m%d%H%M%S")
  else
    "%.3d" % (current_migration_number(dirname) + 1)
  end
end

Instance Method Details

#add_RCObject

add require RedCloth to application_controller



67
68
69
70
71
72
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 67

def add_RC      # add require RedCloth to application_controller
 line = "class ApplicationController < ActionController::Base"
 gsub_file 'app/controllers/application_controller.rb', /(#{Regexp.escape(line)})/mi do |match|
 "#{match}\n  require 'RedCloth'\n"
end
end

#add_routeObject



41
42
43
44
45
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 41

def add_route  
  route "resources #{plural_name.to_sym.inspect}" 
  route "match 'download' => '#{plural_name}#download', :as => :download"
  route "match 'download_html' => '#{plural_name}#download_html', :as => :download_html"
end

#class_nameObject



74
75
76
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 74

def class_name
  name.camelize
end

#copy_templatesObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 55

def copy_templates
  template "show.html.erb", "app/views/#{plural_name}/show.html.erb"
  template "_show.html.erb", "app/views/#{plural_name}/_show.html.erb"
  template "_form.html.erb", "app/views/#{plural_name}/_form.html.erb"
  template "edit.html.erb", "app/views/#{plural_name}/edit.html.erb"
  template "index.html.erb", "app/views/#{plural_name}/index.html.erb"
  template "new.html.erb", "app/views/#{plural_name}/new.html.erb"
  template "model.rb", "app/models/#{name}.rb "   
  template "controller.rb", "app/controllers/#{plural_name}_controller.rb"
  
end

#create_helper_fileObject



47
48
49
50
51
52
53
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 47

def create_helper_file
  create_file "app/helpers/#{plural_name}_helper.rb", <<-FILE
  module #{plural_class_name}Helper
  
  end
  FILE
end

#create_migration_fileObject

Every method that is declared below will be automatically executed when the generator is run



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 20

def create_migration_file
  f = File.open File.join(File.dirname(__FILE__), 'templates', 'schema.rb')
  schema = f.read; f.close
  
  schema.gsub!(/ActiveRecord::Schema.*\n/, '')
  schema.gsub!(/^end\n*$/, '')

  f = File.open File.join(File.dirname(__FILE__), 'templates', 'migration.rb')
  migration = f.read; f.close
  migration.gsub!(/SCHEMA_AUTO_INSERTED_HERE/, schema)
  
  tmp = File.open "tmp/~migration_ready.rb", "w"
  tmp.write migration
  tmp.close

  migration_template  '../../../tmp/~migration_ready.rb',
    "db/migrate/create_#{plural_name}.rb "
  remove_file 'tmp/~migration_ready.rb'
end

#plural_class_nameObject



77
78
79
# File 'lib/rails/generators/rtextile/rtextile_generator.rb', line 77

def plural_class_name
  plural_name.camelize
end