Class: Blogelator::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/blogelator/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(path) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/generators/blogelator/install_generator.rb', line 56

def self.next_migration_number(path)
  if ActiveRecord::Base.timestamped_migrations
    sleep 1 # make sure each time we get a different timestamp
    Time.new.utc.strftime("%Y%m%d%H%M%S")
  else
    "%.3d" % (current_migration_number(path) + 1)
  end
end

Instance Method Details

#copy_controllerObject



9
10
11
12
# File 'lib/generators/blogelator/install_generator.rb', line 9

def copy_controller
  posts_controller_path = "app/controllers/blogelator/posts_controller.rb"
  copy_file posts_controller_path, posts_controller_path
end

#copy_filesObject



46
47
48
49
50
51
52
53
54
# File 'lib/generators/blogelator/install_generator.rb', line 46

def copy_files
  # Add interface for Active Admin (Pro)
  # @see http://activeadmin.info
  # @see http://github.com/codelation/activeadmin_pro
  if defined?(ActiveAdmin)
    admin_path = "app/admin/blogelator"
    directory admin_path, admin_path
  end
end

#copy_javascriptsObject



20
21
22
23
24
25
# File 'lib/generators/blogelator/install_generator.rb', line 20

def copy_javascripts
  blog_js_path = "app/assets/javascripts/blog.js"
  copy_file blog_js_path, blog_js_path
  blog_js_dir_path = "app/assets/javascripts/blog"
  directory blog_js_dir_path, blog_js_dir_path
end

#copy_layoutObject



27
28
29
30
31
32
# File 'lib/generators/blogelator/install_generator.rb', line 27

def copy_layout
  blog_layout_path = "app/views/layouts/blog.html.erb"
  copy_file blog_layout_path, blog_layout_path
  blog_layout_dir_path = "app/views/layouts/blog"
  directory blog_layout_dir_path, blog_layout_dir_path
end

#copy_modelsObject



13
14
15
16
17
18
# File 'lib/generators/blogelator/install_generator.rb', line 13

def copy_models
  blogelator_module = "app/models/blogelator.rb"
  blogelator_directory = "app/models/blogelator"
  copy_file blogelator_module, blogelator_module
  directory blogelator_directory, blogelator_directory
end

#copy_stylesheetsObject



34
35
36
37
38
39
# File 'lib/generators/blogelator/install_generator.rb', line 34

def copy_stylesheets
  blog_scss_path = "app/assets/stylesheets/blog.scss"
  copy_file blog_scss_path, blog_scss_path
  blog_scss_dir_path = "app/assets/stylesheets/blog"
  directory blog_scss_dir_path, blog_scss_dir_path
end

#copy_viewsObject



41
42
43
44
# File 'lib/generators/blogelator/install_generator.rb', line 41

def copy_views
  blogelator_views_path = "app/views/blogelator"
  directory blogelator_views_path, blogelator_views_path
end

#install_migrationsObject

Install the database migrations required for Blogelator’s posts



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/blogelator/install_generator.rb', line 79

def install_migrations
  migrations = [
    "create_blogelator_posts.rb",
    "create_blogelator_authors.rb",
    "create_blogelator_tags.rb",
    "create_blogelator_posts_tags.rb",
    "create_blogelator_posts_posts.rb"
  ]
  migration_path = "db/migrate"
  migrations.each do |file|
    migration_template "#{migration_path}/#{file}", "#{migration_path}/#{file}"
  end
end

#setup_routesObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/generators/blogelator/install_generator.rb', line 65

def setup_routes
  route = '
  namespace :blogelator, path: "blog" do
resources :posts, path: "/"
resources :tags do
  resources :posts, path: "/"
end
  end

'
  inject_into_file "config/routes.rb", route, :before => /^end/
end