Baseapi
When you create a web application in the rails, If you want to CRUD operations in Ajax, might this gem is useful. We only define the empty Controller and Model for us to define the CRUD in.
Installation
Add this line to your application's Gemfile:
gem 'baseapi'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install baseapi
Usage
Introduction create a BaseApiController & JBuilder of view:
$ bundle exec baseapi setup
Create a Model (app/models/user.rb):
class User < ActiveRecord::Base
end
Extend the BaseApiController is when you create a Controller (app/controllers/users_controller.rb):
class UsersController < BaseApiController
end
Routing configuration (config/routes.rb):
constraints(:format => /json/) do
get "users" => "users#index"
get "users/:id" => "users#show"
post "users" => "users#create"
patch "users/:id" => "users#update"
put "users/:id" => "users#update"
delete "users/:id" => "users#destroy"
end
Corresponding API:
| url | action | method |
|---|---|---|
| /users.json | index | GET |
| /users/id.json | show | GET |
| /users.json | create | POST |
| /users/id.json | update | PATCH / PUT |
| /users/id.json | destroy | DELETE |
Examples
Model
class User < ActiveRecord::Base
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :users
end
Users table data
| id | name | company_id |
|---|---|---|
| 1 | hoge | 1 |
| 2 | huga | 2 |
Company table data
| id | name |
|---|---|
| 1 | |
| 2 | Apple |
action index
Get all
GET /users.json
Specify the count
GET /users.json?count=10
Specify the page
GET /users.json?count=10&page=2
Specify the sorting order
GET /users.json?order=desc&orderby=name
Specify the name
GET /users.json?name=hoge
Specify multiple possible
GET /users.json?name[]=hoge&name[]=huga
Specify the not id (v0.1.7~)
GET /users.json?id=!1
Of course the other is also possible (v0.1.7~)
GET /users.json?name=!hoge
Specify the null name (v0.1.7~)
GET /users.json?name=null
Specify the not null name (v0.1.7~)
GET /users.json?name=!null
Specify the empty string and null name (v0.1.8~)
GET /users.json?name=empty
Specify the not empty string and null name (v0.1.8~)
GET /users.json?name=!empty
Specify search for simply string 'empty' (v0.1.8~)
It can also be in double quotes "empty"
GET /users.json?name='empty'
Specify the belongs to company name
Note that this is a single
GET /users.json?company[name]=Google
Specify the has many users name
Note that this is a multiple
GET /companies.json?users[name]=hoge
Relationships can now specify multiple (v0.1.9~)
Specify the User belong to a development part company
GET /users.json?company[units][name]=development
action show
Get id 1
GET /users/1.json
action create
Create a user name is 'hoge'
POST /users.json?name=hoge
action update
Update the name to 'huga'
PATCH /users/1.json?name=huga
PUT /users/1.json?name=huga
action delete
Delete the id to 1
DELETE /users/1.json
return json format
{
error: false,
message: "",
data: [
{
id: 1,
name: "hoge"
},
{
id: 2,
name: "huga"
}
]
}
Override
You can corresponding to the logical deletion, if you want to search condition to like and or, you will be able to override the processing in the Model
Get all
class User < ActiveRecord::Base
module ClassMethods
def self._all
self.all # default
end
end
delete
class User < ActiveRecord::Base
def _destroy
self.destroy # default
end
end
column search
class User < ActiveRecord::Base
module ClassMethods
def self._where(models, column, values)
column_match(models, column, values) # default
end
end
name column search
class User < ActiveRecord::Base
module ClassMethods
def self._where_name(models, column, values)
column_match(models, column, values) # default
end
end
belongs_to search
class User < ActiveRecord::Base
module ClassMethods
def self._belongs_to(models, table, hash)
relation_match(models, table, hash) # default
end
end
company belongs_to search
class User < ActiveRecord::Base
module ClassMethods
def self._belongs_to_company(models, table, hash)
relation_match(models, table, hash) # default
end
end
If there are multiple related belongs_to
def self._belongs_to_company_units_...(models, table, hash)
has_many search
class Company < ActiveRecord::Base
module ClassMethods
def self._has_many(models, table, hash)
relation_match(models, table, hash) # default
end
end
users has_many search
class Company < ActiveRecord::Base
module ClassMethods
def self._has_many_users(models, table, hash)
relation_match(models, table, hash) # default
end
end
If there are multiple related has_many
def self._has_many_users_families_...(models, table, hash)
like & match, or & and Search (v0.1.3~)
There is a useful function for the table Search By default, it looks like the following 'Like' search and, you can change the 'and' and 'or'
Simply If you want to override the search processing of the name column column_match, you can use the column_like function.
class User < ActiveRecord::Base
def self._where_name(models, column, values)
column_match(models, column, values, operator:'or') # default is match OR
# column_like(models, column, values, operator:'or') # LIKE OR
# column_like(models, column, values, operator:'and') # LIKE AND
end
end
If the search process of the related table is to override relation_match, you can use the relation_like function.
class User < ActiveRecord::Base
def self._belongs_to_company(models, table, hash)
relation_match(models, table, hash, operator:'or') # default is match OR
# relation_like(models, table, hash, operator:'or') # LIKE OR
# relation_like(models, table, hash, operator:'and') # LIKE AND
end
end
The short so please read the code for more information
hook action (v0.1.4~)
Controller of 'create, update, destroy' function in advance by attaching the prefix of before, you can post processing Delete the related table or may be useful for error handling It may be good even before_action, but you may use if you want to process in the transaction. It is always surrounded by model of transaction.
class CompaniesController < BaseApiController
# Name Required items
def before_create
if params['name'].blank?
raise 'Please enter your name'
end
end
# delete the relation table
def before_destroy
User.where('company_id = ?', @model.id).each do |user|
user.destroy()
end
end
end
And if not sent the name to api in the above example, it returns an error in the json. Message is a string that was passed to raise.
{
error: true,
message: "Please enter your name",
}
jbuilder
It uses basically
/app/views/baseapi/ooo.json.jbuilder
but you can also make
/app/views/{models}/ooo.json.jbuilder
It will return to a single data (action:show,create,delete,update)
model.json.jbuilder
It will return multiple data (action:index)
models.json.jbuilder
It will return an error content
error.json.jbuilder
Development
After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment. Run bundle exec baseapi to use the code located in this directory, ignoring other installed copies of this gem.
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 to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
- Fork it ( https://github.com/arakawamoriyuki/baseapi/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request