jsonapi-resources 0.0.5 → 0.0.6
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 +4 -4
- data/README.md +10 -10
- data/lib/jsonapi/resource.rb +5 -6
- data/lib/jsonapi/resource_serializer.rb +3 -3
- data/lib/jsonapi/resources/version.rb +1 -1
- data/test/fixtures/active_record.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54ace53b12009a3a05a2ecb95ad41de0521e11e1
|
4
|
+
data.tar.gz: 276a1910eebc42ffea72005edc6b837fb15a29ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2203b61b306a9c646482a156cdc3a073352ce91c683c837821342cf5c84f4910ce7e86e02938070176457362c63b275b27d70c5f08425e4244d462df21d49263
|
7
|
+
data.tar.gz: 27370f259cfc82e4fb3da10b5ea78e04462b563ac870e7ad0c46bdd7bca3fc5240989179cf02b7ef7464ca3787dce1edacb76512b6ff8bf59a3bbe343909ad7d
|
data/README.md
CHANGED
@@ -82,7 +82,7 @@ end
|
|
82
82
|
|
83
83
|
##### Fetchable Attributes
|
84
84
|
|
85
|
-
By default all attributes are assumed to be fetchable. The list of fetchable attributes can be filtered by overriding the `
|
85
|
+
By default all attributes are assumed to be fetchable. The list of fetchable attributes can be filtered by overriding the `fetchable_fields` method.
|
86
86
|
|
87
87
|
Here's an example that prevents guest users from seeing the `email` field:
|
88
88
|
|
@@ -92,11 +92,11 @@ class AuthorResource < JSONAPI::Resource
|
|
92
92
|
model_name 'Person'
|
93
93
|
has_many :posts
|
94
94
|
|
95
|
-
def
|
95
|
+
def fetchable_fields(context)
|
96
96
|
if (context.current_user.guest)
|
97
|
-
super(
|
97
|
+
super(context) - [:email]
|
98
98
|
else
|
99
|
-
super(
|
99
|
+
super(context)
|
100
100
|
end
|
101
101
|
end
|
102
102
|
end
|
@@ -106,7 +106,7 @@ Context flows through from the controller and can be used to control the attribu
|
|
106
106
|
|
107
107
|
##### Creatable and Updateable Attributes
|
108
108
|
|
109
|
-
By default all attributes are assumed to be
|
109
|
+
By default all attributes are assumed to be updateable and creatable. To prevent some attributes from being accepted by the `update` or `create` methods, override the `self.updateable_fields` and `self.createable_fields` methods on a resource.
|
110
110
|
|
111
111
|
This example prevents `full_name` from being set:
|
112
112
|
|
@@ -120,17 +120,17 @@ class ContactResource < JSONAPI::Resource
|
|
120
120
|
"#{@object.name_first}, #{@object.name_last}"
|
121
121
|
end
|
122
122
|
|
123
|
-
def self.
|
124
|
-
super
|
123
|
+
def self.updateable_fields(context)
|
124
|
+
super - [:full_name]
|
125
125
|
end
|
126
126
|
|
127
|
-
def self.
|
128
|
-
super
|
127
|
+
def self.createable_fields(keys, context)
|
128
|
+
super - [:full_name]
|
129
129
|
end
|
130
130
|
end
|
131
131
|
```
|
132
132
|
|
133
|
-
The `context` is not used by the `ResourceController`, but may be used if you override the controller methods.
|
133
|
+
The `context` is not by default used by the `ResourceController`, but may be used if you override the controller methods. By using the context you have the option to determine the createable and updateable fields based on the user.
|
134
134
|
|
135
135
|
##### Attribute Formatting
|
136
136
|
|
data/lib/jsonapi/resource.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'jsonapi/configuration'
|
2
2
|
require 'jsonapi/resource_for'
|
3
3
|
require 'jsonapi/association'
|
4
|
-
require 'action_dispatch/routing/mapper'
|
5
4
|
|
6
5
|
module JSONAPI
|
7
6
|
class Resource
|
@@ -108,8 +107,8 @@ module JSONAPI
|
|
108
107
|
end
|
109
108
|
|
110
109
|
# Override this on a resource instance to override the fetchable keys
|
111
|
-
def
|
112
|
-
|
110
|
+
def fetchable_fields(context)
|
111
|
+
self.class.fields
|
113
112
|
end
|
114
113
|
|
115
114
|
class << self
|
@@ -184,16 +183,16 @@ module JSONAPI
|
|
184
183
|
|
185
184
|
# Override in your resource to filter the updateable keys
|
186
185
|
def updateable_fields(context)
|
187
|
-
|
186
|
+
_updateable_associations | _attributes.keys
|
188
187
|
end
|
189
188
|
|
190
189
|
# Override in your resource to filter the createable keys
|
191
190
|
def createable_fields(context)
|
192
|
-
|
191
|
+
_updateable_associations | _attributes.keys
|
193
192
|
end
|
194
193
|
|
195
194
|
def fields
|
196
|
-
|
195
|
+
_associations.keys | _attributes.keys
|
197
196
|
end
|
198
197
|
|
199
198
|
# Override this method if you have more complex requirements than this basic find method provides
|
@@ -124,12 +124,12 @@ module JSONAPI
|
|
124
124
|
|
125
125
|
def attribute_hash(source)
|
126
126
|
requested = requested_fields(source.class._type)
|
127
|
-
fields = source.class._attributes.keys.to_a
|
127
|
+
fields = source.fetchable_fields(@context) & source.class._attributes.keys.to_a
|
128
128
|
unless requested.nil?
|
129
129
|
fields = requested & fields
|
130
130
|
end
|
131
131
|
|
132
|
-
|
132
|
+
fields.each_with_object({}) do |name, hash|
|
133
133
|
hash[format_key(name)] = format_value(source.send(name),
|
134
134
|
source.class._attribute_options(name)[:format],
|
135
135
|
source,
|
@@ -149,7 +149,7 @@ module JSONAPI
|
|
149
149
|
|
150
150
|
field_set = Set.new(fields)
|
151
151
|
|
152
|
-
included_associations = source.
|
152
|
+
included_associations = source.fetchable_fields(@context) & associations.keys
|
153
153
|
associations.each_with_object({}) do |(name, association), hash|
|
154
154
|
if included_associations.include? name
|
155
155
|
key = association.key
|
@@ -319,11 +319,11 @@ class AuthorResource < JSONAPI::Resource
|
|
319
319
|
return resources
|
320
320
|
end
|
321
321
|
|
322
|
-
def
|
322
|
+
def fetchable_fields(context)
|
323
323
|
if (@object.id % 2) == 1
|
324
|
-
super(
|
324
|
+
super(context) - [:email]
|
325
325
|
else
|
326
|
-
super(
|
326
|
+
super(context)
|
327
327
|
end
|
328
328
|
end
|
329
329
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Gebhardt
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-09-
|
12
|
+
date: 2014-09-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|