lib/jsonapi/basic_resource.rb in jsonapi-resources-0.10.0.beta7 vs lib/jsonapi/basic_resource.rb in jsonapi-resources-0.10.0.beta8
- old
+ new
@@ -451,10 +451,13 @@
check_reserved_resource_name(subclass._type, subclass.name)
subclass._routed = false
subclass._warned_missing_route = false
+
+ subclass._clear_cached_attribute_options
+ subclass._clear_fields_cache
end
def rebuild_relationships(relationships)
original_relationships = relationships.deep_dup
@@ -525,10 +528,13 @@
attribute(attr, options)
end
end
def attribute(attribute_name, options = {})
+ _clear_cached_attribute_options
+ _clear_fields_cache
+
attr = attribute_name.to_sym
check_reserved_attribute_name(attr)
if (attr == :id) && (options[:format].nil?)
@@ -691,11 +697,11 @@
def sortable_field?(key, context = nil)
sortable_fields(context).include? key.to_sym
end
def fields
- _relationships.keys | _attributes.keys
+ @_fields_cache ||= _relationships.keys | _attributes.keys
end
def resources_for(records, context)
records.collect do |record|
resource_for(record, context)
@@ -824,11 +830,11 @@
[filter, raw]
end
# quasi private class methods
def _attribute_options(attr)
- default_attribute_options.merge(@_attributes[attr])
+ @_cached_attribute_options[attr] ||= default_attribute_options.merge(@_attributes[attr])
end
def _attribute_delegated_name(attr)
@_attributes.fetch(attr.to_sym, {}).fetch(:delegate, attr)
end
@@ -1061,10 +1067,12 @@
order_hash[field] = sort[:direction]
end
end
def _add_relationship(klass, *attrs)
+ _clear_fields_cache
+
options = attrs.extract_options!
options[:parent_resource] = self
attrs.each do |name|
relationship_name = name.to_sym
@@ -1103,9 +1111,17 @@
define_method(method_name, block)
end
def register_relationship(name, relationship_object)
@_relationships[name] = relationship_object
+ end
+
+ def _clear_cached_attribute_options
+ @_cached_attribute_options = {}
+ end
+
+ def _clear_fields_cache
+ @_fields_cache = nil
end
private
def check_reserved_resource_name(type, name)