class Object

Public Instance Methods

active_admin() click to toggle source
# File lib/rails_baseline/template_configurations/active_admin.rb, line 1
def active_admin
  # Disable activeadmin for mongoid at the moment
  if @configs['database'] != "mongoid"
    gem 'activeadmin', github: 'activeadmin'

    if yes_wizard?("Active Admin with Users?(no to skip users)")
      model_name = ask_wizard("Enter the model name of ActiveAdmin. Leave it blank to default as AdminUser.")
      after_bundler do
        if model_name.present?
          generate "active_admin:install #{model_name}"
        else
          generate "active_admin:install"
        end
      end
    else
      after_bundler do
        generate "active_admin:install --skip-users"
      end
    end
  end
end
after_bundler(&block) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 52
def after_bundler(&block)
  @after_blocks << [@current_recipe, block]
end
after_everything(&block) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 56
def after_everything(&block)
  @after_everything_blocks << [@current_recipe, block]
end
application_layout() click to toggle source
# File lib/rails_baseline/template_configurations/views.rb, line 1
def application_layout
  application_html_file = <<-TEXT
  <!DOCTYPE html>
  <!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
  <!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
  <!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
  <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
  <html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <meta name="description" content="This is the description">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">

    <meta property="og:title" content="Title" />
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<%= root_url %>" />
    <meta property="og:image" content="<%= root_url %>/logo.png" />
    <meta property="og:description" content="This is the description" />

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->
    <!-- Google Tag Manager -->
    <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-XXXXXX');</script>
    <!-- End Google Tag Manager -->

    <%= yield %>
    
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  </body>
  </html>
  TEXT

  after_bundler do
    remove_file 'app/views/layouts/application.html.erb'
    create_file 'app/views/layouts/application.html.erb', application_html_file
    say_wizard "--------------------------- APPLICATION VIEWS -----------------------------"
    say_wizard "|Please change your GTM, title and meta settings in application.html.erb. |"
    say_wizard "---------------------------------------------------------------------------"
  end
end
ask_wizard(question) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 21
def ask_wizard(question)
  ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + "  #{question}\033[0m"
end
background_processor() click to toggle source
# File lib/rails_baseline/template_configurations/backrground_processor.rb, line 1
def background_processor
  if @configs['database'] == "mongoid"
    gem 'delayed_job_mongoid'
  else
    gem 'delayed_job_active_record'
    after_bundler do
      generate "delayed_job:active_record"
    end
  end

  delayed_job_config = <<-TEXT
    \n
      config.active_job.queue_adapter = :delayed_job
  TEXT

  after_bundler do
    inject_into_file "config/application.rb", delayed_job_config, :after => "# config.i18n.default_locale = :de"
  end
end
before_config(&block) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 60
def before_config(&block)
  @before_configs[@current_recipe] = block
end
bootstrap() click to toggle source
# File lib/rails_baseline/template_configurations/assets.rb, line 12
def bootstrap
  config_lines = <<-TEXT
  @import "bootstrap-sprockets";
  @import "bootstrap";
  TEXT

  flash_message = <<-TEXT
  <% flash.each do |name, msg| %>
    <% 
      code = "warning"
      desc = "Oh!"
      case name.to_s 
      when "notice"
        code = "success"
        desc = "Done!" 
      when "error"
        code = "danger"
        desc = "Alert!"
      when "info"
        code = "info"
        desc = "Info!"
      end
    %>      
    <div class="alert alert-<%= code %>">
        <button type="button" class="close" data-dismiss="alert">x</button>
        <strong><%= desc %></strong> <%= msg %>
    </div> 
  <% end %>
  TEXT

  use_bootstrap = @configs[@current_recipe] = yes_wizard?("Install and configure Bootstrap?")
  if use_bootstrap
    gem 'bootstrap-sass', '~> 3.3.4'

    after_bundler do
      append_to_file 'app/assets/stylesheets/application.css.scss', config_lines
      insert_into_file "app/assets/javascripts/application.js", :after => %r{//= require +['"]?jquery_ujs['"]?} do
        "\n//= require bootstrap-sprockets"
      end
      create_file 'app/views/shared/_messages.html.erb', flash_message
    end
  end
end
bundler() click to toggle source
# File lib/rails_baseline/template_configurations/commands.rb, line 9
def bundler
  say_wizard "Running Bundler install. This will take a while."
  run 'bundle install'
  say_wizard "Running after Bundler callbacks."
  @after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}

  @current_recipe = nil
  say_wizard "Running after everything callbacks."
  @after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}

  @current_recipe = nil
  say_wizard "==================================FINISH PROCESS================================="
