apicasso 0.2.8 → 0.2.9

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: f4a2ce3d41ad82b4b1ac28fc6dfa0b6f8a756b55
4
- data.tar.gz: 8d1d9d6f5aa7abae2406020e6445d1e3b1bb3932
3
+ metadata.gz: 922eed4373921e41ed9970b3bc152e219d2e5c17
4
+ data.tar.gz: 187957d76051bf9d5e6fa0d5a888ee650d30f2d6
5
5
  SHA512:
6
- metadata.gz: 02332ef2f6cb186bfb2f94f88ad44d60341fb6fff892ffac77293474bd3089ba297b48b6934f6d89d1fd3d773ccc4f5b8f7754e597a58b83fee4fb06728e5ff3
7
- data.tar.gz: 9a45a1ac48bb74da7bc40aa003a9faf0d0da486f89b9c9f66ca47732d25a80bfb0f6d74347e5eab1d25983c50f91eaa5330bcb9716c6dfb5a330ef4dced02444
6
+ metadata.gz: acc0fc2db3b22cc91ddbe1bf39f44ade021dc62eeed818050dd744d942ff38f8cff17e6b0c32c8d8937798b62e9ca47906b0a5b35f4f81bb18927c9e2ef1b4da
7
+ data.tar.gz: 7a0dae9df3e370456278d3b04845253cfa5bd598c18a4ae9abc84c0ad619b15f21cd499245fd41da4e1bcaf3d43efbb0223a7d542d907dccc0721d9b79f9d6e1
data/README.md CHANGED
@@ -14,7 +14,7 @@ gem 'apicasso'
14
14
 
15
15
  And then execute this to generate the required migrations:
16
16
  ```bash
17
- $ rails g apicasso:install
17
+ $ bundle install && rails g apicasso:install
18
18
  ```
19
19
  You will need to use a database with JSON fields support to use this gem.
20
20
 
@@ -47,9 +47,26 @@ Your API will reflect very similarly a `resources :resource` statement with the
47
47
 
48
48
  This means all your application's models will be exposed as `:resource` and it's relations will be exposed as `:nested`. It will enable you to CRUD and get schema metadata from your records.
49
49
 
50
+ ## Extending base API actions
51
+
52
+ When your application needs some kind of custom interaction that is not covered by APIcasso's CRUD approach you can make your own actions using our base classes and objects to go straight into your logic. If you have built the APIcasso's engine into a route it is important that your custom action takes precedence over the gem's ones. To do that you need to declare your custom route before the engine on you `config/routes.rb`
53
+ ```ruby
54
+ match '/:resource/:id/a-custom-action' => 'custom#not_a_crud', via: :get
55
+ mount Apicasso::Engine, at: "/api/v1"
56
+ ```
57
+ And in your `app/controllers/custom_controller.rb` you would have something like:
58
+ ```ruby
59
+ class CustomController < Apicasso::CrudController
60
+ def not_a_crud
61
+ render json: @object.some_operation
62
+ end
63
+ end
64
+ ```
65
+ This way you enjoy all our object finder, authorization and authentication features, making your job more straight into your business logic.
66
+
50
67
  ## Authorization/Authentication
51
68
 
52
- > But exposing my models to de internet is permissive as hell! Haven't you thought about security?
69
+ > But exposing my models to the internet is permissive as hell! Haven't you thought about security?
53
70
 
