Class: JSONAPI::Relationship::ToOne

Inherits:
JSONAPI::Relationship show all
Defined in:
lib/jsonapi/relationship.rb

Instance Attribute Summary collapse

Attributes inherited from JSONAPI::Relationship

#_routed, #_warned_missing_route, #acts_as_set, #allow_include, #always_include_optional_linkage_data, #class_name, #custom_methods, #eager_load_on_include, #foreign_key, #inverse_relationship, #name, #options, #parent_resource, #polymorphic

Instance Method Summary collapse

Methods inherited from JSONAPI::Relationship

#_exclude_links, #exclude_link?, #exclude_links, polymorphic_types, #primary_key, #readonly?, #relation_name, #resource_klass, #resource_types, #table_name, #type

Constructor Details

#initialize(name, options = {}) ⇒ ToOne

Returns a new instance of ToOne.



133
134
135
136
137
138
139
140
141
# File 'lib/jsonapi/relationship.rb', line 133

def initialize(name, options = {})
  super
  @class_name = options.fetch(:class_name, name.to_s.camelize)
  @foreign_key ||= "#{name}_id".to_sym
  @foreign_key_on = options.fetch(:foreign_key_on, :self)
  if parent_resource
    @inverse_relationship = options.fetch(:inverse_relationship, parent_resource._type)
  end
end

Instance Attribute Details

#foreign_key_onObject (readonly)

Returns the value of attribute foreign_key_on.



131
132
133
# File 'lib/jsonapi/relationship.rb', line 131

def foreign_key_on
  @foreign_key_on
end

Instance Method Details

#allow_include?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jsonapi/relationship.rb', line 163

def allow_include?(context = nil)
  strategy = if @allow_include.nil?
               JSONAPI.configuration.default_allow_include_to_one
             else
               @allow_include
             end

  if !!strategy == strategy #check for boolean
    return strategy
  elsif strategy.is_a?(Symbol) || strategy.is_a?(String)
    parent_resource.send(strategy, context)
  else
    strategy.call(context)
  end
end

#belongs_to?Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/jsonapi/relationship.rb', line 149

def belongs_to?
  # :nocov:
  foreign_key_on == :self
  # :nocov:
end

#include_optional_linkage_data?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/jsonapi/relationship.rb', line 159

def include_optional_linkage_data?
  @always_include_optional_linkage_data || JSONAPI::configuration.always_include_to_one_linkage_data
end

#polymorphic_typeObject



155
156
157
# File 'lib/jsonapi/relationship.rb', line 155

def polymorphic_type
  "#{name}_type" if polymorphic?
end

#to_sObject



143
144
145
146
147
# File 'lib/jsonapi/relationship.rb', line 143

def to_s
  # :nocov: useful for debugging
  "#{parent_resource}.#{name}(#{belongs_to? ? 'BelongsToOne' : 'ToOne'})"
  # :nocov:
end