end
cancancan() click to toggle source
# File lib/rails_baseline/template_configurations/rails_plugins.rb, line 1
def cancancan
  gem 'cancancan'

  after_bundler do
    generate "cancan:ability"
  end
end
database() click to toggle source
# File lib/rails_baseline/template_configurations/database.rb, line 1
def database
  database_option = "activerecord"

  config = {}
  config['database'] = multiple_choice("Which database are you using?", [["MongoDB", "mongoid"], ["MySQL", "mysql"], ["PostgreSQL", "postgresql"], ["SQLite", "sqlite3"]]) if true && true unless config.key?('database')
  database_option = config['database']
  @configs[@current_recipe] = database_option


  if config['database']
      say_wizard "Configuring '#{config['database']}' database settings..."
      @options = @options.dup.merge(:database => config['database'])
      say_recipe "Currently selected as #{database_option}"
    if database_option != "mongoid"
      config['auto_create'] = yes_wizard?("Automatically create database with default configuration?") if true && true unless config.key?('auto_create')
      gem gem_for_database[0]
      template "config/databases/#{@options[:database]}.yml", "config/database.yml.new"
      run 'mv config/database.yml.new config/database.yml'
    else
      database_option = "mongoid"
      gem 'mongoid'
    end
  end

  after_bundler do
    if database_option == "activerecord"
      rake "db:create" if config['auto_create']
    elsif database_option == "mongoid"
      generate 'mongoid:config'
    end
  end
end
debugger() click to toggle source
# File lib/rails_baseline/template_configurations/rails_plugins.rb, line 25
def debugger
  gem_group :development do
    gem "better_errors"
    gem "hirb"
  end
end
deflator() click to toggle source
# File lib/rails_baseline/template_configurations/environment.rb, line 1
def deflator
  insertion_text = <<-TEXT
  \n
      config.middleware.use Rack::Deflater
  TEXT

  inject_into_file "config/application.rb", insertion_text, :after => "# config.i18n.default_locale = :de"
end
deployment() click to toggle source
# File lib/rails_baseline/template_configurations/deployment.rb, line 1
def deployment
  @configs['deployment'] = multiple_choice("Which deployment method are you using?", [["Heroku", "heroku"], ["Capistrano", "capistrano"], ["Engine Yard", "engineyard"], ["No Recipe", "none"]])

  unicorn_config =<<-TEXT
  # config/unicorn.rb
  worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
  timeout 15
  preload_app true

  before_fork do |server, worker|
    Signal.trap 'TERM' do
      puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
      Process.kill 'QUIT', Process.pid
    end

      defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!
  end

  after_fork do |server, worker|
    Signal.trap 'TERM' do
      puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
    end

    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.establish_connection
  end
  TEXT

  ey_config = <<-TEXT
  ---
  # This is all you need for a typical rails application.
  defaults:
    migrate: true
    migration_command: rake db:migrate
    precompile_assets: true
  TEXT

  case @configs['deployment']
  when "heroku"
    gem 'unicorn'
    gem 'rails_12factor', group: :production

    after_bundler do
      create_file "config/unicorn.rb", unicorn_config
      create_file "Procfile", "web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb"
    end
  when "capistrano"
    gem 'capistrano'

    after_bundler do
      run "bundle exec cap install"
    end

  when "engineyard"
    after_bundler do
      create_file "config/ey.yml", ey_config
    end
  end
end
devise() click to toggle source
# File lib/rails_baseline/template_configurations/devise.rb, line 1
def devise
  gem 'devise'

  after_bundler do
    generate 'devise:install'
  end

  if yes_wizard?("Generate Devise model?")
    model_name = ask_wizard("Enter the model name for Devise. Leave it blank to default as User.")
    after_bundler do
      if model_name.present?
        generate "devise #{model_name}"
      else
        generate "devise user"
      end
    end
  end
end
file_upload() click to toggle source
# File lib/rails_baseline/template_configurations/file_upload.rb, line 1
def file_upload
  if @configs['database'] == "mongoid"
    gem "mongoid-paperclip", :require => "mongoid_paperclip"
    gem 'aws-sdk', '~> 1.3.4'
  else
    gem "paperclip", "~> 4.2"
  end
