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
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.
135
136
137
138
139
140
141
142
143
|
# File 'lib/jsonapi/relationship.rb', line 135
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.
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 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?
foreign_key_on == :self
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
"#{parent_resource}.#{name}(#{belongs_to? ? 'BelongsToOne' : 'ToOne'})"
end
|