class AssociationGenerator
Public Instance Methods
copy_role_migration()
click to toggle source
# File lib/generators/association/association_generator.rb, line 7 def copy_role_migration if (behavior == :invoke && model_exists?("role")) || (behavior == :revoke && role_migration_exists?) migration_template "role_migration_existing.rb", "db/migrate/add_name_to_roles.rb", migration_version: migration_version else migration_template "role_migration.rb", "db/migrate/create_roles.rb", migration_version: migration_version end end
copy_role_model()
click to toggle source
# File lib/generators/association/association_generator.rb, line 23 def copy_role_model if model_exists?("role") copy_file "role_model_existing.rb","app/models/role.rb" else copy_file "role_model.rb","app/models/role.rb" end end
copy_user_role_model()
click to toggle source
# File lib/generators/association/association_generator.rb, line 31 def copy_user_role_model if model_exists?("user_role") copy_file "user_role_model_existing.rb","app/models/user_role.rb" else copy_file "user_role_model.rb","app/models/user_role.rb" end end
copy_user_roles_migration()
click to toggle source
# File lib/generators/association/association_generator.rb, line 15 def copy_user_roles_migration if (behavior == :invoke && model_exists?("user_role")) || (behavior == :revoke && user_role_migration_exists?) migration_template "user_role_migration_existing.rb", "db/migrate/add_user_roles_association_to_user_roles.rb", migration_version: migration_version else migration_template "user_role_migration.rb", "db/migrate/create_user_roles.rb", migration_version: migration_version end end
Private Instance Methods
migration_path()
click to toggle source
# File lib/generators/association/association_generator.rb, line 53 def migration_path @migration_path ||= File.join("db", "migrate") end
migration_version()
click to toggle source
# File lib/generators/association/association_generator.rb, line 61 def migration_version if rails5? "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" end end
model_exists?(model_name)
click to toggle source
# File lib/generators/association/association_generator.rb, line 41 def model_exists?(model_name) File.exist?(File.join(destination_root, model_path(model_name))) end
model_path(model_name)
click to toggle source
# File lib/generators/association/association_generator.rb, line 57 def model_path(model_name) File.join("app", "models", "#{model_name}.rb") end
rails5?()
click to toggle source
# File lib/generators/association/association_generator.rb, line 67 def rails5? Rails.version.start_with? '5' end
role_migration_data()
click to toggle source
# File lib/generators/association/association_generator.rb, line 71 def role_migration_data <<RUBY t.string :name RUBY end
role_migration_exists?()
click to toggle source
# File lib/generators/association/association_generator.rb, line 45 def role_migration_exists? Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_name_to_roles.rb$/).first end
user_role_migration_data()
click to toggle source
# File lib/generators/association/association_generator.rb, line 78 def user_role_migration_data <<RUBY t.belongs_to :user t.belongs_to :role RUBY end
user_role_migration_exists?()
click to toggle source
# File lib/generators/association/association_generator.rb, line 49 def user_role_migration_exists? Dir.glob("#{File.join(destination_root, migration_path)}/[0-9]*_*.rb").grep(/\d+_add_user_roles_association_to_user_roles.rb$/).first end