https://github.com/monade/solipsist
An approach to write rails controllers in a super compact way.
https://github.com/monade/solipsist
rails ruby
Last synced: 8 months ago
JSON representation
An approach to write rails controllers in a super compact way.
- Host: GitHub
- URL: https://github.com/monade/solipsist
- Owner: monade
- License: mit
- Created: 2022-03-28T14:33:38.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-01-20T20:55:40.000Z (over 1 year ago)
- Last Synced: 2025-01-20T21:19:44.834Z (over 1 year ago)
- Topics: rails, ruby
- Language: Ruby
- Homepage:
- Size: 29.3 KB
- Stars: 2
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

# Solipsist
A simplified way to write JSON APIs in Rails, taking use of Cancancan and ActiveModel::Serializers.
Less code to write, more fun!
## Installation
Add the gem to your Gemfile
```ruby
gem 'solipsist', github: 'monade/solipsist'
```
## Philosophy
Why keep re-implementing the same things?
A CRUD looks always the same in 90% of cases. So why don't have a default set of actions, and override what is different?
## Usage
Add Solipsist to your ApplicationController:
```ruby
class ApplicationController < ActionController::Base
include Solipsist
end
```
Now the controller will be super dry!
```ruby
# Router
api_resources :people
# Controller
class PeopleController < ApplicationController
load_and_authorize_resource
def index
default! @people
end
def show
default! @person
end
def create
default! @person
end
def update
default! @person
end
def destroy
default! @person
end
private
def create_params
params.permit(:name, :email)
end
def update_params
params.permit(:name, :email)
end
end
```
### Mass actions
You just have to add the `accept_mass_actions` method to your controller!
```ruby
# Router
api_mass_resources :people
# Controller
class PeopleController < ApplicationController
accept_mass_actions
load_and_authorize_resource
def index
default! @people
end
def show
default! @person
end
def create
default! @person
end
def update
default! @person
end
def destroy
default! @person
end
private
def create_params
params.permit(:name, :email)
end
def update_params
params.permit(:name, :email)
end
end
```