Coder Social home page Coder Social logo

phase-4-rails-api-lab-template's Introduction

Rails API Lab Templates

A Rails Application Template for generating new Rails API labs with updated dependencies.

Usage:

rails new <lab-name> -BT --api --minimal -m=https://raw.githubusercontent.com/learn-co-curriculum/phase-4-rails-api-lab-template/main/template.rb

Notes

  • -m uses the custom Flatiron lab template to provide some additional setup.
  • -B skips bundle install, since we'll want to make some changes before bundling.
  • -T skips test files, since we'll be using rspec.
  • --api configures the app with a limited set of middleware, and skips views/helpers/assets on resource generation.
  • --minimal excludes the following:
    • action_cable
    • action_mailbox
    • action_mailer
    • action_text
    • active_job
    • active_storage
    • bootsnap
    • jbuilder
    • spring
    • system_tests
    • turbolinks
    • webpack

Writing Tests

It's recommended to use Rails generators to add new code, since rspec will give you a template for models and controllers (requests) automatically when you use rails g.

In addition to rspec-rails, these gems provide some additional matchers:

Template Notes

The template.rb file takes care of a few extra things, documented below:

Remove Ruby Version

Since we don't want to update the labs every time a new Ruby version is released, and want them to work for as many versions of Ruby as possible, we need to remove references to the Ruby version in the project:

  • Delete the .ruby-version file
  • Remove the Ruby version at the top of the Gemfile

Setup RSPec

Add Session/Cookie Middleware

To help with lessons on auth, this adds back in the session/cookie middleware.

  • Update config/application.rb with middleware
  • Update app/controllers/application_controller.rb with cookie helpers

Generate React Application

Optionally include a React client.

  • Use npx create-react-app client --use-npm to generate a React app in the client directory
  • Add "proxy": "http://localhost:3000" to package.json to proxy API requests
  • Add rake install script

The foreman gem lets us run multiple processes from a Procfile. This means we can create a rails start script to run Rails and React together.

  • Add Procfile.dev with process runner instructions
  • Add rake start task

Note: foreman shouldn't be included in the Gemfile. Instruct users to install foreman globally:

gem install foreman

Resources

Templating

Testing

phase-4-rails-api-lab-template's People

Contributors

ihollander avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.