resourcey 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8717fa2e66160b64e006f2e8815042efb56863f2
4
- data.tar.gz: f2728bdf17ab672ca8a0722e80072d27b577daf5
3
+ metadata.gz: 576cd7bfe264dae2d656ed025680ac039d4bc3ad
4
+ data.tar.gz: 2d6f3380904ec055a351bdf6fce757df49e2e8bf
5
5
  SHA512:
6
- metadata.gz: 9bf8d7fbf5f97202170dc7bfb474152f77ebc9d54bf61b3299116419cd783fbe621ac2291d233c15887100669fa756a073f146559a7ecb578459011b00e87015
7
- data.tar.gz: 8f885c1ee23b45c714c9cdfd01f3675f83611ad551f733ba4b5410a7c5e1ba430e13d7deb427342e9dd2b8d4ba95cb6b7c5bbd66935349277e7a9ffc5227eab1
6
+ metadata.gz: 196654c51925608372e63e718cb5196c21290fa28cd78bf326ec2b4227b28397eb232bf113d1ee87b32762db4336c7e55607c8fc741476bb55b07ce235f8fa22
7
+ data.tar.gz: 6bd006a5e70cc6a98b0e682c80a4263d16a8a84d195cdc0467c5abab2bd7bb8f916d6c7f6ff6d9fa846d71c1892f57a8f50782c8c690e9ef621586a68fb2761d
data/README.md CHANGED
@@ -19,7 +19,7 @@ gem install resourcey
19
19
 
20
20
  ## Usage
21
21
  ### Controller
22
- For a resource called `user`, just create `UsersController` in a correct namespace (e.g. 'api/v1')
22
+ For a resource called `user`, just create `UsersController`:
23
23
  ```ruby
24
24
  class Api::V1::UsersController < Resourcey::Controller
25
25
  end
@@ -52,6 +52,35 @@ Now just visit `/api/v1/users`, and see how your resources are rendered.
52
52
  ]
53
53
  ```
54
54
 
55
+ ### Pagination
56
+ To paginate resources, simply invoke `paginate` method in controller, like this:
57
+ ```ruby
58
+ class ResourcesController < Resourcey::Controller
59
+ paginate
60
+ end
61
+ ```
62
+
63
+ This will use default `:paged` paginator. Now you need to pass `page` and `per_page` parameters in `pagination` parameter value, like this:
64
+
65
+ ```
66
+ http://api.example.com/resources?page=3&per_page=10
67
+ ```
68
+
69
+ This will fetch page 3, with 10 resources per single page. That's all! Pagination can be configured globally (see "Configuration" below). Also you can configure every controller's pagination.
70
+
71
+ For further reading, [click here](/docs/PAGINATION.md).
72
+
73
+ ## Configuration
74
+ Create configuration file in your `config/initializers` folder, and configure as usual:
75
+ ```ruby
76
+ Resourcey.configure do |config|
77
+ config.some_config_variable = :some_value
78
+ end
79
+ ```
80
+
81
+ ### Available config variables
82
+ - `default_paginator` - name of paginator that will be used in every controller, if not configured on controller-level (default: `:paged`)
83
+
55
84
  ## Gem development
56
85
  If you want to take part in developing resourcey, fork this repository, commit your code, and create pull request.
57
86
 
@@ -1,9 +1,9 @@
1
1
  module Resourcey
2
2
  class Config
3
- attr_accessor :foo
3
+ attr_accessor :default_paginator
4
4
 
5
5
  def initialize
6
- self.foo = 'foo'
6
+ self.default_paginator = :paged
7
7
  end
8
8
  end
9
9
 
@@ -1,7 +1,11 @@
1
+ require 'resourcey/controller_pagination'
2
+
1
3
  module Resourcey
2
4
  class Controller < ActionController::Base
5
+ include Resourcey::ControllerPagination
6
+
3
7
  def index
4
- render json: serialized_collection(resources)
8
+ render json: serialized_collection(paginated_resources)
5
9
  end
6
10
 
7
11
  def show
@@ -53,12 +57,12 @@ module Resourcey
53
57
 
54
58
  def resource_model
55
59
  name = resource_model_name
56
- name.safe_constantize || raise(Errors::ClassNotFound.new(name, :model))
60
+ name.safe_constantize || raise(Errors::ClassNotFound.new(name))
57
61
  end
58
62
 
59
63
  def serializer
