lib/jsonapi/resource.rb in jsonapi-resources-0.0.10 vs lib/jsonapi/resource.rb in jsonapi-resources-0.0.11
- old
+ new
@@ -125,10 +125,13 @@
base._associations = (_associations || {}).dup
base._allowed_filters = (_allowed_filters || Set.new).dup
type = base.name.demodulize.sub(/Resource$/, '').underscore
base._type = type.pluralize.to_sym
+
+ check_reserved_resource_name(base._type, base.name)
+
# If eager loading is on this is how all the resource types are setup
# If eager loading is off some resource types will be initialized in
# _resource_name_from_type
@@resource_types[base._type] ||= base.name.demodulize
end
@@ -157,10 +160,12 @@
attribute(attr)
end
end
def attribute(attr, options = {})
+ check_reserved_attribute_name(attr)
+
@_attributes[attr] = options
define_method attr do
@model.send(attr)
end unless method_defined?(attr)
@@ -300,10 +305,17 @@
# override to allow for key processing and checking
def verify_key(key, context = nil)
return key
end
+ # override to allow for key processing and checking
+ def verify_keys(keys, context = nil)
+ return keys.collect do |key|
+ verify_key(key, context)
+ end
+ end
+
# override to allow for custom filters
def verify_custom_filter(filter, value, context = nil)
return filter, value
end
@@ -386,13 +398,36 @@
_allowed_filters.include?(filter)
end
private
+ def check_reserved_resource_name(type, name)
+ if [:ids, :types, :hrefs, :links].include?(type)
+ warn "[NAME COLLISION] `#{name}` is a reserved resource name."
+ return
+ end
+ end
+
+ def check_reserved_attribute_name(name)
+ # Allow :id since it can be used to specify the format. Since it is a method on the base Resource
+ # an attribute method won't be created for it.
+ if [:type, :href, :links].include?(name.to_sym)
+ warn "[NAME COLLISION] `#{name}` is a reserved key in #{@@resource_types[_type]}."
+ end
+ end
+
+ def check_reserved_association_name(name)
+ if [:id, :ids, :type, :types, :href, :hrefs, :link, :links].include?(name.to_sym)
+ warn "[NAME COLLISION] `#{name}` is a reserved association name in #{@@resource_types[_type]}."
+ end
+ end
+
def _associate(klass, *attrs)
options = attrs.extract_options!
attrs.each do |attr|
+ check_reserved_association_name(attr)
+
@_associations[attr] = klass.new(attr, options)
foreign_key = @_associations[attr].foreign_key
define_method foreign_key do