Class: JSONAPI::Relationship::ToMany

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ToMany.



185
186
187
188
189
190
191
192
193
# File 'lib/jsonapi/relationship.rb', line 185

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

Instance Attribute Details

#reflect ⇒ Object (readonly)

Returns the value of attribute reflect.



183
184
185
# File 'lib/jsonapi/relationship.rb', line 183

def reflect
  @reflect
end

Instance Method Details

#allow_include?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/jsonapi/relationship.rb', line 207

def allow_include?(context = nil)
  strategy = if @allow_include.nil?
               JSONAPI.configuration.default_allow_include_to_many
             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

#include_optional_linkage_data? ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
204
205
# File 'lib/jsonapi/relationship.rb', line 201

def include_optional_linkage_data?
  # :nocov:
  @always_include_optional_linkage_data || JSONAPI::configuration.always_include_to_many_linkage_data
  # :nocov:
end

#to_s ⇒ Object



195
196
197
198
199
# File 'lib/jsonapi/relationship.rb', line 195

def to_s
  # :nocov: useful for debugging
  "#{parent_resource}.#{name}(ToMany)"
  # :nocov:
end