Class: Golden::Setting::InstallGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/golden/setting/install_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object

argument :name, type: :string, default: ‘setting’



10
11
12
13
# File 'lib/generators/golden/setting/install_generator.rb', line 10

def self.next_migration_number dirname
  next_migration_number = current_migration_number(dirname) + 1
  ActiveRecord::Migration.next_migration_number(next_migration_number)
end

Instance Method Details

#add_locales_pathObject



83
84
85
86
87
88
89
# File 'lib/generators/golden/setting/install_generator.rb', line 83

def add_locales_path
  file = 'config/application.rb'
  sentinel = "# config.i18n.default_locale = :de\n"
  insert_into_file file, after: sentinel do
    "    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]\n"
  end
end

#generate_initializerObject



77
78
79
80
81
# File 'lib/generators/golden/setting/install_generator.rb', line 77

def generate_initializer
  file = 'golden_setting.rb'
  log :initializer, file
  template file, File.join('config/initializers', class_path, file), verbose: false
end

#generate_localesObject



91
92
93
94
95
96
97
# File 'lib/generators/golden/setting/install_generator.rb', line 91

def generate_locales
  files = %w{settings.en.yml settings.zh-TW.yml}
  files.each do |file|
    log :locales, "golden/#{file}"
    template file, File.join('config/locales/golden', class_path, file), verbose: false
  end
end

#generate_migrationObject



15
16
17
18
# File 'lib/generators/golden/setting/install_generator.rb', line 15

def generate_migration
  file = 'create_golden_settings.rb'
  migration_template file, File.join('db/migrate', class_path, file)
end

#generate_modelObject



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

def generate_model
  # generate :model, name, '--skip-migration'
  file = 'setting.rb'
  log :model, file
  template file, File.join('app/models', class_path, "#{file_name}.rb"), verbose: false
end

#inject_javascript_coffeeObject



37
38
39
40
41
42
43
44
45
# File 'lib/generators/golden/setting/install_generator.rb', line 37

def inject_javascript_coffee
  file = 'app/assets/javascripts/application.js.coffee'
  return unless File.exists? file
  log :javascripts, 'application.js.coffee'
  sentinel = "#= require_tree .\n"
  insert_into_file file, before: sentinel, verbose: false do
    "#= require golden/setting/settings\n"
  end
end

#inject_javascript_jsObject



27
28
29
30
31
32
33
34
35
# File 'lib/generators/golden/setting/install_generator.rb', line 27

def inject_javascript_js
  file = 'app/assets/javascripts/application.js'
  return unless File.exists? file
  log :javascripts, 'application.js'
  sentinel = "//= require_tree .\n"
  insert_into_file file, before: sentinel, verbose: false do
    "//= require golden/setting/settings\n"
  end
end

#inject_layoutObject



103
104
105
106
107
108
109
# File 'lib/generators/golden/setting/install_generator.rb', line 103

def inject_layout
  file = 'app/views/layouts/application.html.erb'
  sentinel = "<%= yield %>\n"
  gsub_file file, "<%= yield %>\n" do |match|
    %Q{<div class="<%= controller_name %>"><%= yield %></div>\n}
  end
end

#inject_routesObject



99
100
101
# File 'lib/generators/golden/setting/install_generator.rb', line 99

def inject_routes
  route "mount Golden::Setting::Engine, at: '/'\n"
end

#inject_stylesheet_cssObject



47
48
49
50
51
52
53
54
55
# File 'lib/generators/golden/setting/install_generator.rb', line 47

def inject_stylesheet_css
  file = 'app/assets/stylesheets/application.css'
  return unless File.exists? file
  log :stylesheets, 'application.css'
  sentinel = " *= require_self\n"
  insert_into_file file, before: sentinel, verbose: false do
    " *= require golden/setting/settings\n"
  end
end

#inject_stylesheet_sassObject



57
58
59
60
61
62
63
64
65
# File 'lib/generators/golden/setting/install_generator.rb', line 57

def inject_stylesheet_sass
  file = 'app/assets/stylesheets/application.css.sass'
  return unless File.exists? file
  log :stylesheets, 'application.css.sass'
  sentinel = "//= require_self\n"
  insert_into_file file, before: sentinel, verbose: false do
    "//= require golden/setting/settings\n"
  end
end

#inject_stylesheet_scssObject



67
68
69
70
71
72
73
74
75
# File 'lib/generators/golden/setting/install_generator.rb', line 67

def inject_stylesheet_scss
  file = 'app/assets/stylesheets/application.css.scss'
  return unless File.exists? file
  log :stylesheets, 'application.css.scss'
  sentinel = "//= require_self\n"
  insert_into_file file, before: sentinel, verbose: false do
    "//= require golden/setting/settings\n"
  end
end