apique 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +74 -0
- data/Rakefile +2 -0
- data/apique.gemspec +26 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/apique.rb +14 -0
- data/lib/apique/basics.rb +51 -0
- data/lib/apique/editable.rb +67 -0
- data/lib/apique/filterable.rb +91 -0
- data/lib/apique/listable.rb +103 -0
- data/lib/apique/paginatable.rb +38 -0
- data/lib/apique/pickable.rb +56 -0
- data/lib/apique/searchable.rb +7 -0
- data/lib/apique/sortable.rb +50 -0
- data/lib/apique/version.rb +3 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c185ec939c0e1327ab39f7a28e5c0d35c3638eb6
|
4
|
+
data.tar.gz: 446dbe26222728a725f071ff1682a40090f0178a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f522d9cedae7c11e5b1dbe3cf275435bb32141398c9648963352da0cb94daafff6d97918a3dbc99d9c4819f1e000d3dfc24948a332ce24d23944f6316f69ee20
|
7
|
+
data.tar.gz: 0194de1b634a6045f269945e9851c106ecde80358632bcb3f038bfb680547c02cf3ca7ec8b81098e727335429d52ead576350d9bf2bec511b3a4e9c81f439f84
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Sergey Baev, https://github.com/tinbka
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Apique
|
2
|
+
|
3
|
+
Apique modules replace tons of API cotrollers code which makes trivial actions including searching and CRUD, and provides front-end with thought-out errors and explanations.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'apique'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install apique
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
There is 8 modules with the following hierarchy:
|
24
|
+
```
|
25
|
+
- Basics (auth)
|
26
|
+
- - Pickable (singular actions)
|
27
|
+
- - - Editable (singular write actions)
|
28
|
+
- - Listable (plural actions)
|
29
|
+
- - - Filterable
|
30
|
+
- - - Sortable
|
31
|
+
- - - Paginatable
|
32
|
+
- - - Searchable (sum of the previous 3)
|
33
|
+
```
|
34
|
+
Modules suppose to be injected into an ActionController instance to provide needed functional, for example
|
35
|
+
```ruby
|
36
|
+
# In Rails 5 you may also want to inherit from ActionController::API
|
37
|
+
class ApiController < ApplicationController
|
38
|
+
include Apique::Searchable
|
39
|
+
include Apique::Editable
|
40
|
+
end
|
41
|
+
```
|
42
|
+
Since ApiController has both highest-level modules injected, it now provides all the functional and default controller actions which utilize all the power of Apique.
|
43
|
+
|
44
|
+
Now what is left is to make routes to these actions. In future releases it will be done by one engine mount based on the definition of #collection_name method.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
get 'api/:model' => 'api#index'
|
48
|
+
get 'api/:model/:id' => 'api#show'
|
49
|
+
post 'api/:model' => 'api#create'
|
50
|
+
patch 'api/:model/:id' => 'api#update'
|
51
|
+
delete 'api/:model/:id' => 'api#destroy'
|
52
|
+
```
|
53
|
+
|
54
|
+
In order to create controllers with _specific_ actions, just inherit from ApiController (in this example) and define routes... as usual!
|
55
|
+
|
56
|
+
## Current restrictions
|
57
|
+
|
58
|
+
For the moment #filter_collection! used in #index works only with PostgreSQL and #sort_collection! used in #index works only with SQL databases.
|
59
|
+
There will be support for other SQL DBs and Mongoid in the future.
|
60
|
+
|
61
|
+
## TODO
|
62
|
+
|
63
|
+
Describe search facilities and future plans.
|
64
|
+
|
65
|
+
## Development
|
66
|
+
|
67
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
68
|
+
|
69
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tinbka/apique.
|
74
|
+
|
data/Rakefile
ADDED
data/apique.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'apique/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "apique"
|
8
|
+
spec.version = Apique::VERSION
|
9
|
+
spec.authors = ["Sergey Baev"]
|
10
|
+
spec.email = ["tinbka@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Rails CRUD API replacement}
|
13
|
+
spec.description = %q{Apique modules replace tons of API cotrollers code which makes trivial actions including searching and CRUD, and provides front-end with thought-out errors and explanations.}
|
14
|
+
spec.homepage = "https://github.com/tinbka/apique"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
#spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
23
|
+
|
24
|
+
spec.add_dependency "cancancan"
|
25
|
+
spec.add_dependency "rails"
|
26
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "apique"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/apique.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "apique/version"
|
2
|
+
|
3
|
+
module Apique
|
4
|
+
extend ActiveSupport::Autoload
|
5
|
+
|
6
|
+
autoload :Basics
|
7
|
+
autoload :Editable
|
8
|
+
autoload :Filterable
|
9
|
+
autoload :Listable
|
10
|
+
autoload :Paginatable
|
11
|
+
autoload :Pickable
|
12
|
+
autoload :Searchable
|
13
|
+
autoload :Sortable
|
14
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# Basic functional for API implementation.
|
2
|
+
module Apique::Basics
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
protect_from_forgery with: :null_session
|
7
|
+
|
8
|
+
before_action :authorize_access
|
9
|
+
|
10
|
+
rescue_from CanCan::AccessDenied do |e|
|
11
|
+
render json: {error: e.message}, status: :forbidden
|
12
|
+
end
|
13
|
+
|
14
|
+
rescue_from NotImplementedError do |e|
|
15
|
+
render json: {error: e.message}, status: :not_acceptable
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
### Authorization ###
|
23
|
+
|
24
|
+
# @virtual
|
25
|
+
# Used to immediately halt request processing before any other action would be done.
|
26
|
+
# E.g. call `authorize! :access, :admin` inside an admin-only controller
|
27
|
+
# or check a token in a secure public controller.
|
28
|
+
def authorize_access
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
### Basic getters-setters ###
|
33
|
+
|
34
|
+
# @return [Ability]
|
35
|
+
def current_ability
|
36
|
+
@current_ability ||= Ability.new current_user
|
37
|
+
end
|
38
|
+
|
39
|
+
# The plural name of the ORM class.
|
40
|
+
# @return [String]
|
41
|
+
def collection_name
|
42
|
+
@collection_name ||= params[:model]
|
43
|
+
end
|
44
|
+
|
45
|
+
# The ORM class of the resource.
|
46
|
+
# @return [Class]
|
47
|
+
def resource_class
|
48
|
+
@resource_class ||= collection_name.singularize.classify.constantize
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# Basic functional for creating, updating, and destroying records.
|
2
|
+
module Apique::Editable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Apique::Pickable
|
5
|
+
|
6
|
+
included do
|
7
|
+
rescue_from ActionController::ParameterMissing do |e|
|
8
|
+
render json: {error: e.message}, status: :bad_request
|
9
|
+
end
|
10
|
+
|
11
|
+
rescue_from ActionController::UnpermittedParameters, ActiveModel::ForbiddenAttributesError do |e|
|
12
|
+
render json: {error: e.message}, status: :unprocessable_entity
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
# A resource is already built by CanCan according to load_resource params in the current controller.
|
18
|
+
# POST /api/{plural_resource_name}
|
19
|
+
def create
|
20
|
+
if get_resource.save
|
21
|
+
render :show, status: :created
|
22
|
+
else
|
23
|
+
render json: {errors: get_resource.errors}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# PATCH/PUT /api/{plural_resource_name}/{id}
|
28
|
+
def update
|
29
|
+
if get_resource.update(update_api_params)
|
30
|
+
render :show
|
31
|
+
else
|
32
|
+
render json: {errors: get_resource.errors}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# DELETE /api/{plural_resource_name}/{id}
|
37
|
+
def destroy
|
38
|
+
get_resource.destroy
|
39
|
+
unless get_resource.errors.present?
|
40
|
+
on_destroy
|
41
|
+
else
|
42
|
+
render json: {errors: get_resource.errors}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
### Default callbacks ###
|
50
|
+
|
51
|
+
def on_destroy
|
52
|
+
head :no_content
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
### Basic getters-setters ###
|
57
|
+
|
58
|
+
# Only allow a trusted parameter "white list" through.
|
59
|
+
# If a single resource is loaded for #create or #update,
|
60
|
+
# then the controller for the resource must implement
|
61
|
+
# the method "update_params" to limit permitted
|
62
|
+
# parameters for the individual model.
|
63
|
+
def update_api_params
|
64
|
+
@resource_params ||= self.send('update_params')
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# Basic functional to perform detailed filtering inside record collections.
|
2
|
+
module Apique::Filterable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Apique::Listable
|
5
|
+
|
6
|
+
included do
|
7
|
+
params_usage['q'] = {
|
8
|
+
desc: "q [String] filter by a value of a default field (optional)",
|
9
|
+
example: "q=abc requests to filter a default column(s) by abc substring"
|
10
|
+
}
|
11
|
+
params_usage['q[]'] = {
|
12
|
+
desc: "q [Object] filter by a value of a field specified as object key (optional, overrides plain `q')",
|
13
|
+
example: "q[amount]=123&q[title_or_description]=abc requests to filter by amount equal to 123 AND (title OR description contains abc substring)"
|
14
|
+
}
|
15
|
+
params_types['q'] = [String, Hash]
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
### Basic getters-setters ###
|
22
|
+
|
23
|
+
# @abstract
|
24
|
+
# Filter by this column implicitly when q=value is passed. Supports field_or_field2 notation.
|
25
|
+
# @return [String]
|
26
|
+
def default_filter_column
|
27
|
+
raise NotImplementedError, "A developer must implement default_filter_column for this controller to allow plain `q' query parameter."
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
### Whitelists ###
|
32
|
+
|
33
|
+
# @virtual
|
34
|
+
# Pattern method for params a user can filter by. Add whitelisting logic in a descendant using `super`.
|
35
|
+
# @return [ActionController::Parameters]
|
36
|
+
def filter_params
|
37
|
+
params[:q].presence || ActionController::Parameters.new
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
### Collection filters ###
|
42
|
+
|
43
|
+
# Filter the current collection with {column_or_scope => value, ... } Hash within params[:q]
|
44
|
+
# @return [DB collection proxy]
|
45
|
+
def filter_collection!
|
46
|
+
if params[:q].is_a? String
|
47
|
+
params[:q] = {default_filter_column => params[:q]}
|
48
|
+
return filter_collection!
|
49
|
+
end
|
50
|
+
|
51
|
+
if filter_params.present?
|
52
|
+
collection = get_collection
|
53
|
+
|
54
|
+
filter_params.each do |k, v|
|
55
|
+
next if v.blank?
|
56
|
+
|
57
|
+
if resource_class.respond_to? "search_by_#{k}"
|
58
|
+
# A call to a scope. A method must be defined as a scope to work on another ORM relation.
|
59
|
+
collection = collection.public_send "search_by_#{k}", v
|
60
|
+
else
|
61
|
+
is_text = [ActiveRecord::Type::String, ActiveRecord::Type::Text].include? resource_class.columns.find {|i| i.name == k}.cast_type
|
62
|
+
|
63
|
+
if k =~ /_or_/
|
64
|
+
fields = k.split('_or_')
|
65
|
+
|
66
|
+
if is_text
|
67
|
+
collection = collection.where [
|
68
|
+
fields.map {|i| "#{i} ILIKE ?"} * ' OR ',
|
69
|
+
*(["%#{v}%"] * fields.size)
|
70
|
+
]
|
71
|
+
else
|
72
|
+
collection = collection.where [
|
73
|
+
fields.map {|i| "#{i} = ?"} * ' OR ',
|
74
|
+
*([v] * fields.size)
|
75
|
+
]
|
76
|
+
end
|
77
|
+
else
|
78
|
+
if is_text
|
79
|
+
collection = collection.where ["#{k} ILIKE ?", "%#{v}%"]
|
80
|
+
else
|
81
|
+
collection = collection.where k => v
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
set_collection collection
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# Basic functional for retrieving collections of records.
|
2
|
+
module Apique::Listable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Apique::Basics
|
5
|
+
|
6
|
+
included do
|
7
|
+
# [Hash: param_key_string => {:desc, :example}]
|
8
|
+
class_attribute :params_usage
|
9
|
+
self.params_usage = {}
|
10
|
+
# [Hash: param_key_string => param_types_array]
|
11
|
+
class_attribute :params_types
|
12
|
+
self.params_types = {}
|
13
|
+
|
14
|
+
helper_method :subject_collection
|
15
|
+
|
16
|
+
before_action :validate_query, only: :index
|
17
|
+
|
18
|
+
rescue_from Apique::MalformedParameters do |e|
|
19
|
+
render json: e, status: :bad_request
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# Exception will raise if a request query would contain an incorrect of malformed parameter.
|
25
|
+
# This will provide the front-end with a description of the correct API usage.
|
26
|
+
class Apique::MalformedParameters < Exception
|
27
|
+
|
28
|
+
def initialize(request, param, params_usage, *args)
|
29
|
+
message = "Bad request query: malformed parameter #{param}"
|
30
|
+
usage = "GET #{request.fullpath[/[^?]+/]}?#{params_usage.keys*'&'}"
|
31
|
+
|
32
|
+
@json = {message: message, usage: usage, params: params_usage.values}
|
33
|
+
|
34
|
+
message = [
|
35
|
+
message, "Usage: #{usage}",
|
36
|
+
*params_usage.values.flat_map {|v| [v[:desc], "Example: #{v[:example]}"]}
|
37
|
+
]*"\n"
|
38
|
+
|
39
|
+
super(message, *args)
|
40
|
+
end
|
41
|
+
|
42
|
+
def as_json(*)
|
43
|
+
@json
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
# GET /api/{plural_resource_name}
|
50
|
+
def index
|
51
|
+
if respond_to? :filter_collection!, true
|
52
|
+
filter_collection!
|
53
|
+
end
|
54
|
+
if respond_to? :sort_collection!, true
|
55
|
+
sort_collection!
|
56
|
+
end
|
57
|
+
if respond_to? :paginate_collection!, true
|
58
|
+
paginate_collection!
|
59
|
+
end
|
60
|
+
render json: subject_collection
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
### Basic getters-setters ###
|
67
|
+
|
68
|
+
# Returns the collection from the created instance variable.
|
69
|
+
# @return [DB collection proxy]
|
70
|
+
def get_collection
|
71
|
+
instance_variable_get("@#{collection_name}") || set_collection
|
72
|
+
end
|
73
|
+
|
74
|
+
# Used as a helper to create DRYed json builders in inheritable controllers.
|
75
|
+
alias_method :subject_collection, :get_collection
|
76
|
+
|
77
|
+
# Puts the collection into an instance variable.
|
78
|
+
# In most cases, the collection exactly allowed for the user by CanCan rules should be an entry point.
|
79
|
+
# @return [DB collection proxy]
|
80
|
+
def set_collection(collection = resource_class.accessible_by(current_ability))
|
81
|
+
instance_variable_set("@#{collection_name}", collection)
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
### Whitelists ###
|
86
|
+
|
87
|
+
# Check consistency of a query parameters for a list. Use it as a before filter if needed.
|
88
|
+
# @raise [MalformedParameters] if the request query is malformed.
|
89
|
+
def validate_query
|
90
|
+
request.query_parameters.each do |k, v|
|
91
|
+
param_is_valid = self.class.params_types.find do |name, types|
|
92
|
+
if k == name
|
93
|
+
break true if types.any? {|type| v.is_a? type}
|
94
|
+
raise MalformedParameters.new(request, k, self.class.params_usage)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
unless param_is_valid
|
98
|
+
raise MalformedParameters.new(request, k, self.class.params_usage)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Basic functional for paginating record collections.
|
2
|
+
module Apique::Paginatable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Apique::Listable
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_attribute :per_page
|
8
|
+
|
9
|
+
params_usage['page'] = {
|
10
|
+
desc: "",
|
11
|
+
example: "page=20"
|
12
|
+
}
|
13
|
+
params_types['page'] = [String]
|
14
|
+
|
15
|
+
set_per_page 25
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
|
20
|
+
def set_per_page(number)
|
21
|
+
self.per_page = number
|
22
|
+
params_usage['page'][:desc] = "page [Integer] 1-based number of a bunch of #{number} items list (optional)"
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
### Collection filters ###
|
31
|
+
|
32
|
+
# Get 1-based params[:page]-th page of the current collection.
|
33
|
+
# @return [DB collection proxy]
|
34
|
+
def paginate_collection!
|
35
|
+
set_collection get_collection.page(params[:page]).per(self.class.per_page)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Basic functional for retrieving discrete records.
|
2
|
+
module Apique::Pickable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Apique::Basics
|
5
|
+
|
6
|
+
included do
|
7
|
+
helper_method :subject_resource
|
8
|
+
|
9
|
+
rescue_from ActiveRecord::RecordNotFound, Apique::RecordNotFound do |e|
|
10
|
+
render json: {error: e.message}, status: :not_found
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# This exception will be raised if a request would query for an inexisted record and
|
15
|
+
# this case would not have been handled by CanCan.
|
16
|
+
class Apique::RecordNotFound < ActionController::RoutingError; end
|
17
|
+
|
18
|
+
|
19
|
+
# GET /api/{plural_resource_name}/{id}
|
20
|
+
def show
|
21
|
+
render json: subject_resource
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
### Basic getters-setters ###
|
28
|
+
|
29
|
+
# The singular name of the ORM class.
|
30
|
+
# @return [String]
|
31
|
+
def resource_name
|
32
|
+
@resource_name ||= collection_name.singularize
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the resource from the created instance variable.
|
36
|
+
# @return [Object]
|
37
|
+
def get_resource
|
38
|
+
instance_variable_get("@#{resource_name}") || set_resource
|
39
|
+
end
|
40
|
+
|
41
|
+
# Used as a helper to create DRYed json builders in inheritable controllers.
|
42
|
+
alias_method :subject_resource, :get_resource
|
43
|
+
|
44
|
+
# Puts the resource into an instance variable.
|
45
|
+
# @raise [ActionController::RoutingError] if there is no resource found.
|
46
|
+
# @return [Object]
|
47
|
+
def set_resource(resource = nil)
|
48
|
+
resource ||= resource_class.find(params[:id])
|
49
|
+
unless resource
|
50
|
+
raise RecordNotFound, "could not find a record of type #{resource_class} with id = #{params[:id]}"
|
51
|
+
end
|
52
|
+
|
53
|
+
instance_variable_set("@#{resource_name}", resource)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Basic functional for ordering record collections.
|
2
|
+
module Apique::Sortable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include Apique::Listable
|
5
|
+
|
6
|
+
included do
|
7
|
+
params_usage['s'] = {
|
8
|
+
desc: "s [Object] sort by a field in a specified direction (optional)",
|
9
|
+
example: "s[id]=1&s[name]=-1&s[price] requests to sort by by id (most recent last), name (Z to A), and price (lower to higher)"
|
10
|
+
}
|
11
|
+
params_types['s'] = [Hash]
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
### Whitelists ###
|
18
|
+
|
19
|
+
# @virtual
|
20
|
+
# Pattern method for params a user can sort by. Add whitelisting logic in a descendant using `super`.
|
21
|
+
# @return [ActionController::Parameters]
|
22
|
+
def sort_params
|
23
|
+
params[:s].presence || ActionController::Parameters.new
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
### Collection filters ###
|
28
|
+
|
29
|
+
# Sort the current collection by {column => direction, ... } Hash within params[:s]
|
30
|
+
# @return [DB collection proxy]
|
31
|
+
def sort_collection!
|
32
|
+
collection = get_collection
|
33
|
+
|
34
|
+
if sort_params.present?
|
35
|
+
sort_params.each do |k, v|
|
36
|
+
if v.present? and ['desc', '-1'].include? v.downcase
|
37
|
+
collection = collection.order "#{k} desc"
|
38
|
+
else
|
39
|
+
collection = collection.order "#{k} asc"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
if collection.order_values.blank?
|
44
|
+
collection = collection.order "id desc"
|
45
|
+
end
|
46
|
+
|
47
|
+
set_collection collection
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apique
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Baev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: cancancan
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Apique modules replace tons of API cotrollers code which makes trivial
|
84
|
+
actions including searching and CRUD, and provides front-end with thought-out errors
|
85
|
+
and explanations.
|
86
|
+
email:
|
87
|
+
- tinbka@gmail.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- apique.gemspec
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/apique.rb
|
101
|
+
- lib/apique/basics.rb
|
102
|
+
- lib/apique/editable.rb
|
103
|
+
- lib/apique/filterable.rb
|
104
|
+
- lib/apique/listable.rb
|
105
|
+
- lib/apique/paginatable.rb
|
106
|
+
- lib/apique/pickable.rb
|
107
|
+
- lib/apique/searchable.rb
|
108
|
+
- lib/apique/sortable.rb
|
109
|
+
- lib/apique/version.rb
|
110
|
+
homepage: https://github.com/tinbka/apique
|
111
|
+
licenses: []
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.4.8
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Rails CRUD API replacement
|
133
|
+
test_files: []
|