60
64
  name = "#{resource_model_name}Serializer"
61
- name.safe_constantize || raise(Errors::ClassNotFound.new(name, :serializer))
65
+ name.safe_constantize || raise(Errors::ClassNotFound.new(name))
62
66
  end
63
67
 
64
68
  def resources
@@ -0,0 +1,46 @@
1
+ module Resourcey
2
+ module ControllerPagination
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :pagination_enabled
7
+ class_attribute :controller_paginator_name
8
+ class_attribute :controller_paginator_options
9
+ end
10
+
11
+ private
12
+
13
+ def paginated_resources
14
+ return resources unless self.pagination_enabled
15
+
16
+ paginator = current_paginator_class.new(params)
17
+ paginator.paginate(resources)
18
+ end
19
+
20
+ def current_paginator_class
21
+ Resourcey::Paginator.class_for(current_paginator_name)
22
+ end
23
+
24
+ def current_paginator_name
25
+ self.controller_paginator_name || Config.default_paginator
26
+ end
27
+
28
+ module ClassMethods
29
+ def paginate_with(name, opts = {})
30
+ enable_pagination!
31
+ self.controller_paginator_name = name
32
+ self.controller_paginator_options = opts
33
+ end
34
+
35
+ def paginate(opts = {})
36
+ paginate_with(Resourcey.config.default_paginator, opts)
37
+ end
38
+
39
+ private
40
+
41
+ def enable_pagination!
42
+ self.pagination_enabled = true
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,6 +1,7 @@
1
1
  module Resourcey
2
2
  module Errors
3
3
  class BaseError < StandardError; end
4
+ class BaseError < StandardError; end
4
5
 
5
6
  class NotImplemented < BaseError; end
6
7
  class ClassNotFound < BaseError; end
@@ -0,0 +1,70 @@
1
+ module Resourcey
2
+ class Paginator
3
+ class_attribute :allowed_params
4
+
5
+ def initialize(params)
6
+ parsed_params = parse_params(params)
7
+ setup(parsed_params)
8
+ end
9
+
10
+ def parse_params(params)
11
+ params.permit(self.allowed_params)
12
+ end
13
+
14
+ def paginate(*args)
15
+ raise Errors::NotImplemented.new(:paginate)
16
+ end
17
+
18
+ def setup(*args)
19
+ raise Errors::NotImplemented.new(:setup)
20
+ end
21
+
22
+ private
23
+
24
+ class << self
25
+ def class_for(name)
26
+ paginator_name = "#{name.to_s.camelize}Paginator"
27
+ paginator_name.safe_constantize || raise(Errors::ClassNotFound.new(paginator_name))
28
+ end
29
+
30
+ def permit_params(*args)
31
+ self.allowed_params = args
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ class PagedPaginator < Resourcey::Paginator
38
+ permit_params :page, :per_page
39
+
40
+ def setup(opts)
41
+ @page = opts[:page].to_i
42
+ @per_page = opts[:per_page].to_i
43
+ end
44
+
45
+ def paginate(scope)
46
+ offset = (page - 1) * per_page
47
+ scope.offset(offset).limit(per_page)
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :page, :per_page
53
+ end
54
+
55
+ class OffsetPaginator < Resourcey::Paginator
56
+ permit_params :offset, :limit
57
+
58
+ def setup(opts)
59
+ @offset = opts[:offset]
60
+ @limit = opts[:limit]
61
+ end
62
+
63
+ def paginate(scope)
64
+ scope.offset(offset).limit(limit)
65
+ end
66
+
67
+ private
68
+
69
+ attr_reader :offset, :limit
70
+ end
data/lib/resourcey.rb CHANGED
@@ -4,3 +4,4 @@ require 'resourcey/errors'
4
4
  require 'resourcey/config'
5
5
  require 'resourcey/controller'
6
6
  require 'resourcey/serializer'
7
+ require 'resourcey/paginator'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resourcey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - polakowski
@@ -90,7 +90,9 @@ files:
90
90
  - lib/resourcey.rb
91
91
  - lib/resourcey/config.rb
92
92
  - lib/resourcey/controller.rb
93
+ - lib/resourcey/controller_pagination.rb
93
94
  - lib/resourcey/errors.rb
95
+ - lib/resourcey/paginator.rb
94
96
  - lib/resourcey/serializer.rb
95
97
  homepage: https://github.com/polakowski/resourcey
96
98
  licenses: