Class: JSONAPI::Relationship::ToOne
- Inherits:
-
Relationship
- Object
- Relationship
- JSONAPI::Relationship::ToOne
- Defined in:
- lib/jsonapi/relationship.rb
Instance Attribute Summary collapse
-
#foreign_key_on ⇒ Object
readonly
Returns the value of attribute foreign_key_on.
Instance Method Summary collapse
- #allow_include?(context = nil) ⇒ Boolean
- #belongs_to? ⇒ Boolean
- #include_optional_linkage_data? ⇒ Boolean
-
#initialize(name, options = {}) ⇒ ToOne
constructor
A new instance of ToOne.
- #polymorphic_type ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ ToOne
Returns a new instance of ToOne.
135 136 137 138 139 140 141 142 143 |
# File 'lib/jsonapi/relationship.rb', line 135 def initialize(name, = {}) super @class_name = .fetch(:class_name, name.to_s.camelize) @foreign_key ||= "#{name}_id".to_sym @foreign_key_on = .fetch(:foreign_key_on, :self) if parent_resource @inverse_relationship = .fetch(:inverse_relationship, parent_resource._type) end end |
Instance Attribute Details
#foreign_key_on ⇒ Object (readonly)
Returns the value of attribute foreign_key_on.
133 134 135 |
# File 'lib/jsonapi/relationship.rb', line 133 def foreign_key_on @foreign_key_on end |
Instance Method Details
#allow_include?(context = nil) ⇒ Boolean
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/jsonapi/relationship.rb', line 165 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
151 152 153 154 155 |
# File 'lib/jsonapi/relationship.rb', line 151 def belongs_to? # :nocov: foreign_key_on == :self # :nocov: end |
#include_optional_linkage_data? ⇒ Boolean
161 162 163 |
# File 'lib/jsonapi/relationship.rb', line 161 def include_optional_linkage_data? @always_include_optional_linkage_data || JSONAPI::configuration.always_include_to_one_linkage_data end |
#polymorphic_type ⇒ Object
157 158 159 |
# File 'lib/jsonapi/relationship.rb', line 157 def polymorphic_type "#{name}_type" if polymorphic? end |
#to_s ⇒ Object
145 146 147 148 149 |
# File 'lib/jsonapi/relationship.rb', line 145 def to_s # :nocov: useful for debugging "#{parent_resource}.#{name}(#{belongs_to? ? 'BelongsToOne' : 'ToOne'})" # :nocov: end |