Rails on Rails
If you updated from the minor version from the patch versions, you will need to reinitialize your project The global controller file has changed
All commands must be execute in your Rails root project directory
Gem Dependencies
- Ruby Version )= 2.5
- Rails Version 5:
gem 'rails', '~) 5'
- Rails Rspec Version )= 3.8:
gem 'rspec-rails', '~) 3.8'
- Faker Version )= 1.9:
gem 'faker', '~) 1.9'
Installation
Install in your gem file and a global install for this gem
Global Install:
gem install rails_on_rails
Gemfile Install:
gem 'rails_on_rails'
Initialize your project with Rails on Rails
If you run into hangups with spring, run spring stop
before your rails_on_rails
Then spring server
to resume spring
rails new tutorial
cd tutorial
rails_on_rails init
Before running the server add this initializer for the API Documentation
in your config/application.rb
add this inside of your Rails::Apllication inherited class
config.after_initialize do
require 'rake'
Rails.application.reload_routes!
Rails.application.load_tasks
Rake::Task["api_documentation:frontend_javascript"].invoke
end
In your config/routes.rb
add this route for your documentation page
resources :documentation, only: :index
You are now free to run the rails_on_rails model
command which has the same paramters as rails g model
But instead generates a controller for your model with unit tests to test the fully operationality controller after you declare your routes
and update a model assocations.
CLI Commands
All these commands have --help
flags with more descriptions on it's functionality.
For more details on the generated CLI Commands
Generate your CRUD Operations for Index, Show, Create, Update, & Destroy
The rails_on_rails model
command which has the same paramters as rails g model
But instead generates a controller for your model with unit tests to test the fully operationality controller after you declare your routes
and update a model assocations.
rails_on_rails model (ModelName) (*native-rails-model-generate-parameters)
By default all our basic CRUD operations are complete for: Index, Show, Create, Update, & Destroy There are other functions you can use as well
With your default CRUD operations you have many tools at your deposible
For all of your options, you can see all the available options in the API docs based on our schema in for that model
Querystring paramters
Index: -- include: Include will grab any associated tables with that endpoint separated by a comma (Check the docs for your schema's options) -- where: Where will be a custom where hash the string string format is a json object without curly brackets (Check the docs for your schema's options) -- like: Like will be a custom where string with a like operator all on all values given the string string format is a json object without curly brackets (Check the docs for your schema's options) -- order: Order will be a string passed into order the options are all separated by a comma in querystring (Check the docs for your schema's options)
Show: -- include: Include will grab any associated tables with that endpoint separated by a comma (Check the docs for your schema's options) -- where: Where will be a custom where hash the string string format is a json object without curly brackets (Check the docs for your schema's options)
Body paramters
All values except id, created_at, updated_at, or deleted_at are available by default you have the available to overwrite everything We use a custom method instead of params.permits For associated keys like 'user_id' provide an array with the first value being a symbol for the assocation ':user' The second value in the ActiveRecord you would like to use When using polymorphoric assocations, it is recommended to use a hash map of types and the value being the ActiveReord you provide in your schema with the search key being the string_type in the database
Bonus Methods
- bulk_create: Create multiple rows the paramter is 'bulk' which is an array of create hashes
params[:bulk] = [{ first_name: 'Mr. Robot' },{ first_name: 'Elloit }]
- bulk_update: Update multiple rows the paramter is 'bulk' which is a hash of ids with the update hash
params[:bulk] = { 1 =) { name: 'Luke Skywalker' }, 2 =) { name: 'Darth Vader' } }
- bulk_destroy: Destroy multiple rows the paramter is 'ids' which is an array of ids
- duplicate: Create a deep clone of any row in the database with all of it's assocations included the param is 'id' the table is for the controller associated to it
- bulk_csv_create: Create multiple rows the paramter is 'file' which is a csv file separated by commas and the first line is keys and the rest of values all separated by commas
Declaring bonus methods in config/routes.rb
resources :user, except: [:edit, :new] do
collection do
post 'duplicate'
post 'bulk_create'
post 'bulk_csv_create'
put 'bulk_update'
delete 'bulk_destroy'
end
end
API Documentation
The API Documentation requires CORS on your server, please set that up for all options for the web. Also setup your file uploads (multipart) to be independent of erb templating
For custom headers that you may add in the future update the lib/api_documentation.rake
Line 48: is where you can update the needed headers for your axios ajax requests.
var csrfHeader = { headers: { 'X-CSRF-Token': null } };
By default only json formats are unaccept for RESTful API JSON formatting
Querystring parameters
A querystring in the url looks like this
(protocol)://(host-name)/(path-name)(/path-name)?(querystring-key)=(querystring-value)&(querystring-key)=(querystring-value)
Anything after and including the question mark is a querystring You chain multiple querystring options spaced with '&'
The API Docs do the string maniplication for you under the hood.
The options for our API are:
- include = (include-options-separated-by-a-comma)
- where = (where-options-separated-by-a-comma)
- like = (like-options-separated-by-a-comma)
- order = (order-options-separated-by-a-comma)
Those options are given in the description for the endpoint you are testing
- Include will grab any associated tables with that endpoint
- Where will grab data where the options are true
- Like will grab data where the options are true but can be partially correct for searching
- Order will grab the data in the order you request
Body parameters
Body parameters are the key and value pairs you send in a request Those options are available as well Keeping the same key-value pair input UI with different functionality Under the 'Body' title
Path parameters
Path parameters are the key and value pairs you send in a request through the url Keeping the same key-value pair input UI however other the value will be in the url Under the 'Param' title
Body Data Type option
This dropdown is only used for file uploads by default they are set to JSON Use 'Form Data' for file uploads
For more details on the generated API Documentation
RSpec Unit Tests
Authenicated Routes
In your support file there is a called auth_helper. auth_helper.rb contains a login method. You can enter your own login endpoint here and pass in any needed params with email and password as an example so you can test authenicated routes with a JSON Web Token in your headers
Test Database Seeders
The database seeders that are generated are a template that work for most types of schemas. For custom schemas that may have unique constraints or unique behaviors. You can set a conditional to search for the unique column name and set a value for it to satsify your unique needs.
These seeders are only for your test database with randomized data from faker
Controller Specs
The controller spec has tests for the generated controller. After you declare your routes, you can restrict the tests for the routes you declare. The config_routes hash at the top will toggle those tests on or off because on what you set. By default all tests are true. Tests only cover basic CRUD resources: index, show, create, update, and destroy
For more details on the generated RSpec testing
Global Controller
There are many helpful util functions in the global controller at your deposal. You can inherit the GlobalController instead of ApplicationController for your new controllers. This gives you both the default Rails controller functionality and all these utils
In the autogeneration we utilize inheritance as much as posssible for cleaner controllers