Class: Cloudfuji::Generators::AuthMigrationGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/cloudfuji/auth_migration_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_auth_migration_fileObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/cloudfuji/auth_migration_generator.rb', line 5

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)


  attr_accessor_string = fields_to_add.collect { |field| ":"+field}.join(", ")
  
  inject_into_class "app/models/#{class_name.underscore}.rb", class_name do
<<-EOF
  attr_accessor #{attr_accessor_string}\n

  def cloudfuji_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"]
    self.timezone   = extra_attributes["timezone"]
  end
EOF
  end

  gem("devise_cloudfuji_authenticatable")

  generate("migration", "AddCloudfujiFieldsTo#{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