A framework for creating CRUDs in Rails APIs. https://croods-rails.netlify.app
Add this line to your application's Gemfile:
gem 'croods'
And then execute:
$ bundle
Resource is a generic abstraction for any object your app needs to represent. Instead of app/models/
and app/controllers/
, with croods we have app/resources/
.
To add a Project
resource to your app, start by generating a migration:
rails g migration CreateProjects
# It's crucial to write really solid migrations
# croods will use your database schema to build your resources.
class CreateProjects < ActiveRecord::Migration[5.2]
def change
create_table :projects do |t|
t.string :name, null: false
t.timestamps
end
end
end
Then create the module and the main file app/resources/projects/resource.rb
:
module Projects
class Resource < ApplicationResource
end
end
Last step is to initialize your resource in config/initializers/croods.rb
:
Croods.initialize_for(:users, :projects)
Croods creates five basic endpoints for your resource. If you don't want one, you need to skip its action:
module Projects
class Resource < ApplicationResource
skip_actions :index, :create, :update, :show, :destroy
end
end
By default, every single attribute in your table is exposed in your endpoints. If you don't want this, let croods know:
module Projects
class Resource < ApplicationResource
skip_attributes :created_at, :updated_at
end
end
Croods creates a model for your resource. It looks at your database and automatically infers your model's belongs_to
associations. But if you need to add code to your model just use extend_model
.
module Projects
class Resource < ApplicationResource
extend_model do
before_create :do_somethig
end
end
end
Protip: you can create a Model module and extend_model { include Projects::Model }
.
module Projects
module Model
extend ActiveSupport::Concern
included do
before_create :do_something
end
end
end
Croods uses devise_token_auth under the hood.
To customize which devise modules are loaded, you can pass them as arguments to use_for_authentication!
use_for_authentication!(
:database_authenticatable,
:recoverable,
:rememberable,
:trackable,
:validatable
)
You can manually check your changes in the dummy Rails app under /todos
.
Use it to run integration tests since it doesn't have an UI.
- Clone the repository
- Install bundler
gem install bundler
- Install gems
bin/bundle
To run both Croods-rails' and the example's specs use:
bin/rspec
The gem is available as open source under the terms of the MIT License.