rails-rapido 0.4.0 → 0.5.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
- SHA1:
3
- metadata.gz: 04bd1cc6e6fd9527fc1b90022653cf1f8d48c041
4
- data.tar.gz: bb1d6b86f00de708625b1b62f0aeb8363f4e6571
2
+ SHA256:
3
+ metadata.gz: eae5461189f3b0d670b870ac16c3f8e5b04ca3b57465e95151a360fd563cfa71
4
+ data.tar.gz: d89ae7dbcb57d036a65bf4f507f36b331e00ba77bb34e6305bd86f46e844e463
5
5
  SHA512:
6
- metadata.gz: 6ff34955402b0927030447e6cf25314cc0be498ebe8da896d0d490902753fb0d2efd4d9cbf3961f7b2651d0bcf12e3934b2eb27bb88b56159cc411c1245651ef
7
- data.tar.gz: 2966b6c780cd4b58e85719e338bd2ef7b4780199aaa523bc1d348c474efdd759e866877514f254cd6c2a8ac3a116a26291c1b93c5d57d082b91e7d46b7a764ce
6
+ metadata.gz: ac1880f71e3289703a168beb617dcacb8136f6aceafddda189e143bd35e059608da3d6c6d4b6d7143a2af8dea1993dfee283d54407b9e5ad214c6807f5e317ea
7
+ data.tar.gz: be1bea5642f1da1261b786abe9c668737ef96fac8525207322635c937a158deb32759a03f68b93e9907132fd77dfc5ceb8462b8656860467e2a444b2a7b0803e
data/README.md CHANGED
@@ -4,7 +4,79 @@
4
4
 
5
5
  Rapido is a simple, highly opinionated library that can be included into your Rails controllers to enforce standardized behavior and security.
6
6
 
7
- Documentation coming soon.
7
+ ## API
8
+
9
+ #### `belongs_to`
10
+
11
+ Specifies the owner of the resource. For example, if many `Post` belonged to `User`, then in the `PostsController`, the following would be included: `belongs_to :user` and `Post` would be retrieved like so:
12
+
13
+ `User.find(params[:user_id]).posts.find([params[:id])`.
14
+
15
+ ##### Optional Parameters
16
+
17
+ **has_one**
18
+
19
+ Default `false`. If `true`, will treat the `belongs_to` as `has_one`.
20
+
21
+ **owner**
22
+
23
+ This is the owner of the owner - if supplied, will be used to retrieve the owner. For example, if `User` belongs to `Group`, `Post` belongs to `User`, then in the `PostsController` if `belongs_to :user, owner: :current_group` was supplied, then the User would be retrieved from group before Post is retrieved from user, like so:
24
+
25
+ `current_group.users.find(params[:user_id]).posts.find(params[:id])`.
26
+
27
+ **getter**
28
+
29
+ Specifies the method to call to retrieve the owner, rather than retrieving by with URL parameters. For example, if `Post` belongs to `User`, then in the `PostsController` if `belongs_to :user, getter: :current_user` was supplied, then the post would be retrieved like so:
30
+
31
+ `current_user.posts.find(params[:id])`.
32
+
33
+ This can be useful when ownership exists but is not reflected in the route structure explicitly.
34
+
35
+ **foreign_key**
36
+
37
+ Default `id`. Specifies the name of the lookup column for the owner. For example, if `Post` belongs to `User`, then in the `PostsController` if `belongs_to :user, foreign_key: :token` is supplied, then the post would be retrieved like so:
38
+
39
+ `User.find_by(token: id).posts.find(params[:id])`.
40
+
41
+ **foreign_key_param**
42
+
43
+ Default `[singular owner name]_id`. Specifies the param used as the owner's foreign key. For example, if `Post` belongs to `User`, then in the `PostsController` if `belongs_to :user, foreign_key_param: :author_id` is supplied, then the post would be retrieved like so:
44
+
45
+ `User.find(params[:author_id]).posts.find(params[:id])`.
46
+
47
+ #### `lookup_param`
48
+
49
+ Specifies the param used to retrieve the resource. For example, if `Post` belongs to `User`, then in the `PostsController` if `lookup_param :token` is supplied, then the post would be retrieved like so:
50
+
51
+ `User.find_by(params[:id]).posts.find_by(token: params[:token])`.
52
+
53
+ #### `presented_by`
54
+
55
+ Specifies the class that will present the resource. This class must accept the resource as the only parameter at initialization, and respond to the `as_json` for output when used with the `Rapido::ApiController`.
56
+
57
+ #### `collection_presented_by`
58
+
59
+ Specifies the class that will present a collection of resources, as in the index method. Similar to `presented_by`, the class must accept the resource collection as the only argument at initialization, and respond to `as_json` for output when used with the `Rapido::ApiController`. The `collection_presented_by` can also accept a list of arguments, as symbols, that should be pulled from the `params` hash and passed to presenter class at initialization as optional argments.
60
+
61
+ For example, if `params` contained a `:filter` parameter which should be used by the presenter to filter the collection, then the following would work:
62
+
63
+ `collection_presented_by WidgetsCollectionPresenter, :filter`
64
+
65
+ The `initialize` method of the presenter should be structured as such:
66
+
67
+ `def initialize(widgets, filter = nil)`
68
+
69
+ #### `attr_permitted`
70
+
71
+ Accepts a list of symbols. These symbols specify the attributes that are supplied to StrongParameters as permitted parameters in `create` and `update` actions.
72
+
73
+ #### `permit_no_params!`
74
+
75
+ This will disallow any parameters in `create` and `update` actions.
76
+
77
+ #### `permit_all_params!`
78
+
79
+ This will permit all parameters in `create` and `update` actions.
8
80
 
