Sha256: 71b06d3700c1aa37aa2e52783d1bf62c5a2e8f67a8b8194493686334730f26b8
Contents?: true
Size: 1.89 KB
Versions: 2
Compression:
Stored size: 1.89 KB
Contents
module JSONAPI class Association attr_reader :acts_as_set, :foreign_key, :type, :options, :name, :class_name, :polymorphic def initialize(name, options = {}) @name = name.to_s @options = options @acts_as_set = options.fetch(:acts_as_set, false) == true @foreign_key = options[:foreign_key] ? options[:foreign_key].to_sym : nil @module_path = options[:module_path] || '' @relation_name = options.fetch(:relation_name, @name) @polymorphic = options.fetch(:polymorphic, false) == true end alias_method :polymorphic?, :polymorphic def primary_key @primary_key ||= resource_klass._primary_key end def resource_klass @resource_klass ||= Resource.resource_for(@module_path + @class_name) end def relation_name(options = {}) case @relation_name when Symbol # :nocov: @relation_name # :nocov: when String @relation_name.to_sym when Proc @relation_name.call(options) end end def type_for_source(source) if polymorphic? resource = source.public_send(name) resource.class._type if resource else type end end class HasOne < Association def initialize(name, options = {}) super @class_name = options.fetch(:class_name, name.to_s.camelize) @type = class_name.underscore.pluralize.to_sym @foreign_key ||= "#{name}_id".to_sym end def polymorphic_type "#{type.to_s.singularize}_type" if polymorphic? end end class HasMany < Association def initialize(name, options = {}) super @class_name = options.fetch(:class_name, name.to_s.camelize.singularize) @type = class_name.underscore.pluralize.to_sym @foreign_key ||= "#{name.to_s.singularize}_ids".to_sym end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
jsonapi-resources-0.4.4 | lib/jsonapi/association.rb |
jsonapi-resources-0.4.3 | lib/jsonapi/association.rb |