class ActiveRecord::Generators::BbsGenerator

Public Instance Methods

generate_avatar() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 22
def generate_avatar
  return unless invoke?

  migration_template avatar_migration_filename, "db/migrate/#{avatar_migration_filename}"
  template 'avatar.rb', 'app/models/avatar.rb' unless model_file_exists?('Avatar')
end
generate_user() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 8
def generate_user
  return unless invoke?

  invoke('active_record:model', [@name]) unless model_file_exists?(@name) # we need blank User model
  inject_into_user
end
generate_user_profile() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 15
def generate_user_profile
  return unless invoke?

  migration_template user_profile_migration_filename, "db/migrate/#{user_profile_migration_filename}"
  template 'user_profile.rb', 'app/models/user_profile.rb' unless model_file_exists?('UserProfile')
end

Private Instance Methods

avatar_migration_filename() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 58
def avatar_migration_filename
  model_exists?('Avatar') ? 'change_avatar.rb' : 'create_avatar.rb'
end
inject_into_user() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 31
      def inject_into_user
        inject_into_class model_path(@name), @name do
          <<-RUBY
  has_one :profile, class_name: 'UserProfile', dependent: :destroy

  accepts_nested_attributes_for :profile

  validates_associated :profile
  validates_presence_of :profile
          RUBY
        end
      end
invoke?() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 44
def invoke?; behavior == :invoke end
model_exists?(const) click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 46
def model_exists?(const)
  Object.const_defined?(const) && const.constantize.ancestors.include?(ActiveRecord::Base)
end
model_file_exists?(const) click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 50
def model_file_exists?(const)
  File.exist?(model_path(const))
end
model_path(model_name) click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 62
def model_path(model_name)
  Rails.root.join('app', 'models', "#{model_name.underscore}.rb")
end
user_model_name() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 66
def user_model_name; @name.underscore end
user_profile_migration_filename() click to toggle source
# File lib/generators/active_record/bbs_generator.rb, line 54
def user_profile_migration_filename
  model_exists?('UserProfile') ? 'change_profile.rb' : 'create_profile.rb'
end