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, #use_related_resource_records_for_joins

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.



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

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.



140
141
142
# File 'lib/jsonapi/relationship.rb', line 140

def foreign_key_on
  @foreign_key_on
end

Instance Method Details

#allow_include?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/jsonapi/relationship.rb', line 172

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)


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

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

#include_optional_linkage_data?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/jsonapi/relationship.rb', line 168

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

#polymorphic_typeObject



164
165
166
# File 'lib/jsonapi/relationship.rb', line 164

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

#to_sObject



152
153
154
155
156
# File 'lib/jsonapi/relationship.rb', line 152

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