Skip to content

Commit 9307231

Browse files
📝 Improves README.md
1 parent bfea25b commit 9307231

File tree

1 file changed

+75
-8
lines changed

1 file changed

+75
-8
lines changed

README.md

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
# Rest Api Generator
1+
# rest-api-generator
22

3-
This gem helps your to build a Ruby on Rails REST api, using a scaffold generator following the best pratices.
3+
This gem helps you to build a Ruby on Rails REST API, using a scaffold generator following the best practices.
44

55
## How it works?
66

7-
We use SwitchDreams default way to make controller using RSpec and FactoryBot for testing and use a custom expection to centralize error handle like this article https://medium.com/rails-ember-beyond/error-handling-in-rails-the-modular-way-9afcddd2fe1b
7+
The gems use rails generators and some templates to create all resources needed to build an REST API.
8+
9+
The controller is built following SwitchDreams default way:
10+
11+
- Using an error handler module to deal with a custom exception for centralize error handler following
12+
this article https://medium.com/rails-ember-beyond/error-handling-in-rails-the-modular-way-9afcddd2fe1b.
13+
- For tests, we use RSpec and FactoryBot.
814

915
## Installation
1016

@@ -23,6 +29,7 @@ Or install it yourself as:
2329
$ gem install rest-api-generator
2430

2531
## Requirements
32+
2633
You need to have installed in your application rspec and factory bot
2734

2835
<ul>
@@ -31,31 +38,91 @@ You need to have installed in your application rspec and factory bot
3138
</ul>
3239

3340
## Usage
41+
3442
### Generate Resource
43+
3544
```bash
36-
$ rails g rest-api-generator-resource table_name attributes
45+
$ rails g rest-api-generator:resource table_name attributes
3746
```
47+
3848
This command will create:
49+
3950
- **Model and Migration**: Using rails default model generator
4051
- **Controller**: A controller with index,show,create,update and destroy methods.
4152
- **Specs for the created controller**
4253
- **Factory bot factory for created model**
4354
- **Routes**: with rails resources
4455

56+
### Example
57+
58+
```bash
59+
$ rails g rest-api-generator:resource car name:string color:string
60+
```
61+
62+
Will generate following controller and the other files:
63+
64+
```ruby
65+
66+
class CarsController < ApplicationController
67+
before_action :set_car, only: %i[show update destroy]
68+
69+
def index
70+
@car = Car.all
71+
render json: @car, status: :ok
72+
end
73+
74+
def show
75+
render json: @car, status: :ok
76+
end
77+
78+
def create
79+
@car = Car.create!(car_params)
80+
render json: @car, status: :created
81+
end
82+
83+
def update
84+
@car = Car.update!(car_params)
85+
render json: @car, status: :ok
86+
end
87+
88+
def destroy
89+
@car.destroy!
90+
end
91+
92+
private
93+
94+
def set_car
95+
@car = Car.find(params[:id])
96+
end
97+
98+
def car_params
99+
params.require(:car).permit(:name, :color)
100+
end
101+
end
102+
103+
```
104+
45105
## Development
46106

47-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
108+
also run `bin/console` for an interactive prompt that will allow you to experiment.
48109

49-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
110+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
111+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
112+
push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50113

51114
## Contributing
52115

53-
Bug reports and pull requests are welcome on GitHub at https://github.com/SwitchDreams/rest-api-generator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/SwitchDreams/rest-api-generator/blob/master/CODE_OF_CONDUCT.md).
116+
Bug reports and pull requests are welcome on GitHub at https://github.com/SwitchDreams/rest-api-generator. This project
117+
is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
118+
the [code of conduct](https://github.com/SwitchDreams/rest-api-generator/blob/master/CODE_OF_CONDUCT.md).
54119

55120
## License
56121

57122
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
58123

59124
## Code of Conduct
60125

61-
Everyone interacting in the Rest::Api::Generator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rest-api-generator/blob/master/CODE_OF_CONDUCT.md).
126+
Everyone interacting in the Rest::Api::Generator project's codebases, issue trackers, chat rooms and mailing lists is
127+
expected to follow
128+
the [code of conduct](https://github.com/[USERNAME]/rest-api-generator/blob/master/CODE_OF_CONDUCT.md).

0 commit comments

Comments
 (0)