9
81
  ## Development
10
82
 
@@ -14,9 +86,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
14
86
 
15
87
  ## Testing
16
88
 
17
- Run `rake` to execute the feature tests written for the sample application in the `test` directory.
89
+ Run `cucumber test/test_5_1` to execute all the tests in the test application.
18
90
 
19
91
  ## Contributing
20
92
 
21
- Bug reports and pull requests are welcome on GitHub at https://github.com/jskirst/rapido.
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/starfighter/rapido.
22
94
 
@@ -16,35 +16,49 @@ module Rapido
16
16
  end
17
17
 
18
18
  def index
19
- render json: resource_collection.map(&:to_h)
19
+ render json: present_resource_collection(resource_collection)
20
20
  end
21
21
 
22
22
  def show
23
- render json: resource.to_h
23
+ render json: present_resource(resource)
24
24
  end
25
25
 
26
26
  def create
27
27
  new_resource = build_resource(resource_params)
28
28
  if new_resource.save
29
- render json: new_resource.to_h, status: :created
29
+ render json: present_resource(new_resource), status: :created
30
30
  else
31
31
  render json: { errors: new_resource.errors.full_messages }, status: 422
32
32
  end
33
33
  end
34
34
 
35
35
  def destroy
36
- resource_hsh = resource.to_h
36
+ resource_before_destruction = present_resource(resource)
37
37
  resource.destroy
38
- render json: resource_hsh
38
+ render json: resource_before_destruction
39
39
  end
40
40
 
41
41
  def update
42
42
  resource.assign_attributes(resource_params)
43
43
  if resource.save
44
- render json: resource.to_h
44
+ render json: present_resource(resource)
45
45
  else
46
46
  render json: { errors: resource.errors.full_messages }, status: 422
47
47
  end
48
48
  end
49
+
50
+ private
51
+
52
+ def present_resource(resource)
53
+ return presenter.new(resource).as_json if presenter
54
+ resource.to_h
55
+ end
56
+
57
+ def present_resource_collection(resource_collection)
58
+ args = collection_presenter_args.nil? ? nil : collection_presenter_args.map { |arg| params[arg] }
59
+ return collection_presenter.new(*[resource_collection, *args].compact).as_json if collection_presenter
60
+ return resource_collection.map{ |r| presenter.new(r).as_json } if presenter
61
+ resource_collection.map(&:to_h)
62
+ end
49
63
  end
50
64
  end
@@ -44,6 +44,11 @@ module Rapido
44
44
  end
45
45
  end
46
46
 
47
+ def collection_presented_by(presenter_class, *args)
48
+ @collection_presenter ||= presenter_class
49
+ @collection_presenter_args = args if args.count > 0
50
+ end
51
+
47
52
  def owner_lookup_defaults
48
53
  owner_lookup_param(@owner_class, :id)
49
54
  owner_lookup_field(:id)
@@ -81,6 +86,10 @@ module Rapido
81
86
  def resource_class_name
82
87
  resource_class_from_controller
83
88
  end
89
+
90
+ def presented_by(presenter_class)
91
+ @presenter ||= presenter_class
92
+ end
84
93
  end
85
94
 
86
95
  private
@@ -95,6 +104,14 @@ module Rapido
95
104
  end
96
105
  end
97
106
 
107
+ def collection_presenter
108
+ setting(:collection_presenter)
109
+ end
110
+
111
+ def collection_presenter_args
112
+ setting(:collection_presenter_args)
113
+ end
114
+
98
115
  def owner_class
99
116
  return nil unless setting(:owner_class)
100
117
  @owner_class ||= begin
@@ -138,6 +155,10 @@ module Rapido
138
155
  end
139
156
  end
140
157
 
158
+ def presenter
159
+ setting(:presenter)
160
+ end
161
+
141
162
  def resource
142
163
  @resource ||= begin
143
164
  if setting(:has_one)
@@ -1,3 +1,3 @@
1
1
  module Rapido
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-rapido
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Kirst
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-05 00:00:00.000000000 Z
11
+ date: 2018-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kaminari
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.6.14
107
+ rubygems_version: 2.7.6
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Rails API Dryer-o