Class: JSONAPI::Relationship

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

Direct Known Subclasses

ToMany, ToOne

Defined Under Namespace

Classes: ToMany, ToOne

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Relationship.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jsonapi/relationship.rb', line 14

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
  @parent_resource = options[:parent_resource]
  @relation_name = options.fetch(:relation_name, @name)
  @polymorphic = options.fetch(:polymorphic, false) == true
  @polymorphic_types = options[:polymorphic_types]
  if options[:polymorphic_relations]
    JSONAPI::CompatibilityHelper.deprecation_warn('Use polymorphic_types instead of polymorphic_relations')
    @polymorphic_types ||= options[:polymorphic_relations]
  end

  @always_include_optional_linkage_data = options.fetch(:always_include_optional_linkage_data, false) == true
  @eager_load_on_include = options.fetch(:eager_load_on_include, true) == true
  @allow_include = options[:allow_include]
  @class_name = nil
  @inverse_relationship = nil

  @_routed = false
  @_warned_missing_route = false

  exclude_links(options.fetch(:exclude_links, JSONAPI.configuration.default_exclude_links))

  # Custom methods are reserved for future use
  @custom_methods = options.fetch(:custom_methods, {})
end

Instance Attribute Details

#_routedObject

Returns the value of attribute _routed.



12
13
14
# File 'lib/jsonapi/relationship.rb', line 12

def _routed
  @_routed
end

#_warned_missing_routeObject

Returns the value of attribute _warned_missing_route.



12
13
14
# File 'lib/jsonapi/relationship.rb', line 12

def _warned_missing_route
  @_warned_missing_route
end

#acts_as_setObject (readonly)

Returns the value of attribute acts_as_set.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def acts_as_set
  @acts_as_set
end

#allow_includeObject

Returns the value of attribute allow_include.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def allow_include
  @allow_include
end

#always_include_optional_linkage_dataObject (readonly)

Returns the value of attribute always_include_optional_linkage_data.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def always_include_optional_linkage_data
  @always_include_optional_linkage_data
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def class_name
  @class_name
end

#custom_methodsObject (readonly)

Returns the value of attribute custom_methods.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def custom_methods
  @custom_methods
end

#eager_load_on_includeObject (readonly)

Returns the value of attribute eager_load_on_include.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def eager_load_on_include
  @eager_load_on_include
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def foreign_key
  @foreign_key
end

#inverse_relationshipObject (readonly)

Returns the value of attribute inverse_relationship.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def inverse_relationship
  @inverse_relationship
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def options
  @options
end

#parent_resourceObject (readonly) Also known as: parent_resource_klass

Returns the value of attribute parent_resource.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def parent_resource
  @parent_resource
end

#polymorphicObject (readonly) Also known as: polymorphic?

Returns the value of attribute polymorphic.



5
6
7
# File 'lib/jsonapi/relationship.rb', line 5

def polymorphic
  @polymorphic
end

Class Method Details

.polymorphic_types(name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jsonapi/relationship.rb', line 62

def self.polymorphic_types(name)
  @poly_hash ||= {}.tap do |hash|
    ObjectSpace.each_object do |klass|
      next unless Module === klass
      if ActiveRecord::Base > klass
        klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
          (hash[reflection.options[:as]] ||= []) << klass.name.downcase
        end
      end
    end
  end
  @poly_hash[name.to_sym]
end

Instance Method Details



124
125
126
# File 'lib/jsonapi/relationship.rb', line 124

def _exclude_links
  @_exclude_links ||= []
end

#belongs_to?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/jsonapi/relationship.rb', line 101

def belongs_to?
  # :nocov:
  false
  # :nocov:
end

#exclude_link?(link) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/jsonapi/relationship.rb', line 128

def exclude_link?(link)
  _exclude_links.include?(link.to_sym)
end


111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jsonapi/relationship.rb', line 111

def exclude_links(exclude)
  case exclude
    when :default, "default"
      @_exclude_links = [:self, :related]
    when :none, "none"
      @_exclude_links = []
    when Array
      @_exclude_links = exclude.collect {|link| link.to_sym}
    else
      fail "Invalid exclude_links"
  end
end

#primary_keyObject



46
47
48
49
50
# File 'lib/jsonapi/relationship.rb', line 46

def primary_key
  # :nocov:
  @primary_key ||= resource_klass._primary_key
  # :nocov:
end

#readonly?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/jsonapi/relationship.rb', line 107

def readonly?
  @options[:readonly]
end

#relation_name(options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jsonapi/relationship.rb', line 88

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

#resource_klassObject



52
53
54
# File 'lib/jsonapi/relationship.rb', line 52

def resource_klass
  @resource_klass ||= @parent_resource.resource_klass_for(@class_name)
end

#resource_typesObject



76
77
78
79
80
81
82
# File 'lib/jsonapi/relationship.rb', line 76

def resource_types
  if polymorphic? && belongs_to?
    @polymorphic_types ||= self.class.polymorphic_types(@relation_name).collect {|t| t.pluralize}
  else
    [resource_klass._type.to_s.pluralize]
  end
end

#table_nameObject



56
57
58
59
60
# File 'lib/jsonapi/relationship.rb', line 56

def table_name
  # :nocov:
  @table_name ||= resource_klass._table_name
  # :nocov:
end

#typeObject



84
85
86
# File 'lib/jsonapi/relationship.rb', line 84

def type
  @type ||= resource_klass._type.to_sym
end