Skip to content
Browse files

Adding a generator when setting up Tolk.

  • Loading branch information...
1 parent 3ad92bf commit 732c283596e8004bc1474700ecbde76a983a49a3 @thomasdarde thomasdarde committed May 16, 2012
View
8 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)
@@ -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
View
43 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
View
11 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
View
0 ...n/templates/migrate/create_tolk_tables.rb → lib/generators/tolk/templates/migration.rb
File renamed without changes.
View
30 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
+
View
19 lib/generators/tolk_migration/tolk_migration_generator.rb
@@ -1,19 +0,0 @@
-class TolkMigrationGenerator < Rails::Generators::Base
- include Rails::Generators::Migration
-
- source_root File.expand_path("../templates", __FILE__)
-
- def create_migration_file
- migration_template "migrate/create_tolk_tables.rb", "db/migrate/create_tolk_tables.rb"
- end
-
- # Implement the required interface for Rails::Generators::Migration.
- def self.next_migration_number(dirname) #:nodoc:
- next_migration_number = current_migration_number(dirname) + 1
- if ActiveRecord::Base.timestamped_migrations
- [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
- else
- "%.3d" % next_migration_number
- end
- end
-end
View
3 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
View
2 lib/tolk/config.rb
@@ -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

0 comments on commit 732c283

Please sign in to comment.
Something went wrong with that request. Please try again.