Class: Gaku::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/gaku/install/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_pathsObject



15
16
17
18
19
20
21
# File 'lib/generators/gaku/install/install_generator.rb', line 15

def self.source_paths
  paths = superclass.source_paths
  paths << File.expand_path('../templates', "../../#{__FILE__}")
  paths << File.expand_path('../templates', "../#{__FILE__}")
  paths << File.expand_path('../templates', __FILE__)
  paths.flatten
end

Instance Method Details

#completeObject



144
145
146
147
148
149
150
151
# File 'lib/generators/gaku/install/install_generator.rb', line 144

def complete
  unless options[:quiet]
    puts '*' * 50
    puts "Gaku has been installed successfully. You're all ready to go!"
    puts ' '
    puts 'Enjoy!'
  end
end

#configure_applicationObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/generators/gaku/install/install_generator.rb', line 66

def configure_application
  application <<-APP

config.to_prepare do
  # Load application's model / class injectors
  Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_injector*.rb")) do |c|
    Rails.configuration.cache_classes ? require(c) : load(c)
  end

  # Load application's view overrides
  Dir.glob(File.join(File.dirname(__FILE__), "../app/overrides/*.rb")) do |c|
    Rails.configuration.cache_classes ? require(c) : load(c)
  end
end
  APP

  append_file 'config/environment.rb', "\nActiveRecord::Base.include_root_in_json = true\n"
end

#create_databaseObject



99
100
101
102
103
104
105
106
# File 'lib/generators/gaku/install/install_generator.rb', line 99

def create_database
  say_status :creating, 'database'
  silence_stream(STDOUT) do
    silence_stream(STDERR) do
      silence_warnings { rake 'db:create', env: @env }
    end
  end
end

#create_overrides_directoryObject



61
62
63
# File 'lib/generators/gaku/install/install_generator.rb', line 61

def create_overrides_directory
  empty_directory 'app/overrides'
end

#include_seed_dataObject



85
86
87
88
89
90
# File 'lib/generators/gaku/install/install_generator.rb', line 85

def include_seed_data
  append_file 'db/seeds.rb', <<-SEEDS
\n
Gaku::Core::Engine.load_seed if defined?(Gaku::Core)
  SEEDS
end

#install_migrationsObject



92
93
94
95
96
97
# File 'lib/generators/gaku/install/install_generator.rb', line 92

def install_migrations
  say_status :copying, 'migrations'
  silence_stream(STDOUT) do
    silence_warnings { rake 'railties:install:migrations' }
  end
end

#notify_about_routesObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/generators/gaku/install/install_generator.rb', line 127

def notify_about_routes
  if File.readlines(File.join('config', 'routes.rb')).grep(/mount Gaku::Core::Engine/).any?
    say_status :skipping, "route Gaku::Core::Engine already present."
  else
    insert_into_file File.join('config', 'routes.rb'), after: "Application.routes.draw do\n" do
      %Q{ mount Gaku::Core::Engine, at: '/' }
    end

    unless options[:quiet]
      puts '*' * 50
      puts "We added the following line to your application's config/routes.rb file:"
      puts ' '
      puts "    mount Gaku::Core::Engine, at: '/'"
    end
  end
end

#populate_seed_dataObject



117
118
119
120
121
122
123
124
125
# File 'lib/generators/gaku/install/install_generator.rb', line 117

def populate_seed_data
  if @load_seed_data
    say_status :loading,  'seed data'
    cmd = lambda { rake "db:seed", env: @env }
    cmd.call
  else
    say_status :skipping, 'seed data (you can always run rake db:seed)'
  end
end

#prepare_optionsObject



23
24
25
26
27
28
29
30
31
# File 'lib/generators/gaku/install/install_generator.rb', line 23

def prepare_options
  @env = options[:env]
  @run_migrations = options[:migrate]
  @load_seed_data = options[:seed]

  unless @run_migrations
     @load_seed_data = false
  end
end

#remove_unneeded_filesObject



33
34
35
# File 'lib/generators/gaku/install/install_generator.rb', line 33

def remove_unneeded_files
  remove_file 'public/index.html'
end

#run_migrationsObject



108
109
110
111
112
113
114
115
# File 'lib/generators/gaku/install/install_generator.rb', line 108

def run_migrations
  if @run_migrations
    say_status :running, 'migrations'
    rake 'db:migrate', env: @env
  else
    say_status :skipping, "migrations (don't forget to run rake db:migrate)"
  end
end

#setup_assetsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/generators/gaku/install/install_generator.rb', line 37

def setup_assets
  @lib_name = 'gaku'
  %w{javascripts stylesheets images}.each do |path|
    empty_directory "app/assets/#{path}/gaku/frontend" if defined? Gaku::Frontend || Rails.env.test?
    empty_directory "app/assets/#{path}/gaku/admin" if defined? Gaku::Admin || Rails.env.test?
    empty_directory "app/assets/#{path}/gaku/archive" if defined? Gaku::Archive || Rails.env.test?
  end

  if defined? Gaku::Frontend || Rails.env.test?
    template "app/assets/javascripts/gaku/frontend/all.js"
    template "app/assets/stylesheets/gaku/frontend/all.css"
  end

  if defined? Gaku::Admin || Rails.env.test?
    template "app/assets/javascripts/gaku/admin/all.js"
    template "app/assets/stylesheets/gaku/admin/all.css"
  end

  if defined? Gaku::Archive || Rails.env.test?
    template "app/assets/javascripts/gaku/archive/all.js"
    template "app/assets/stylesheets/gaku/archive/all.css"
  end
end