Permalink
Browse files

Merge pull request #14 from Bushido/more_generators

Bushido generators
  • Loading branch information...
2 parents 989899e + f2004d2 commit 8b7fa95a762ee4d40c821f695abe18e8e08ca6e3 @sgrove committed Dec 17, 2011
@@ -0,0 +1,41 @@
+module Bushido
+ module Generators
+ class AuthMigrationGenerator < Rails::Generators::NamedBase
+
+ def create_auth_migration_file
+ fields_to_add = []
+ new_resource = class_name.constantize.new
+
+ fields_to_add << "ido_id" if not new_resource.respond_to?(:ido_id)
+ fields_to_add << "first_name" if not new_resource.respond_to?(:first_name)
+ fields_to_add << "last_name" if not new_resource.respond_to?(:last_name)
+ fields_to_add << "email" if not new_resource.respond_to?(:email)
+ fields_to_add << "locale" if not new_resource.respond_to?(:locale)
+ fields_to_add << "timezone" if not new_resource.respond_to?(:timezone)
+
+ inject_into_class "app/models/#{class_name.underscore}.rb", class_name do
+<<-EOF
+ attr_accessor :ido_id\n
+
+ def bushido_extra_attributes(extra_attributes)
+ self.first_name = extra_attributes["first_name"].to_s
+ self.last_name = extra_attributes["last_name"].to_s
+ self.email = extra_attributes["email"]
+ self.locale = extra_attributes["locale"]
+ end
+EOF
+ end
+
+ generate("migration", "AddBushidoFieldsTo#{class_name}", *fields_to_add.collect! { |field| field + ":string"})
+ generate("migration", "AddIndexForIdoIdTo#{class_name}")
+ Dir["db/migrate/*add_index_for_ido_id_to*"].each do |file|
+ inject_into_file file, :after => "class AddIndexForIdoIdToUser < ActiveRecord::Migration\n def change\n" do
+ " add_index :#{plural_name}, :ido_id\n"
+ end
+ end
+ end
+
+ end
+ end
+end
+
@@ -0,0 +1,43 @@
+module Bushido
+ module Generators
+ class HooksGenerator < Rails::Generators::Base
+
+ def create_hooks_file
+
+ lib("bushido/hooks/user_hooks.rb") do
+<<-EOF
+class BushidoUserHooks < Bushido::EventObserver
+ def user_added
+ user.create(:email => params['data']['email'],
+ :ido_id => params['data']['ido_id'],
+ :active => true)
+ end
+
+ def user_removed
+ User.find_by_ido_id(params['data']['ido_id']).try(:disable!)
+ end
+end
+EOF
+ end
+
+ lib('bushido/hooks/app_hooks.rb') do
+<<-EOF
+class BushidoAppHooks < Bushido::EventObserver
+ def app_claimed
+ User.find(1).update_attributes(:email => params['data']['email'],
+ :ido_id => params['data']['ido_id'])
+ end
+end
+EOF
+ end
+
+ initializer "bushido_hooks.rb" do
+<<-EOF
+Dir["\#{Dir.pwd}/lib/bushido/**/*.rb"].each { |file| require file }
+EOF
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,45 @@
+module Bushido
+ module Generators
+ class MailRoutesGenerator < Rails::Generators::Base
+
+ def create_mail_routes_file
+ # Create the lib/bushido directory if it doesnt exist
+ Dir.mkdir("#{Rails.root}/lib/bushido") if not Dir.exists? "#{Rails.root}/lib/bushido"
+
+ lib "bushido/mail_routes.rb" do
+ <<-EOF
+::Bushido::Mailroute.map do |m|
+
+ m.route("mail.simple") do
+ m.subject("hello")
+ end
+
+end
+ EOF
+ end
+
+ lib("bushido/hooks/email_hooks.rb") do
+ <<-EOF
+class BushidoEmailHooks < Bushido::EventObserver
+
+ def mail_simple
+ puts "YAY!"
+ puts params.inspect
+ end
+
+end
+ EOF
+ end
+
+ initializer "bushido_hooks.rb" do
+ <<-EOF
+Dir["\#{Dir.pwd}/lib/bushido/**/*.rb"].each { |file| require file }
+ EOF
+ end
+
+ initializer("bushido_mail_routes.rb", "require './lib/bushido/mail_routes.rb'")
+
+ end
+ end
+ end
+end
@@ -0,0 +1,22 @@
+module Bushido
+ module Generators
+ class RoutesGenerator < Rails::Generators::Base
+
+ def create_routes_file
+ prepend_to_file("config/routes.rb") do
+<<-EOF
+begin
+ Rails.application.routes.draw do
+ bushido_routes
+ end
+rescue => e
+ puts "Error loading the Bushido routes:"
+ puts "\#{e.inspect}"
+end
+EOF
+ end
+
+ end
+ end
+ end
+end

0 comments on commit 8b7fa95

Please sign in to comment.