The Rails API server for Canadian Pump & Packing Distribution. Serves up JSONAPI payloads for an EmberJs SPA over at https://github.com/cybertooth-io/ccpdist-com-emberjs.
You need the following:
- Ruby-2.3+ - suggest Ruby-2.5 but check your production environment to be sure -- e.g. AWS EB
- Docker - we use two containers, one for the PostgreSQL database and one for Redis
- The
config/master.key
file
Perform the following from the command line:
bundle install
- will install any of the missing gems declared in theGemfile
docker-compose up -d
- start up a Redis and PostgreSQL server in a containerrake db:create
- only required the first time, make sure your database is createdrake db:migrate
- run as required, your application will produce a stacktrace of errors if you're not up to daterake db:seed_fu
- run as required, to seed the database with datarake test
- run as required, test to make sure your API is behaving
rails s
- to serve the API on http://localhost:3000
For development, feel free to edit the db/fixtures/development/002_users.rb
file to add yourself.
Seed the database with:
$ rake db:seed_fu
Redis is used by JWTSessions to store Tokens and is also used by Sidekiq to queue up jobs.
JWTSessions is configured in config/initializers/jwt_session.rb
to use database 0
.
Sidekiq is configured in config/initializers/sidekiq.rb
to use database 1
.
If you're creating Sidekiq jobs please use the generator: rails g sidekiq:worker record_session_activity
- Create a model with its singular name:
rails g model role key:string name:string notes:text
- Edit the migration to ensure the
default
andnull
values are defined - Add validations, relationships, scopes, etc. to the new model class
- Is the model audited? Yes, then add the
audited
declaration to the model class - Add test fixture data accordingly to
test/fixtures/*.yml
(keep it general and un-crazy) - Unit test accordingly
- Add the model information to the
config/locales/*.yml
file(s)
- Edit the migration to ensure the
- Create the pundit policy with the model's singular name:
rails g pundit:policy role
- Make sure your policy file extends
ApplicationPolicy
(it should by default) - Override
create?
,destroy?
,index?
,show?
, andupdate?
accordingly - Unit test accordingly
- Add the policy error messages to the
config/locales/*.yml
if so desired
- Make sure your policy file extends
- Create the protected resource using the model's singular name at the appropriate api path:
rails g jsonapi:resource api/v1/protected/role
- Make sure the resource extends
BaseResource
- Add the appropriate attributes from the model that will be serialized in the JSONAPI payload
- Make sure all relationships you want exposed are added
- Add any filters that use model scopes
- Unit test accordingly through the controller (next step)
- Make sure the resource extends
- Create the protected controller using the model's plural name at the appropriate api path:
rails g controller api/v1/protected/roles
- Make sure the controller extends
BaseResourceController
- Add the controller's end points to the
config/routes.rb
file; usejsonapi_resources
helper :-) - Unit test accordingly (e.g. confirm returned payload only contains the fields specified in the resource)
- Make sure the controller extends
- Use a branch and a pull request into master.
- Run
rubocop -a
prior to commits to make sure your code conforms to the formatting and linting.
The config/application.rb
sets the record_session_activity
boolean which is used to determine whether
we should be logging session activity.
As of Rails-5.2 secrets are hashed and locked down with the config/master.key
file. Run rails credentials:help
for
more information.
Do you need to create a key? Use rake secret
Do you need to edit some secrets? Do it from the command line:
$ rails credentials:edit
secret_key_base
- used by Rails in many ways (e.g. BCrypt)
secret_jwt_encryption_key
- used by JWT & JWT_Sessions to create access & refresh tokens
- Confirm (and edit) the
config/application.rb
's version property. - Commit.
- Tag:
git tag v#.#.#
- Edit the
config/application.rb
's version property. - Commit & push everything.
Coming soon
Team members, create a branch and pull request.
General Public: Fork and create pull request.