From 90ec8d5dd9c23733c25641c6f91ebb14a06c3c04 Mon Sep 17 00:00:00 2001 From: Declan McGrath Date: Tue, 23 Feb 2016 16:03:17 +0000 Subject: [PATCH 1/3] More user friendly docs for context. --- README.md | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index be54cde4a..5591bc79c 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ backed by ActiveRecord models or by custom objects. * [Usage] (#usage) * [Resources] (#resources) * [JSONAPI::Resource] (#jsonapiresource) + * [Context] (#context) * [Attributes] (#attributes) * [Primary Key] (#primary-key) * [Model Name] (#model-name) @@ -155,6 +156,28 @@ In the above example vehicles are immutable. A call to `/vehicles` or `/vehicles of either `car` or `boat`. But calls to PUT or POST a `car` must be made to `/cars`. The rails models backing the above code use Single Table Inheritance. +#### Context + +Sometimes you will want to access things such as the current logged in user (and other state only available within your controllers) from within your resource classes. To make this state available to a resource class you need to put it into the context hash - this can be done via a `context` method one of your controllers or across all controllers using ApplicationController. + +For example: + +```ruby +class ApplicationController < JSONAPI::ResourceController + def context + {current_user: current_user} + end +end + +# Specific resource controllers derive from ApplicationController +# and share its context +class PeopleController < ApplicationController + +end +``` + +You can put things that affect serialization and resource configuration into the context. + #### Attributes Any of a resource's attributes that are accessible must be explicitly declared. Single attributes can be declared using @@ -1135,26 +1158,6 @@ A jsonapi-controller generator is avaliable rails generate jsonapi:controller contact ``` -###### Context - -The context that's used for serialization and resource configuration is set by the controller's `context` method. - -For example: - -```ruby -class ApplicationController < JSONAPI::ResourceController - def context - {current_user: current_user} - end -end - -# Specific resource controllers derive from ApplicationController -# and share its context -class PeopleController < ApplicationController - -end -``` - ###### Serialization Options Additional options can be passed to the serializer using the `serialization_options` method. From ae8e2c22dfdd116db40791f4b08031c699e5e23c Mon Sep 17 00:00:00 2001 From: Declan McGrath Date: Wed, 9 Mar 2016 19:45:33 +0000 Subject: [PATCH 2/3] Fixed typo in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5591bc79c..0cc1d5a5f 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ code use Single Table Inheritance. #### Context -Sometimes you will want to access things such as the current logged in user (and other state only available within your controllers) from within your resource classes. To make this state available to a resource class you need to put it into the context hash - this can be done via a `context` method one of your controllers or across all controllers using ApplicationController. +Sometimes you will want to access things such as the current logged in user (and other state only available within your controllers) from within your resource classes. To make this state available to a resource class you need to put it into the context hash - this can be done via a `context` method in one of your controllers or across all controllers using ApplicationController. For example: From 8d32fcf74ee87975556cbcc74a77e4b149d153d9 Mon Sep 17 00:00:00 2001 From: Declan McGrath Date: Wed, 9 Mar 2016 19:47:48 +0000 Subject: [PATCH 3/3] Really updated readme this time :-) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0cc1d5a5f..ad98d0548 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ code use Single Table Inheritance. #### Context -Sometimes you will want to access things such as the current logged in user (and other state only available within your controllers) from within your resource classes. To make this state available to a resource class you need to put it into the context hash - this can be done via a `context` method in one of your controllers or across all controllers using ApplicationController. +Sometimes you will want to access things such as the current logged in user (and other state only available within your controllers) from within your resource classes. To make this state available to a resource class you need to put it into the context hash - this can be done via a `context` method on one of your controllers or across all controllers using ApplicationController. For example: