Module: Pineapples::Actions::Rails

Included in:
Pineapples::AppGenerator
Defined in:
lib/pineapples/actions/rails/rails.rb,
lib/pineapples/actions/rails/copy_migration.rb,
lib/pineapples/actions/rails/erb_converters.rb

Defined Under Namespace

Classes: CopyMigration

Instance Method Summary collapse

Instance Method Details

#after_bundle(&block) ⇒ Object

Registers a callback to be executed after bundle and spring binstubs have run.

after_bundle do
  git add: '.'
end


148
149
150
151
# File 'lib/pineapples/actions/rails/rails.rb', line 148

def after_bundle(&block)
  @after_bundle_callbacks ||= []
  @after_bundle_callbacks << block
end

#copy_migration(migration_name, options = {}) ⇒ Object



6
7
8
9
# 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

#environment(content = nil, options = {}) ⇒ Object Also known as: application

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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

#erb2haml(target, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

#erb2slim(target, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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(what, *args) ⇒ Object

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")


111
112
113
114
115
116
# 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

#initializer(filename, content = nil, &block) ⇒ Object

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'")


101
102
103
104
# 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

#lib(filename, content = nil, &block) ⇒ Object

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")


61
62
63
64
# 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

#rake(command, options = {}) ⇒ Object

Runs the supplied rake task

rake("db:migrate")
rake("db:migrate", env: "production")
rake("gems:install", sudo: true)


123
124
125
126
127
128
# 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

#rakefile(filename, content = nil, &block) ⇒ Object

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"')


81
82
83
84
85
86
# 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

#route(routing_code) ⇒ Object

Make an entry in Rails routing file config/routes.rb

route "root 'welcome#index'"


133
134
135
136
137
138
139
140
# 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

#vendor(filename, content = nil, &block) ⇒ Object

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")


48
49
50
51
# 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