module Heartwood::GeneratorsHelper

Public Instance Methods

add_gems_to_gemfile(gems) click to toggle source

Add several gems to the gemfile

# File lib/generators/helpers/generators_helper.rb, line 156
def add_gems_to_gemfile(gems)
  gems.each { |g| add_to_gemfile(g) }
end
add_model_concern(name) click to toggle source

Copies model concern templates to the project

# File lib/generators/helpers/generators_helper.rb, line 101
def add_model_concern(name)
  copy_unless_exists "app/models/concerns/#{name}.rb"
end
add_model_concerns(concerns) click to toggle source

Add multiple concerns

# File lib/generators/helpers/generators_helper.rb, line 113
def add_model_concerns(concerns)
  concerns.each { |c| add_model_concern(c) }
end
add_to_gemfile(name, options = {}) click to toggle source

Add gem to Gemfile

# File lib/generators/helpers/generators_helper.rb, line 144
def add_to_gemfile(name, options = {})
  unless gem_exists?(name)
    if options[:require].present?
      text = "\n\ngem '#{name}', :require => '#{options[:require]}'"
    else
      text = "\n\ngem '#{name}'"
    end
    insert_into_file "Gemfile", text, :after => "rubygems.org'"
  end
end
annotate() click to toggle source

Annotate our models and tests

# File lib/generators/helpers/generators_helper.rb, line 50
def annotate
  # need to make sure we have the annotate gem to avoid errors
  install_gem('annotate')
  run_cmd "#{be} annotate"
end
be() click to toggle source

Shorthand for `bundle exec`

# File lib/generators/helpers/generators_helper.rb, line 8
def be
  "bundle exec"
end
bundle() click to toggle source

Run `bundle install`

# File lib/generators/helpers/generators_helper.rb, line 129
def bundle
  run_cmd "bundle install"
  run_cmd "bundle clean"
end
class_exists?(class_name) click to toggle source

Check to see if a class exists. This can be helpful in determining if other generators have been run.

# File lib/generators/helpers/generators_helper.rb, line 221
def class_exists?(class_name)
  klass = Module.const_get(class_name)
  klass.is_a?(Class)
end
confirm_ask(question) click to toggle source

Make sure we get a matching answer before moving on. Useful for usernames, passwords, and other items where user error is harmful (or painful)

# File lib/generators/helpers/generators_helper.rb, line 31
def confirm_ask(question)
  answer = ask("\n#{question}")
  match = ask("CONFIRM #{question}")
  if answer == match
    answer
  else
    say set_color("Did not match.", :red)
    confirm_ask(question)
  end
end
copy_files(files, dir) click to toggle source
# File lib/generators/helpers/generators_helper.rb, line 71
def copy_files(files, dir)
  files.each { |f| copy_unless_exists("#{dir}/#{f}") }
end
copy_unless_exists(path, new_path = path) click to toggle source

Copy template file to project unless it's already there

# File lib/generators/helpers/generators_helper.rb, line 67
def copy_unless_exists(path, new_path = path)
  copy_file(path, new_path) unless File.exists?("#{Rails.root}/#{new_path}")
end
directories(dir_arr, container) click to toggle source

Copy multiple directories

# File lib/generators/helpers/generators_helper.rb, line 119
def directories(dir_arr, container)
  dir_arr.each do |dir|
    directory "#{container}/#{dir}", "#{container}/#{dir}"
  end
end
file_contents(template) click to toggle source

Read a template and return its contents

# File lib/generators/helpers/generators_helper.rb, line 90
def file_contents(template)
  File.read(template_file(template))
end
gem_dir() click to toggle source

Get the local path where the gems are installed

# File lib/generators/helpers/generators_helper.rb, line 136
def gem_dir
  gem_dir = `bundle show rails`
  gem_dir = gem_dir.split('/')
  gem_dir[0..-3].join('/')
end
gem_exists?(name) click to toggle source

Find if a gem exists

# File lib/generators/helpers/generators_helper.rb, line 162
def gem_exists?(name)
  Gem::Specification::find_all_by_name(name).any?