54
71
  *Sure!* The **APIcasso** suite is exposing your application using authentication through `Authorization: Token` [HTTP header authentication](http://tools.ietf.org/html/draft-hammer-http-token-auth-01). The API key objects are manageable through the `Apicasso::Key` model, which gets setup at install. When a new key is created a `.token` is generated using an [Universally Unique Identifier(RFC 4122)](https://tools.ietf.org/html/rfc4122).
55
72
 
@@ -70,9 +87,9 @@ A scope configured like this translates directly into which kind of access each
70
87
 
71
88
  You can have two kind of access control:
72
89
  * `true` - This will mean the key will have the declared clearance on **ALL** of this model's records
73
- * `Hash` - This will build a condition to what records this key have. A scope as `{ read: [{ account: { manager_id: 1 } }] }` will have read access into accounts with `manager_id == 1`
90
+ * `Hash` - This will build a condition to what records this key have access to. A scope as `{ read: [{ account: { manager_id: 1 } }] }` will have read access into accounts with `manager_id == 1`
74
91
 
75
- This saves you the trouble of having to setup every controller for each model. And even if your application really needs it, just make your controllers inherit from `Apicasso::CrudController` will extend it's functionalities, enabling the use of `@object` and `@resource` variables to access what is being resquested.
92
+ This saves you the trouble of having to setup every controller for each model. And even if your application really needs it, just make your controllers inherit from `Apicasso::CrudController` extending it and enabling the use of `@object` and `@resource` variables to access what is being resquested.
76
93
 
77
94
  ## Features on index actions
78
95
 
@@ -143,7 +160,7 @@ Each of those attributes on the `?group` parameter represent an option of the qu
143
160
  # Contributing
144
161
  Bug reports and pull requests are welcome on GitHub at https://github.com/ErvalhouS/APIcasso. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant code of conduct](http://contributor-covenant.org/). To find good places to start contributing, try looking into our issue list and our Codeclimate profile, or if you want to participate actively on what the core team is working on checkout our todo list:
145
162
 
146
- ## TODO
163
+ ### TODO
147
164
 
148
165
  - Abstract a configurable CORS approach, maybe using middleware.
149
166
  - Add gem options like: Token rotation, Alternative authentication methods
@@ -116,8 +116,10 @@ module Apicasso
116
116
  def set_records
117
117
  authorize! :read, resource.name.underscore.to_sym
118
118
  @records = resource.ransack(parsed_query).result
119
+ key_scope_records
119
120
  reorder_records if params[:sort].present?
120
121
  select_fields if params[:select].present?
122
+ include_relations if params[:include].present?
121
123
  end
122
124
 
123
125
  # Selects a fieldset that should be returned, instead of all fields
@@ -133,14 +135,14 @@ module Apicasso
133
135
 
134
136
  # Raw paginated records object
135
137
  def paginated_records
136
- accessible_records
138
+ @records
137
139
  .paginate(page: params[:page], per_page: params[:per_page])
138
140
  end
139
141
 
140
142
  # Records that can be accessed from current Apicasso::Key scope
141
143
  # permissions
142
- def accessible_records
143
- @records.accessible_by(current_ability).unscope(:order)
144
+ def key_scope_records
145
+ @records = @records.accessible_by(current_ability).unscope(:order)
144
146
  end
145
147
 
146
148
  # The response for index action, which can be a pagination of a record collection
@@ -155,17 +157,25 @@ module Apicasso
155
157
 
156
158
  # Parsing of `paginated_records` with pagination variables metadata
157
159
  def built_paginated
158
- { entries: entries_json }.merge(pagination_metadata_for(paginated_records))
160
+ { entries: @records }.merge(pagination_metadata_for(paginated_records))
159
161
  end
160
162
 
161
163
  # All records matching current query and it's total
162
164
  def built_unpaginated
163
- { entries: accessible_records, total: accessible_records.size }
165
+ { entries: @records, total: @records.size }
164
166
  end
165
167
 
166
- # Parsed JSON to be used as response payload
167
- def entries_json
168
- JSON.parse(paginated_records.to_json(include: parsed_include))
168
+ # Parsed JSON to be used as response payload, with included relations
169
+ def include_relations
170
+ @records = JSON.parse(included_collection.to_json(include: parsed_include))
171
+ end
172
+
173
+ def included_collection
174
+ if @records.try(:includes, parsed_include).present?
175
+ @records.includes(parsed_include)
176
+ else
177
+ @records
178
+ end
169
179
  end
170
180
 
171
181
  # Returns the collection checking if it needs pagination
@@ -1,3 +1,3 @@
1
1
  module Apicasso
2
- VERSION = '0.2.8'
2
+ VERSION = '0.2.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apicasso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Bellincanta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-29 00:00:00.000000000 Z
11
+ date: 2018-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan