Class: JSONAPI::Relationship::ToOne
Instance Attribute Summary collapse
#_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
#_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_on ⇒ Object
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
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 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
158
159
160
161
162
|
# File 'lib/jsonapi/relationship.rb', line 158
def belongs_to?
foreign_key_on == :self
end
|
#include_optional_linkage_data? ⇒ 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_type ⇒ Object
164
165
166
|
# File 'lib/jsonapi/relationship.rb', line 164
def polymorphic_type
"#{name}_type" if polymorphic?
end
|
#to_s ⇒ Object
152
153
154
155
156
|
# File 'lib/jsonapi/relationship.rb', line 152
def to_s
"#{parent_resource}.#{name}(#{belongs_to? ? 'BelongsToOne' : 'ToOne'})"
end
|