Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a generator when setting up Tolk.
- Loading branch information
1 parent
3ad92bf
commit 732c283
Showing
8 changed files
with
89 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'rails/generators' | ||
require File.expand_path('../utils', __FILE__) | ||
|
||
# http://guides.rubyonrails.org/generators.html | ||
# http://rdoc.info/github/wycats/thor/master/Thor/Actions.html | ||
# keep generator idempotent, thanks | ||
# Thanks to https://github.com/sferik/rails_admin ! | ||
|
||
module Tolk | ||
class InstallGenerator < Rails::Generators::Base | ||
|
||
source_root File.expand_path("../templates", __FILE__) | ||
include Rails::Generators::Migration | ||
include Generators::Utils::InstanceMethods | ||
extend Generators::Utils::ClassMethods | ||
|
||
argument :_namespace, :type => :string, :required => false, :desc => "Tolk url namespace" | ||
desc "Tolk installation generator" | ||
def install | ||
routes = File.open(Rails.root.join("config/routes.rb")).try :read | ||
initializer = (File.open(Rails.root.join("config/initializers/tolk.rb")) rescue nil).try :read | ||
|
||
display "Hello, Tolk installer will help you sets things up!", :blue | ||
unless initializer | ||
template "initializer.erb", "config/initializers/tolk.rb" | ||
else | ||
display "You already have a config file. You're updating, heh? I'm generating a new 'tolk.rb.example' that you can review." | ||
template "initializer.erb", "config/initializers/tolk.rb.example" | ||
end | ||
|
||
display "Adding a migration..." | ||
migration_template 'migration.rb', 'db/migrate/create_tolk_tables.rb' rescue display $!.message | ||
|
||
namespace = ask_for("Where do you want to mount tolk?", "tolk", _namespace) | ||
gsub_file "config/routes.rb", /mount Tolk::Engine => \'\/.+\'/, '' | ||
gsub_file "config/routes.rb", /mount Tolk::Engine => \'\/.+\', :as => \'tolk\'/, '' | ||
route("mount Tolk::Engine => '/#{namespace}', :as => 'tolk'") | ||
|
||
display "Job's done: migrate, start your server and visit '/#{namespace}'!", :blue | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# encoding: utf-8 | ||
|
||
# Tolk config file. Generated on <%= DateTime.now.to_s(:long) %> | ||
# See github.com/tolk/tolk for more informations | ||
|
||
Tolk.config do |config| | ||
|
||
# If you need to add a mapping do it like this : | ||
# config.mapping["fr-ES"] = 'Frañol !' | ||
|
||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Tolk | ||
module Generators | ||
module Utils | ||
module InstanceMethods | ||
def display(output, color = :green) | ||
say(" - #{output}", color) | ||
end | ||
|
||
def ask_for(wording, default_value = nil, override_if_present_value = nil) | ||
override_if_present_value.present? ? | ||
display("Using [#{override_if_present_value}] for question '#{wording}'") && override_if_present_value : | ||
ask(" ? #{wording} Press <enter> for [#{default_value}] >", :yellow).presence || default_value | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def next_migration_number(dirname) | ||
if ActiveRecord::Base.timestamped_migrations | ||
migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i | ||
migration_number += 1 | ||
migration_number.to_s | ||
else | ||
"%.3d" % (current_migration_number(dirname) + 1) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters