resourcey 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -3
- data/lib/resourcey/controller.rb +2 -2
- data/lib/resourcey/controller_filtering.rb +16 -0
- data/lib/resourcey/controller_modules.rb +3 -0
- data/lib/resourcey/controller_pagination.rb +2 -2
- data/lib/resourcey/errors.rb +1 -1
- data/lib/resourcey/filter.rb +33 -0
- data/lib/resourcey/filter_processor.rb +35 -0
- data/lib/resourcey.rb +2 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5348dbbfc10aec535cb095b78c129e9c8156c307
|
4
|
+
data.tar.gz: e7bd3e77e11450f4ea48bf33df1159fccce6360c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ebf6b3b8b417a0b155a6f228cc7f20a321bbe405286c8bd4fc6a241bb49fecca8fc2589c2c59e0084d30e5b1489b4f3257151aebf3aa2fc62936ae1366d3163
|
7
|
+
data.tar.gz: cc7c5945c9020292441d3886e53d35575f53a379b63a335e944505e05d748b82805543b7a041df698a2eb046f44ea14576cdb16035ef3757d71b5b7bb3b4a2a2
|
data/README.md
CHANGED
@@ -3,6 +3,17 @@
|
|
3
3
|
|
4
4
|
A lightweight rails gem for building resource-based APIs.
|
5
5
|
|
6
|
+
### Table of contents
|
7
|
+
- [Installation](#installation)
|
8
|
+
- [Usage](#usage)
|
9
|
+
- [Controller](#controller)
|
10
|
+
- [Serialization](#serialization)
|
11
|
+
- [How to fetch](#fetch-the-data)
|
12
|
+
- [Pagination](#pagination)
|
13
|
+
- [Filtering](#filtering)
|
14
|
+
- [Configuration](#configuration)
|
15
|
+
- [Contributing](#contributing)
|
16
|
+
|
6
17
|
## Installation
|
7
18
|
Add Resourcey to your `Gemfile`:
|
8
19
|
```ruby
|
@@ -20,6 +31,7 @@ gem install resourcey
|
|
20
31
|
```
|
21
32
|
|
22
33
|
## Usage
|
34
|
+
|
23
35
|
### Controller
|
24
36
|
For a resource called `user`, just create `UsersController`:
|
25
37
|
```ruby
|
@@ -29,8 +41,7 @@ end
|
|
29
41
|
|
30
42
|
For further reading, [click here](/docs/CONTROLLER.md).
|
31
43
|
|
32
|
-
###
|
33
|
-
|
44
|
+
### Serialization
|
34
45
|
Now you need a serializer, let's assume that `User` has an email and an ID, go with this:
|
35
46
|
```ruby
|
36
47
|
class UserSerializer < Resourcey::Serializer
|
@@ -72,6 +83,19 @@ This will fetch page 3, with 10 resources per single page. That's all! Paginatio
|
|
72
83
|
|
73
84
|
For further reading, [click here](/docs/PAGINATION.md).
|
74
85
|
|
86
|
+
### Filtering
|
87
|
+
You can filter your resources using filter objects defined per single resource model. See below for an example:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class UserFilter < Resourcey::Filter
|
91
|
+
filter :older_than do |value, scope|
|
92
|
+
scope.where('users.age > ?', value)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
For further reading, [click here](/docs/FILTERING.md).
|
98
|
+
|
75
99
|
## Configuration
|
76
100
|
Create configuration file in your `config/initializers` folder, and configure as usual:
|
77
101
|
```ruby
|
@@ -83,7 +107,7 @@ end
|
|
83
107
|
### Available config variables
|
84
108
|
- `default_paginator` - name of paginator that will be used in every controller, if not configured on controller-level (default: `:paged`)
|
85
109
|
|
86
|
-
##
|
110
|
+
## Contributing
|
87
111
|
If you want to take part in developing resourcey, fork this repository, commit your code, and create pull request.
|
88
112
|
|
89
113
|
### Requirements
|
data/lib/resourcey/controller.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'resourcey/
|
2
|
-
require 'resourcey/controller_model'
|
1
|
+
require 'resourcey/controller_modules'
|
3
2
|
|
4
3
|
module Resourcey
|
5
4
|
class Controller < ActionController::Base
|
5
|
+
include Resourcey::ControllerFiltering
|
6
6
|
include Resourcey::ControllerPagination
|
7
7
|
include Resourcey::ControllerModel
|
8
8
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Resourcey
|
2
|
+
module ControllerFiltering
|
3
|
+
def filtered_resources
|
4
|
+
return resources if filter_class.nil?
|
5
|
+
|
6
|
+
filter_instance = filter_class.new(params)
|
7
|
+
filter_instance.apply(resources)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def filter_class
|
13
|
+
"#{resource_model}Filter".safe_constantize
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -11,10 +11,10 @@ module Resourcey
|
|
11
11
|
private
|
12
12
|
|
13
13
|
def paginated_resources
|
14
|
-
return
|
14
|
+
return filtered_resources unless self.pagination_enabled
|
15
15
|
|
16
16
|
paginator = current_paginator_class.new(params)
|
17
|
-
paginator.paginate(
|
17
|
+
paginator.paginate(filtered_resources)
|
18
18
|
end
|
19
19
|
|
20
20
|
def current_paginator_class
|
data/lib/resourcey/errors.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Resourcey
|
2
|
+
class Filter
|
3
|
+
class_attribute :allowed_params
|
4
|
+
class_attribute :filters
|
5
|
+
|
6
|
+
def initialize(params)
|
7
|
+
@permitted_params = params.permit(self.allowed_params)
|
8
|
+
end
|
9
|
+
|
10
|
+
def apply(scope)
|
11
|
+
@permitted_params.each do |filter_name, permitted_param|
|
12
|
+
filter = self.filters.find { |filter| filter.name == filter_name }
|
13
|
+
scope = filter.apply(permitted_param, scope)
|
14
|
+
end
|
15
|
+
|
16
|
+
scope
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def filter(filter_name, opts = {}, &block)
|
21
|
+
self.allowed_params ||= []
|
22
|
+
self.allowed_params << filter_name
|
23
|
+
|
24
|
+
self.filters ||= []
|
25
|
+
self.filters << build_filter(filter_name, opts, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def build_filter(filter_name, opts, &block)
|
29
|
+
FilterProcessor.new(filter_name, opts, &block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Resourcey
|
2
|
+
class FilterProcessor
|
3
|
+
ALLOWED_OPTIONS = %i[multivalue].freeze
|
4
|
+
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
def initialize(name, opts = {}, &block)
|
8
|
+
@name = name.to_s
|
9
|
+
@opts = opts
|
10
|
+
@block = block
|
11
|
+
|
12
|
+
validate_options!
|
13
|
+
end
|
14
|
+
|
15
|
+
def apply(permitted_param, scope)
|
16
|
+
filter_value = parse_filter_value(permitted_param)
|
17
|
+
block.call(filter_value, scope)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :opts, :block
|
23
|
+
|
24
|
+
def parse_filter_value(permitted_param)
|
25
|
+
return permitted_param unless opts[:multivalue]
|
26
|
+
permitted_param.split(',')
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_options!
|
30
|
+
invalid_option = opts.keys.find { |option| ALLOWED_OPTIONS.exclude? option }
|
31
|
+
return true if invalid_option.nil?
|
32
|
+
raise Errors::OptionNotAllowed.new(invalid_option)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/resourcey.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resourcey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- polakowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -90,9 +90,13 @@ files:
|
|
90
90
|
- lib/resourcey.rb
|
91
91
|
- lib/resourcey/config.rb
|
92
92
|
- lib/resourcey/controller.rb
|
93
|
+
- lib/resourcey/controller_filtering.rb
|
93
94
|
- lib/resourcey/controller_model.rb
|
95
|
+
- lib/resourcey/controller_modules.rb
|
94
96
|
- lib/resourcey/controller_pagination.rb
|
95
97
|
- lib/resourcey/errors.rb
|
98
|
+
- lib/resourcey/filter.rb
|
99
|
+
- lib/resourcey/filter_processor.rb
|
96
100
|
- lib/resourcey/paginator.rb
|
97
101
|
- lib/resourcey/serializer.rb
|
98
102
|
homepage: https://github.com/polakowski/resourcey
|