lib/jsonapi/association.rb in jsonapi-resources-0.0.7 vs lib/jsonapi/association.rb in jsonapi-resources-0.0.8
- old
+ new
@@ -1,31 +1,42 @@
module JSONAPI
class Association
- attr_reader :primary_key, :acts_as_set, :type, :key, :options, :name, :class_name
+ attr_reader :acts_as_set, :foreign_key, :type, :options, :name, :class_name
def initialize(name, options={})
- @name = name.to_s
- @options = options
- @key = options[:key] ? options[:key].to_sym : nil
- @primary_key = options.fetch(:primary_key, 'id').to_sym
- @acts_as_set = options.fetch(:acts_as_set, false) == true
+ @name = name.to_s
+ @options = options
+ @acts_as_set = options.fetch(:acts_as_set, false) == true
+ @key = options[:key] ? options[:key].to_sym : nil
+
+ if @key.nil?
+ @foreign_key = options[:foreign_key ] ? options[:foreign_key ].to_sym : nil
+ else
+ # :nocov:
+ warn '[DEPRECATION] `key` is deprecated in associations. Please use `foreign_key` instead.'
+ # :nocov:
+ end
end
+ def primary_key
+ @primary_key ||= Resource.resource_for(@name)._primary_key
+ end
+
class HasOne < Association
def initialize(name, options={})
super
@class_name = options.fetch(:class_name, name.to_s.capitalize)
@type = class_name.underscore.pluralize.to_sym
- @key ||= "#{name}_id".to_sym
+ @foreign_key ||= @key.nil? ? "#{name}_id".to_sym : @key
end
end
class HasMany < Association
def initialize(name, options={})
super
@class_name = options.fetch(:class_name, name.to_s.capitalize.singularize)
@type = class_name.underscore.pluralize.to_sym
- @key ||= "#{name.to_s.singularize}_ids".to_sym
+ @foreign_key ||= @key.nil? ? "#{name.to_s.singularize}_ids".to_sym : @key
end
end
end
end