end
gem_installation_notification(gems) click to toggle source

Notifies the user of the gems that were installed and how desperately important they are.

# File lib/generators/helpers/generators_helper.rb, line 193
def gem_installation_notification(gems)
  say "\nThis generator installed the following gems (and added them to "
  say "your Gemfile):\n\n"
  gems.each { |g| say "    #{g}" }
  say "\nThese gems *may* be necessary for Heartwood to work properly. "
end
gem_root() click to toggle source
# File lib/generators/helpers/generators_helper.rb, line 226
def gem_root
  Gem::Specification.find_by_name("heartwood").gem_dir
end
gemfile_update_notification(gems) click to toggle source

Similar to gem_installation_notification, except here we just tell the user we added gems to the gemfile and they will have to bundle

# File lib/generators/helpers/generators_helper.rb, line 203
def gemfile_update_notification(gems)
  say "\nThis generator added some gems to your Gemfile. You will want to "
  say "run:\n\n    $ bundle install"
  say "\nto complete this generator. Please note, "
  say "these gems *may* be necessary for Heartwood to work properly."
end
help_message(file) click to toggle source
# File lib/generators/helpers/generators_helper.rb, line 230
def help_message(file)
  puts File.read("#{gem_root}/lib/help/#{file}.txt")
end
install_gem(name, options = {}) click to toggle source

First, install the gem in the local gem directory, then add it to the Gemfile (if it doesn't already exist).

Note: This will run bundle install after adding the gem, unless specified otherwise.

# File lib/generators/helpers/generators_helper.rb, line 172
def install_gem(name, options = {})
  if gem_exists?(name)
    say "Found existing gem: #{name}"
  else
    run "gem install #{name} -i #{gem_dir}"
    add_to_gemfile(name, options)
    bundle if options[:bundle].nil? || options[:bundle] == true
  end
end
install_gems(gem_arr) click to toggle source

Install an array of gems – useful if needing to install more than one gem.

# File lib/generators/helpers/generators_helper.rb, line 185
def install_gems(gem_arr)
  gem_arr.each { |gem_name| install_gem(gem_name, :bundle => false) }
  bundle
end
migrate() click to toggle source

Run rake db:migrate

# File lib/generators/helpers/generators_helper.rb, line 44
def migrate
  rake "db:migrate"
end
migrate_and_annotate() click to toggle source

Run migrate and annotate

# File lib/generators/helpers/generators_helper.rb, line 58
def migrate_and_annotate
  migrate
  annotate
end
model_concern(name) click to toggle source

Alias for previous method

# File lib/generators/helpers/generators_helper.rb, line 107
def model_concern(name)
  add_model_concern(name)
end
run_cmd(cmd, options = {}) click to toggle source

Runs a system command, but with pretty output

# File lib/generators/helpers/generators_helper.rb, line 14
def run_cmd(cmd, options = {})
  print_table(
    [
      [set_color("run", :green, :bold), cmd]
    ],
    :indent => 9
  )
  if options[:quiet] == true
    `#{cmd}`
  else
    system(cmd)
  end
end
template_file(name) click to toggle source

Get the path to a particular template

# File lib/generators/helpers/generators_helper.rb, line 77
def template_file(name)
  File.expand_path("../../templates/#{name}", __FILE__)
end
template_partial_path(name) click to toggle source

Partial template path are for templates that aren't the entire file to be copied.

# File lib/generators/helpers/generators_helper.rb, line 84
def template_partial_path(name)
  File.expand_path("../../templates/_partials/#{name}", __FILE__)
end
template_snippet(template) click to toggle source
# File lib/generators/helpers/generators_helper.rb, line 94
def template_snippet(template)
  require 'erb'
  ERB.new(File.read(template_file(template))).result(binding)
end
timestamp() click to toggle source

Return current time in migration timestamp format

# File lib/generators/helpers/generators_helper.rb, line 214
def timestamp
  Time.now.gmtime.strftime('%Y%m%d%H%M%S')
end