class Gempire::Generator

Public Instance Methods

add_default_gemset() click to toggle source

This method will add the default gem set to the Gemfile

# File lib/gempire/cli.rb, line 190
    def add_default_gemset
      gems = <<-gems
gem 'simple_form'
gem 'stripe'
gem 'ledermann-rails-settings'
gem 'factory_girl_rails'
gem 'faker'
gem 'bcrypt', '~> 3.1.7'
gems


      File.open('Gemfile', 'a') do |f|
        f.puts(gems)
      end
    end
generate() click to toggle source
# File lib/gempire/cli.rb, line 20
    def generate

      ##
      # Add default gemset
      add_default_gemset

      puts 'Default gemset added'

      ##
      # Return early if no options were specified
      return 'Please specify an option. Use gempire --help to see a list of options.' if options.empty?

      ##
      # This is the control structure for generating everything related to the
      # the user model
      if options[:with_users]
        ##
        # Executes the rails generator for the user model and passes in the default
        # attributes
        exec 'rails generate model User first_name last_name email password_digest'

        user_content = <<-ruby
  # Settings provided by the ledermann-rails-settings gem
  has_settings do |s|
    s.key :time_zone, defaults: {
      zone: 'UTC'
    }
  end
ruby

        ##
        # Add the has_secure_password method to the user model
        lines = File.readlines('app/models/user.rb')
        lines.insert(1, "\thas_secure_password")
        File.open('app/models/user.rb', 'w') do |f|
          lines.each do |l|
            f.puts l
          end
        end

        ##
        # Create the log in functionality. This will create the sessions_controller
        template_path = File.expand_path('../templates/sessions_controller.rb', File.dirname(__FILE__))
        File.open('app/controllers/sessions_controller.rb', 'w') do |dest|
          File.open(template_path) do |template|
            dest.puts(template.read)
          end
        end

        ##
        # Now write some convenience methods to the application_controller
        definition = <<-definition
  helper_method :current_user
  helper_method :signed_in?

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def signed_in?
    current_user.present?
  end

  def sign_in(user)
    session[:user_id] = user.id
  end

  def after_sign_in
    redirect_to dashboard_path
  end

  def after_sign_out
    redirect_to sign_in_path
  end
definition

        ##
        # Read out the existing application_controller
        lines = File.readlines('app/controllers/application_controller.rb')
        lines.insert(1, definition)

        ##
        # Write back the application_controller with the new after_sign_in_method
        File.open('app/controllers/application_controller.rb', 'w') do |f|
          lines.each {|l| f.puts(l)}
        end

        routes = <<-routes
  get '/sign-in' => 'sessions#new', as: 'sign_in'
  post '/sessions' => 'sessions#create'
  delete '/sessions' => 'sessions#delete'
routes

        ##
        # Write the sessions routes to the routes.rb file
        lines = File.readlines('app/config/routes.rb')
        lines.insert(1, routes)

        File.open('app/config/routes.rb') do |f|
          lines.each {|l| f.puts(l)}
        end

        ##
        # Make the views/sessions directory if it's not already there
        if File.directory?('app/views/sesions') === false
          Dir.mkdir('app/views/sessions')
        end

        ##
        # Create the login form
        if options[:with_bootstrap]
          template_path = File.expand_path('../templates/sign_in_form_bootstrap.html.erb', File.dirname(__FILE__))
          File.open('app/views/sessions/new.html.erb') do |dest|
            File.open(template_path) do |template|
              dest.write(template.read)
            end
          end
        else
          template_path = File.expand_path('../templates/sign_in_form.html.erb', File.dirname(__FILE__))
          File.open('app/views/sessions/new.html.erb') do |dest|
            File.open(template_path) do |template|
              dest.write(template.read)
            end
          end
        end
      end

      ##
      # Control structure for using bootstrap
      if options[:with_bootstrap]
        text = <<-bower
{
  "directory": "vendor/assets/bower_components"
}
bower

        File.open('.bowerrc', 'w+') do |dest|
          dest.write(text)
        end


        exec 'mv app/assets/stylesheets/application.css app/assets/stylesheets/application.scss'

        File.open('app/assets/stylesheets/application.scss', 'w+') do |dest|
          dest.puts("@import 'bootstrap/dist/css/bootstrap';")
        end

        ##
        # Find what line to insert the bower components
        start_at = 0
        lines = File.readlines('config/application.rb')
        lines.each_with_index do |line, index|
          if line.include?('Rails::Application')
            starts_at = index
            break
          end
        end
        lines.insert(index, "\t\tconfig.assets.paths << Rails.root.join('bower_components')")
        File.open('config/application.rb', 'w') do |dest|
          lines.each {|line| dest.puts(line)}
        end

        # exec 'bower install --save bootstrap'
      end
    end