Skip to content

Commit

Permalink
Adding a generator when setting up Tolk.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdarde committed May 16, 2012
1 parent 3ad92bf commit 732c283
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 27 deletions.
8 changes: 2 additions & 6 deletions README.md
@@ -1,5 +1,5 @@
# Tolk
[![Travis](https://secure.travis-ci.org/tolk/tolk.png?branch=master)](travis-ci.org/#!/tolk/tolk)
[![Travis](https://secure.travis-ci.org/tolk/tolk.png?branch=master)](http://travis-ci.org/#!/tolk/tolk)

[![Dependency Status](https://gemnasium.com/tolk/tolk.png?travis)](https://gemnasium.com/tolk/tolk)

Expand All @@ -21,11 +21,7 @@ To setup just run:
$ rake tolk:setup
```

and add tolk to your `config/routes.rb` file :

```ruby
mount Tolk::Engine => "/tolk"
```
and follow the guide !

## Usage

Expand Down
43 changes: 43 additions & 0 deletions lib/generators/tolk/install_generator.rb
@@ -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
11 changes: 11 additions & 0 deletions lib/generators/tolk/templates/initializer.erb
@@ -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.
30 changes: 30 additions & 0 deletions lib/generators/tolk/utils.rb
@@ -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

19 changes: 0 additions & 19 deletions lib/generators/tolk_migration/tolk_migration_generator.rb

This file was deleted.

3 changes: 2 additions & 1 deletion lib/tasks/tolk_tasks.rake
@@ -1,7 +1,8 @@
namespace :tolk do
desc "Add database tables, copy over the assets, and import existing translations"
task :setup => :environment do
system("rails generate tolk_migration")
system 'rails g tolk:install'

Rake::Task['db:migrate'].invoke
Rake::Task['tolk:sync'].invoke
Rake::Task['tolk:import'].invoke
Expand Down
2 changes: 1 addition & 1 deletion lib/tolk/config.rb
Expand Up @@ -5,7 +5,7 @@ module Config


class << self
# Application title, can be an array of two elements
# Mapping : a hash of the type { 'ar' => 'Arabic' }
attr_accessor :mapping

def reset
Expand Down

0 comments on commit 732c283

Please sign in to comment.