jsonapi-resources 0.0.5 → 0.0.6

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: 51cd49198136e8a49dd3bb8138721babc499c0cf
4
- data.tar.gz: 29ddb62840cb17c2e753cfb03ab5e5cfe9dcdbe0
3
+ metadata.gz: 54ace53b12009a3a05a2ecb95ad41de0521e11e1
4
+ data.tar.gz: 276a1910eebc42ffea72005edc6b837fb15a29ac
5
5
  SHA512:
6
- metadata.gz: 276688390c5ae3d33ab1c40ea8a181ed8024c8c99cca514de6a941f52a64ace4b39b81b4aaba7222730b5d0604ec9e52a6cb48e0a49d61933df148daf0ecba3f
7
- data.tar.gz: bd36f549771a05afb8efec6ef4337e3888a9b2d9ad153fd870cba852c06c353a32b42484e0eb9b84cbfbfa5c3a14dfcb3254b0d1c3c07099f0debe849524a583
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 `fetchable` method.
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 fetchable(keys, context)
95
+ def fetchable_fields(context)
96
96
  if (context.current_user.guest)
97
- super(keys - [:email])
97
+ super(context) - [:email]
98
98
  else
99
- super(keys)
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 updateble and creatable. To prevent some attributes from being accepted by the `update` or `create` methods, override the `self.updateable` and `self.creatable` methods on a resource.
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.updateable(keys, context = nil)
124
- super(keys - [:full_name])
123
+ def self.updateable_fields(context)
124
+ super - [:full_name]
125
125
  end
126
126
 
127
- def self.createable(keys, context = nil)
128
- super(keys - [:full_name])
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
 
@@ -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 fetchable(keys, context = nil)
112
- keys
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
- fields
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
- fields
191
+ _updateable_associations | _attributes.keys
193
192
  end
194
193
 
195
194
  def fields
196
- _updateable_associations | _attributes.keys
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
- source.fetchable(fields, @context).each_with_object({}) do |name, hash|
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.fetchable(associations.keys, @context)
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
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
@@ -319,11 +319,11 @@ class AuthorResource < JSONAPI::Resource
319
319
  return resources
320
320
  end
321
321
 
322
- def fetchable(keys, context = nil)
322
+ def fetchable_fields(context)
323
323
  if (@object.id % 2) == 1
324
- super(keys - [:email])
324
+ super(context) - [:email]
325
325
  else
326
- super(keys)
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.5
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-20 00:00:00.000000000 Z
12
+ date: 2014-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler