Class: ActiveRecord::Generators::NinshoGenerator

Inherits:
Base
  • Object
show all
Includes:
Ninsho::Generators::OrmHelpers
Defined in:
lib/generators/active_record/ninsho_generator.rb

Instance Method Summary collapse

Methods included from Ninsho::Generators::OrmHelpers

#migration_exists?, #migration_path, #model_exists?, #model_path

Instance Method Details

#copy_ninsho_migrationObject



12
13
14
15
16
17
18
# File 'lib/generators/active_record/ninsho_generator.rb', line 12

def copy_ninsho_migration
  if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
    migration_template "existing_migration.rb", "db/migrate/add_ninsho_to_#{table_name}"
  else
    migration_template "migration.rb", "db/migrate/ninsho_create_#{table_name}"
  end
end

#generate_modelObject



20
21
22
# File 'lib/generators/active_record/ninsho_generator.rb', line 20

def generate_model
  invoke "active_record:model", [name], :migration => false unless model_exists? && behavior == :invoke
end

#inject_ninsho_contentObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/active_record/ninsho_generator.rb', line 24

def inject_ninsho_content
 content = <<CONTENT
  attr_accessible :provider, :uid, :oauth_token
  # belongs_to_ninsho :user
CONTENT

 class_path = if namespaced?
   class_name.to_s.split("::")
 else
   [class_name]
 end

 indent_depth = class_path.size - 1
 content = content.split("\n").map { |line| "  " * indent_depth + line } .join("\n") << "\n"
 inject_into_class(model_path, class_path.last, content) if model_exists?
end

#migration_dataObject



41
42
43
44
45
46
47
48
49
# File 'lib/generators/active_record/ninsho_generator.rb', line 41

def migration_data
<<RUBY
## Ninsho model fields
t.integer :user_id
t.string :provider
t.string :uid
t.string :oauth_token
RUBY
end