Skip to content

Repository files navigation

RESTomatic

TestGem Version

RESTomatic helps Rails developers organize nested resources with automatic namespacing. Each nested resource gets its own controller module, making your app cleaner and easier to maintain.

Unlike Rails shallow routes that send everything to one controller, RESTomatic enforces proper separation: Blogs::PostsController, Users::PostsController, etc. keeping your controllers organized and your codebase more maintainable.

Installation

Add to your Rails application Gemfile:

bundleadd"restomatic"

That's it! The routing helpers are automatically available in your config/routes.rb file.

Then start using more RESTful routes in config/routes.rb:

resources:blogsdonest:postsdo# Blogs::PostsControllercollectiondoget:search# Blogs::PostsController#searchendendendresources:postsdocreate:comments# Posts::CommentsController#new, #createend

The Problem

Rails provides shallow routes and nested resources, but the syntax becomes verbose and repetitive, especially when you want to properly namespace controllers.

Before RESTomatic

resources:blogsdoscopemodule: :blogsdoresources:posts,only: %i[indexnewcreate]docollectiondoget:searchendendendendresources:postsdoscopemodule: :postsdoresources:comments,only: %i[newcreate]endend

With RESTomatic

resources:blogsdonest:postsdo# Blogs::PostsControllercollectiondoget:search# Blogs::PostsController#searchendendendresources:postsdocreate:comments# Posts::CommentsController#new, #createend

Much cleaner! The nest helper automatically:

  • Scopes to the parent resource's module (e.g., Blogs::PostsController)
  • Sets sensible defaults for which actions are included
  • Reduces boilerplate while maintaining Rails conventions

Route Helpers

nest

Nest resources under a parent with automatic module scoping and sensible action defaults.

resources:blogsdonest:posts# Creates index, new, create actions under Blogs::PostsControllernest:post# Singular: creates new, create actions (no edit/update/destroy)end

Options:

  • except: - Exclude specific actions (overrides defaults)
  • Singular resources default to excluding: [:edit, :update, :destroy]
  • Plural resources default to excluding: [:show, :edit, :update, :destroy]

Module-only nesting:

resources:postsdonestdo# Routes defined here will be scoped to Posts module# without creating a nested resourceget:analyticsendend

create

Define routes for creating a resource (new + create actions only).

resources:postsdocreate:comments# Only new and create actionsendcreate:session# Works with both singular and plural forms

edit

Define routes for editing a resource (edit + update actions only).

resources:postsdoedit:metadata# Only edit and update actionsend

show

Define routes for showing a resource (show action only).

resources:postsdoshow:preview# Only show actionend

destroy

Define routes for destroying a resource (destroy action only).

resources:postsdodestroy:attachment# Only destroy actionend

list

Define routes for listing resources (index action only).

resources:usersdolist:posts# Only index actionend

Real-World Example

Rails.application.routes.drawdoroot"home#index"# Public blog routesresources:blogs,only: [:index,:show]dolist:posts# Blogs::PostsController#indexend# Admin area with nested resourcesnamespace:admindoresources:blogsdonest:postsdo# Admin::Blogs::PostsControllercreate:comments# Admin::Blogs::Posts::CommentsController#new, #createcollectiondoget:scheduled# Admin::Blogs::PostsController#scheduledpost:bulk_publish# Admin::Blogs::PostsController#bulk_publishendendendresources:postsdoedit:seo# Admin::Posts::SeosController#edit, #updateshow:preview# Admin::Posts::PreviewsController#showdestroy:featured_image# Admin::Posts::FeaturedImagesController#destroyendend# User account managementresource:accountdonestdoedit:profile# Accounts::ProfilesController#edit, #updateedit:password# Accounts::PasswordsController#edit, #updateshow:billing# Accounts::BillingsController#showendendend

How It Works

RESTomatic extends ActionDispatch::Routing::Mapper to add these helper methods. The helpers automatically:

  1. Detect singular vs. plural resource names
  2. Apply appropriate module scoping
  3. Set sensible action defaults based on the helper used
  4. Work seamlessly with existing Rails routing features

Philosophy

Rails routing is powerful but can become verbose when building properly organized applications with shallow routes and namespaced controllers. RESTomatic embraces Rails conventions while reducing boilerplate, making your routes file more readable and maintainable.

Requirements

  • Rails 7.0+
  • Ruby 3.0+

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

The gem is available as open source under the terms of the MIT License.

About

Rapidly build controllers in Rails that map cleanly to resources and models

Resources

Stars

60 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages