Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/baccigalupi/repossessed
Single responsibility for your ActiveRecord world
https://github.com/baccigalupi/repossessed
Last synced: about 1 month ago
JSON representation
Single responsibility for your ActiveRecord world
- Host: GitHub
- URL: https://github.com/baccigalupi/repossessed
- Owner: baccigalupi
- License: mit
- Created: 2014-11-02T20:30:23.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-01T02:06:18.000Z (about 9 years ago)
- Last Synced: 2023-04-20T21:34:56.853Z (over 1 year ago)
- Language: Ruby
- Size: 164 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Repossessed
Repossessed provides [Single Responsibility](https://en.wikipedia.org/wiki/Single_responsibility_principle) for your ActiveRecord convenience.
ActiveRecord does everything for you. At first it seems like heaven. Then the callbacks become a complex maze of the unknown. Validations are applied with complex conditionals on the class level. All this happens because we are breaking single responsibility, and as the user stories generate many uses for the same record, our poor ActiveRecord object can't keep up with the complexity.Repossessed aims to be as easy as ActiveRecord, with convention over configuration and easy configuration. It does this by extracting the many responsibilities of an ActiveRecord object into a builder class. When the configuration becomes to complex, you can customize each of the underlying objects.
Repossessed uses the builder pattern to configure a class that parses params,
validates the params (yup, the params), saves and serializes to a json response
replete with a status and errors.Repossessed was originally created to generate JSON APIs. JSON params
were parsed, processed and then serialized seamlessly. In progress are
conveniences that make standard Rails view renders easier too.## Installation
Add this line to your application's Gemfile:
gem 'repossessed'
And then execute:
$ bundle
Or install it yourself as:
$ gem install repossessed
## Usage
### Creating A Do-Everything Repossessed Class
AdvocateManager = Repossessed::Config.build('Advocate') do
permit :name, :dob, :email
endThis kind of a class will filter down big params into just something with those
three keys.### Using Your Class
class MyController < ApplicationController
def create
render AdvocateManager.new(params).save
end
endBy default, #save will do everything update or save and then return a response:
{
json: {
name: 'Kane',
email: '[email protected]',
dob: 'long ass time ago',errors: {}
},status: 200
}If the save fails, the status is 400. If validations are defined, the errors get
populated.AdvocateManager = Repossessed::Config.build('Advocate') do
permit :name, :dob, :emailvalidate(:password, 'password must match confirmation') do |attr, attrs|
attrs[:password] == attrs[:password_confirmation]
end
endError response:
{
json: {
name: 'Kane',
email: '[email protected]',
dob: 'long ass time ago',errors: {
password: 'password must match confirmation'
}
},status: 400
}### But Why? ActiveRecord already does this??
We found that connecting our database directly to our UI through ActiveRecord and
Rails 4 params is initially easy, but gets expensive when product or our code assumptions
change. While building small single responsibility object solved this problem (hugely),
we saw the same kinds of code, over and over: parsing params,## Contributing
1. Fork it
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 new Pull Request