module Pineapples::Actions::Rails
Public Instance Methods
Registers a callback to be executed after bundle and spring binstubs have run.
after_bundle do git add: '.' end
# File lib/pineapples/actions/rails/rails.rb, line 148 def after_bundle(&block) @after_bundle_callbacks ||= [] @after_bundle_callbacks << block end
# File lib/pineapples/actions/rails/copy_migration.rb, line 6 def copy_migration(migration_name, options = {}) # TODO: find migration file action CopyMigration.new(self, migration_name, options) end
Adds a line inside the Application class for config/application.rb
.
If options :env
is specified, the line is appended to the corresponding file in config/environments
.
environment do "config.autoload_paths += %W(#{config.root}/extras)" end environment(nil, env: "development") do "config.autoload_paths += %W(#{config.root}/extras)" end
# File lib/pineapples/actions/rails/rails.rb, line 20 def environment(content = nil, options = {}) sentinel = /class [a-z_:]+ < Rails::Application/i env_file_sentinel = /Rails\.application\.configure do/ content = yield if !content && block_given? in_root do if options[:env].nil? insert_into_file 'config/application.rb', "\n #{content}", after: sentinel, verbose: false else Array(options[:env]).each do |env| insert_into_file "config/environments/#{env}.rb", "\n #{content}", after: env_file_sentinel, verbose: false end end end end
# File lib/pineapples/actions/rails/erb_converters.rb, line 4 def erb2haml(target, options = {}) recursive = options.fetch(:recursive, true) verbose = options.fetch(:verbose, verbose?) execute = !options.fetch(:pretend, pretend?) preserve = options.fetch(:keep_old_files, false) description = 'Convert ERB views to HAML' say_status(:erb2haml, description, verbose) if execute target_path = File.expand_path(target, app_root) target_path = File.join(target_path, '**') if recursive shell "find #{target_path} -name \\*.erb -print | sed 'p;s/.erb$/.haml/' | xargs -n2 html2haml", verbose: false if !preserve erb_pattern = File.join(target_path, '*.erb') Dir.glob(erb_pattern).each { |file| ::FileUtils.rm_f(file) } end end end
# File lib/pineapples/actions/rails/erb_converters.rb, line 25 def erb2slim(target, options = {}) recursive = options.fetch(:recursive, true) verbose = options.fetch(:verbose, verbose?) execute = !options.fetch(:pretend, pretend?) preserve = options.falsetch(:keep_old_files, false) description = 'Convert ERB views to SLIM' say_status(:erb2slim, description, verbose) if execute erb2haml(target, options) target_path = File.expand_path(target, app_root) target_path = File.join(target_path, '**') if recursive shell "haml2slim #{target_path} #{preserve ? '' : '--delete'} --trace", verbose: false end end
Generate something using a generator from Rails
or a plugin. The second parameter is the argument string that is passed to the generator or an Array that is joined.
generate(:authenticated, "user session")
# File lib/pineapples/actions/rails/rails.rb, line 111 def generate(what, *args) say_status :generate, what, color_from_behaviour arguments = args.flat_map(&:to_s).join(' ') in_root { ruby("bin/rails generate #{what} #{arguments}", verbose: false) } end
Create a new initializer with the provided code (either in a block or a string).
initializer("globals.rb") do data = "" ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do |const| data << "#{const} = :entp\n" end data end initializer("api.rb", "API_KEY = '123456'")
# File lib/pineapples/actions/rails/rails.rb, line 101 def initializer(filename, content = nil, &block) say_status :initializer, filename, color_from_behaviour create_file("config/initializers/#{filename}", content, verbose: false, &block) end
Create a new file in the lib/ directory. Code can be specified in a block or a data string can be given.
lib("crypto.rb") do "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'" end lib("foreign.rb", "# Foreign code is fun")
# File lib/pineapples/actions/rails/rails.rb, line 61 def lib(filename, content = nil, &block) say_status :lib, filename, color_from_behaviour create_file("lib/#{filename}", content, verbose: false, &block) end
Runs the supplied rake task
rake("db:migrate") rake("db:migrate", env: "production") rake("gems:install", sudo: true)
# File lib/pineapples/actions/rails/rails.rb, line 123 def rake(command, options = {}) say_status :rake, command, color_from_behaviour env = options[:env] || ENV['RAILS_ENV'] || 'development' sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : '' in_root { shell("#{sudo}rake #{command} RAILS_ENV=#{env}", verbose: false) } end
Create a new Rakefile
with the provided code (either in a block or a string).
rakefile("bootstrap.rake") do project = ask("What is the UNIX name of your project?") <<-TASK namespace :#{project} do task :bootstrap do puts "I like boots!" end end TASK end rakefile('seed.rake', 'puts "Planting seeds"')
# File lib/pineapples/actions/rails/rails.rb, line 81 def rakefile(filename, content = nil, &block) say_status :lib, filename, color_from_behaviour rake_extention = File.extname(filename) == '.rake' filename = rake_extention ? filename : "#{filename}.rake" create_file("lib/tasks/#{filename}", content, verbose: false, &block) end
Make an entry in Rails
routing file config/routes.rb
route "root 'welcome#index'"
# File lib/pineapples/actions/rails/rails.rb, line 133 def route(routing_code) say_status :route, routing_code, color_from_behaviour sentinel = /\.routes\.draw do\s*\n/m in_root do insert_into_file 'config/routes.rb', " #{routing_code}\n", {after: sentinel, verbose: false, force: true} end end
Create a new file in the vendor/
directory. Code can be specified in a block or a data string can be given.
vendor("sekrit.rb") do sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--" "salt = '#{sekrit_salt}'" end vendor("foreign.rb", "# Foreign code is fun")
# File lib/pineapples/actions/rails/rails.rb, line 48 def vendor(filename, content = nil, &block) log :vendor, filename create_file("vendor/#{filename}", content, verbose: false, &block) end
Protected Instance Methods
# File lib/pineapples/actions/rails/rails.rb, line 155 def color_from_behaviour behaviour == :invoke ? :light_green : :light_red end
# File lib/pineapples/actions/rails/rails.rb, line 171 def comment_if(setting = nil) return yield ? '# ' : '' if block_given? raise ArgumentError, 'You should provide either setting key or block' if setting.nil? settings[setting].value ? '# ' : '' end
# File lib/pineapples/actions/rails/rails.rb, line 178 def comment_if_not(setting = nil) return yield ? '' : '# ' if block_given? raise ArgumentError, 'You should provide either setting key or block' if setting.nil? settings[setting].value ? '' : '# ' end
Surround string with single quotes if there is no quotes. Otherwise fall back to double quotes
# File lib/pineapples/actions/rails/rails.rb, line 161 def quote(value) return value.inspect unless value.is_a? String if value.include?("'") value.inspect else "'#{value}'" end end