module Cambium::GeneratorsHelper
Public Instance Methods
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
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 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 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 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
Shorthand for `bundle exec`
# File lib/generators/helpers/generators_helper.rb, line 8 def be "bundle exec" end
Run `bundle install`
# File lib/generators/helpers/generators_helper.rb, line 129 def bundle run_cmd "bundle install" run_cmd "bundle clean" end
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
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
# File lib/generators/helpers/generators_helper.rb, line 71 def copy_files(files, dir) files.each { |f| copy_unless_exists("#{dir}/#{f}") } end
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
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
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
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
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
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 Cambium to work properly. " end
# File lib/generators/helpers/generators_helper.rb, line 226 def gem_root Gem::Specification.find_by_name("cambium").gem_dir end
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 Cambium to work properly." end
# File lib/generators/helpers/generators_helper.rb, line 230 def help_message(file) puts File.read("#{gem_root}/lib/help/#{file}.txt") end
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 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
Run rake db:migrate
# File lib/generators/helpers/generators_helper.rb, line 44 def migrate rake "db:migrate" end
Run migrate and annotate
# File lib/generators/helpers/generators_helper.rb, line 58 def migrate_and_annotate migrate annotate end
Alias for previous method
# File lib/generators/helpers/generators_helper.rb, line 107 def model_concern(name) add_model_concern(name) end
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
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
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
# 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
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