resourcey 0.2.2 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9aa954231608cbfdc8b87e05cb5f94e31d80514
4
- data.tar.gz: 381b79282c48ffc3bf6312178cebea47b0991a6b
3
+ metadata.gz: 5348dbbfc10aec535cb095b78c129e9c8156c307
4
+ data.tar.gz: e7bd3e77e11450f4ea48bf33df1159fccce6360c
5
5
  SHA512:
6
- metadata.gz: 67480d655294ea8c964c355a12f921d42c754d88701c4a63167db43165337191cc3f42b2cdf96725f9c034a632c7c4ea204a8817d842a70ba4a4c5f9d6cd7408
7
- data.tar.gz: e64c924852dfb40710766cea3959dc373abb3ca4bad35bd4e50a3fd99410ecdc40096b338189921471e30a63497c113621cd92a06f1d9567b265a08335d13cad
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
- ### Serializer
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
- ## Gem development
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
@@ -1,8 +1,8 @@
1
- require 'resourcey/controller_pagination'
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
@@ -0,0 +1,3 @@
1
+ require 'resourcey/controller_filtering'
2
+ require 'resourcey/controller_pagination'
3
+ require 'resourcey/controller_model'
@@ -11,10 +11,10 @@ module Resourcey
11
11
  private
12
12
 
13
13
  def paginated_resources
14
- return resources unless self.pagination_enabled
14
+ return filtered_resources unless self.pagination_enabled
15
15
 
16
16
  paginator = current_paginator_class.new(params)
17
- paginator.paginate(resources)
17
+ paginator.paginate(filtered_resources)
18
18
  end
19
19
 
20
20
  def current_paginator_class
@@ -1,9 +1,9 @@
1
1
  module Resourcey
2
2
  module Errors
3
3
  class BaseError < StandardError; end
4
- class BaseError < StandardError; end
5
4
 
6
5
  class NotImplemented < BaseError; end
7
6
  class ClassNotFound < BaseError; end
7
+ class OptionNotAllowed < BaseError; end
8
8
  end
9
9
  end
@@ -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
@@ -5,3 +5,5 @@ require 'resourcey/config'
5
5
  require 'resourcey/controller'
6
6
  require 'resourcey/serializer'
7
7
  require 'resourcey/paginator'
8
+ require 'resourcey/filter'
9
+ require 'resourcey/filter_processor'
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.2.2
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-23 00:00:00.000000000 Z
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