end
font_awesome() click to toggle source
# File lib/rails_baseline/template_configurations/assets.rb, line 56
def font_awesome
  font_awesome_configs = <<-TEXT
  @import "font-awesome-sprockets";
  @import "font-awesome";
  TEXT

  gem 'font-awesome-sass', '~> 4.3.0'

  after_bundler do
    append_to_file 'app/assets/stylesheets/application.css.scss', font_awesome_configs
  end
end
git() click to toggle source
# File lib/rails_baseline/template_configurations/commands.rb, line 1
def git
  after_everything do
    git :init
    git :add => '.'
    git :commit => '-m "Initial commit"'
  end
end
home_page() click to toggle source
# File lib/rails_baseline/template_configurations/views.rb, line 60
def home_page
  after_bundler do
    generate "controller home index"
    inject_into_file "config/routes.rb", "\n  root 'home#index'", :after => "devise_for :users"
  end
end
jquery_datatable() click to toggle source
# File lib/rails_baseline/template_configurations/jquery_plugins.rb, line 11
def jquery_datatable
  gem 'jquery-datatables-rails', '~> 3.2.0'

  datatable_config_lines_bootstrap = <<-TEXT
  @import "dataTables/bootstrap/3/jquery.dataTables.bootstrap";
  TEXT

  datatable_config_lines_non_bootstrap = <<-TEXT
  @import "dataTables/jquery.dataTables";
  TEXT

  datatable_model = <<-TEXT
  class Datatable
    include ApplicationHelper
    delegate :params, :t, :h, :current_user, :link_to, to: :@view

    def initialize(view)
      @view = view
    end

    def as_json(options = {})
      {
        sEcho: params[:sEcho].to_i,
        iTotalRecords: rows ? rows.count : 0,
        iTotalDisplayRecords: rows ? rows.total_entries : 0,
        aaData: data
      }
    end


    def rows
      @rows ||= fetch_datas
    end

    def fetch_datas
    end

    def page
      params[:iDisplayStart].to_i/per_page + 1
    end

    def per_page
      params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
    end

    def columns
      %w[]
    end

    def sort_column
      if params["iSortCol_0"].blank?
        columns[params[:order]["0"]["column"].to_i]
      else 
        columns[params["iSortCol_0"].to_i]    
      end
      
    end

    def sort_direction
      if params["iSortCol_0"].blank?
        params[:order]["0"]["dir"] == "desc" ? "desc" : "asc"
      else
        params["sSortDir_0"] == "desc" ? "desc" : "asc"
      end
      
    end
  end
  TEXT

  after_bundler do
    after_bundler do
      if @configs["bootstrap"] # if bootstrap configuration is true
        say_wizard "Generating Bootstrap 3 dataTables"
        append_to_file 'app/assets/stylesheets/application.css.scss', datatable_config_lines_bootstrap
        insert_into_file "app/assets/javascripts/application.js", :after => %r{//= require +['"]?jquery_ujs['"]?} do
          "\n//= require dataTables/jquery.dataTables\n//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap"
        end
      else
        append_to_file 'app/assets/stylesheets/application.css.scss', datatable_config_lines_non_bootstrap
        insert_into_file "app/assets/javascripts/application.js", :after => %r{//= require +['"]?jquery_ujs['"]?} do
          "\n//= require dataTables/jquery.dataTables\n//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap"
        end
      end
      create_file "app/models/datatable.rb", datatable_model
    end
  end
end
jquery_validation() click to toggle source
# File lib/rails_baseline/template_configurations/jquery_plugins.rb, line 1
def jquery_validation
  gem "jquery-validation-rails"

  after_bundler do
    insert_into_file "app/assets/javascripts/application.js", :after => %r{//= require +['"]?jquery_ujs['"]?} do
      "\n//= require jquery.validate\n//= require jquery.validate.additional-methods"
    end
  end
end
kaminari() click to toggle source
# File lib/rails_baseline/template_configurations/rails_plugins.rb, line 32
def kaminari
  gem 'kaminari'

  after_bundler do
    generate "kaminari:views bootstrap3" if @configs["bootstrap"]
  end
end
multiple_choice(question, choices) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 41
def multiple_choice(question, choices)
  say_custom('question', question)
  values = {}
  choices.each_with_index do |choice,i| 
    values[(i + 1).to_s] = choice[1]
    say_custom (i + 1).to_s + ')', choice[0]
  end
  answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
  values[answer]
end
no_wizard?(question) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 37
def no_wizard?(question)
  !yes_wizard?(question) 
end
paranoia() click to toggle source
# File lib/rails_baseline/template_configurations/rails_plugins.rb, line 17
def paranoia
  if @configs['database'] != "mongoid"
    gem "paranoia"
  else
    gem 'mongoid_paranoia'
  end  
end
quiet_assets() click to toggle source
# File lib/rails_baseline/template_configurations/assets.rb, line 1
def quiet_assets
  gem 'quiet_assets', group: :development
end
rails_config() click to toggle source
# File lib/rails_baseline/template_configurations/rails_config.rb, line 1
def rails_config
  gem 'rails_config'

  after_bundler do
    generate 'rails_config:install'
  end
end
recipe?(name) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 5
def recipe?(name)
  @recipes.include?(name) 
end
recipes() click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 1
def recipes
 @recipes 
end
sass() click to toggle source
# File lib/rails_baseline/template_configurations/assets.rb, line 5
def sass
  after_bundler do
    copy_file 'app/assets/stylesheets/application.css', 'app/assets/stylesheets/application.css.scss'
    remove_file 'app/assets/stylesheets/application.css'
  end
end
say_custom(tag, text) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 9
def say_custom(tag, text)
  say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + "  #{text}"
end
say_recipe(name) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 13
def say_recipe(name)
  say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + "  Running #{name} recipe..."
end
say_wizard(text) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 17
def say_wizard(text)
  say_custom(@current_recipe || 'wizard', text) 
end
smtp() click to toggle source
# File lib/rails_baseline/template_configurations/email.rb, line 1
def smtp
  email_configuration_text = <<-TEXT
  \n
    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      :enable_starttls_auto => true,
      :address              => "smtp.mandrillapp.com",
      :port                 => 587,
      :domain               => 'YOUR_DOMAIN',
      :user_name            => "USERNAME",
      :password             => "PASSWORD",
      :authentication       => :plain
    }
  TEXT
  
  after_bundler do
    inject_into_file 'config/environments/development.rb', email_configuration_text, :after => "config.assets.debug = true"
    inject_into_file 'config/environments/production.rb', email_configuration_text, :after => "config.active_support.deprecation = :notify"
    say_wizard "------------------------ EMAIL SETTINGS --------------------------"
    say_wizard "| Please change your email settings in development.rb            |"
    say_wizard "| and production.rb                                              |"
    say_wizard "------------------------------------------------------------------"
  end
end
state_machines() click to toggle source
# File lib/rails_baseline/template_configurations/rails_plugins.rb, line 9
def state_machines
  if @configs['database'] == "mongoid"
    gem 'state_machines-mongoid'
  else
    gem 'state_machines-activerecord'
  end
end
test_suite() click to toggle source
# File lib/rails_baseline/template_configurations/test_suite.rb, line 1
def test_suite
  gem_group :development do
    gem 'spring-commands-rspec'
    gem 'rails_apps_testing'
  end

  gem_group :development, :test do
    gem "rspec-rails"
    gem 'factory_girl_rails'
    gem 'faker'
  end

  gem_group :test do
    gem 'capybara'
    gem 'database_cleaner'
    gem 'launchy'
    gem 'selenium-webdriver'
  end

  rspec_text = <<-TEXT
  \n
      config.generators do |g|
        g.test_framework :rspec,
        fixtures: true,
        view_specs: false,
        helper_specs: false,
        routing_specs: false,
        controller_specs: true,
        request_specs: false
        g.fixture_replacement :factory_girl, dir: "spec/factories"
      end
  TEXT

  after_bundler do
    run 'bundle binstubs rspec-core'
    generate 'rspec:install'
    remove_dir 'test'

    inject_into_file "config/application.rb", rspec_text, :after => "# config.i18n.default_locale = :de"
  end   
end
yes_wizard?(question) click to toggle source
# File lib/rails_baseline/helper_functions.rb, line 25
def yes_wizard?(question)
  answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
  case answer.downcase
    when "yes", "y"
      true
    when "no", "n"
      false
    else
      yes_wizard?(question)
  end
end