Class: JSONAPI::Relationship
- Inherits:
-
Object
- Object
- JSONAPI::Relationship
- Defined in:
- lib/jsonapi/relationship.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#_routed ⇒ Object
Returns the value of attribute _routed.
-
#_warned_missing_route ⇒ Object
Returns the value of attribute _warned_missing_route.
-
#acts_as_set ⇒ Object
readonly
Returns the value of attribute acts_as_set.
-
#allow_include ⇒ Object
Returns the value of attribute allow_include.
-
#always_include_optional_linkage_data ⇒ Object
readonly
Returns the value of attribute always_include_optional_linkage_data.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#custom_methods ⇒ Object
readonly
Returns the value of attribute custom_methods.
-
#eager_load_on_include ⇒ Object
readonly
Returns the value of attribute eager_load_on_include.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#inverse_relationship ⇒ Object
readonly
Returns the value of attribute inverse_relationship.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent_resource ⇒ Object
(also: #parent_resource_klass)
readonly
Returns the value of attribute parent_resource.
-
#polymorphic ⇒ Object
(also: #polymorphic?)
readonly
Returns the value of attribute polymorphic.
-
#use_related_resource_records_for_joins ⇒ Object
readonly
Returns the value of attribute use_related_resource_records_for_joins.
Class Method Summary collapse
Instance Method Summary collapse
- #_exclude_links ⇒ Object
- #belongs_to? ⇒ Boolean
- #exclude_link?(link) ⇒ Boolean
- #exclude_links(exclude) ⇒ Object
-
#initialize(name, options = {}) ⇒ Relationship
constructor
A new instance of Relationship.
- #primary_key ⇒ Object
- #readonly? ⇒ Boolean
- #relation_name(options) ⇒ Object
- #resource_klass ⇒ Object
- #resource_types ⇒ Object
- #table_name ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Relationship
Returns a new instance of Relationship.
12 13 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 42 43 44 45 46 47 48 |
# File 'lib/jsonapi/relationship.rb', line 12 def initialize(name, = {}) @name = name.to_s @options = @acts_as_set = .fetch(:acts_as_set, false) == true @foreign_key = [:foreign_key] ? [:foreign_key].to_sym : nil @parent_resource = [:parent_resource] @relation_name = .fetch(:relation_name, @name) @polymorphic = .fetch(:polymorphic, false) == true @polymorphic_types = [:polymorphic_types] if [:polymorphic_relations] ActiveSupport::Deprecation.warn('Use polymorphic_types instead of polymorphic_relations') @polymorphic_types ||= [:polymorphic_relations] end = if [:relation_name] false else JSONAPI.configuration. end @use_related_resource_records_for_joins = .fetch(:use_related_resource_records_for_joins, ) == true @always_include_optional_linkage_data = .fetch(:always_include_optional_linkage_data, false) == true @eager_load_on_include = .fetch(:eager_load_on_include, false) == true @allow_include = [:allow_include] @class_name = nil @inverse_relationship = nil @_routed = false @_warned_missing_route = false exclude_links(.fetch(:exclude_links, JSONAPI.configuration.default_exclude_links)) # Custom methods are reserved for future use @custom_methods = .fetch(:custom_methods, {}) end |
Instance Attribute Details
#_routed ⇒ Object
Returns the value of attribute _routed.
10 11 12 |
# File 'lib/jsonapi/relationship.rb', line 10 def _routed @_routed end |
#_warned_missing_route ⇒ Object
Returns the value of attribute _warned_missing_route.
10 11 12 |
# File 'lib/jsonapi/relationship.rb', line 10 def _warned_missing_route @_warned_missing_route end |
#acts_as_set ⇒ Object (readonly)
Returns the value of attribute acts_as_set.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def acts_as_set @acts_as_set end |
#allow_include ⇒ Object
Returns the value of attribute allow_include.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def allow_include @allow_include end |
#always_include_optional_linkage_data ⇒ Object (readonly)
Returns the value of attribute always_include_optional_linkage_data.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def always_include_optional_linkage_data @always_include_optional_linkage_data end |
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def class_name @class_name end |
#custom_methods ⇒ Object (readonly)
Returns the value of attribute custom_methods.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def custom_methods @custom_methods end |
#eager_load_on_include ⇒ Object (readonly)
Returns the value of attribute eager_load_on_include.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def eager_load_on_include @eager_load_on_include end |
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def foreign_key @foreign_key end |
#inverse_relationship ⇒ Object (readonly)
Returns the value of attribute inverse_relationship.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def inverse_relationship @inverse_relationship end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def @options end |
#parent_resource ⇒ Object (readonly) Also known as: parent_resource_klass
Returns the value of attribute parent_resource.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def parent_resource @parent_resource end |
#polymorphic ⇒ Object (readonly) Also known as: polymorphic?
Returns the value of attribute polymorphic.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def polymorphic @polymorphic end |
#use_related_resource_records_for_joins ⇒ Object (readonly)
Returns the value of attribute use_related_resource_records_for_joins.
3 4 5 |
# File 'lib/jsonapi/relationship.rb', line 3 def @use_related_resource_records_for_joins end |
Class Method Details
.polymorphic_types(name) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jsonapi/relationship.rb', line 69 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.[:as] }.each do |reflection| (hash[reflection.[:as]] ||= []) << klass.name.underscore end end end end @poly_hash[name.to_sym] end |
Instance Method Details
#_exclude_links ⇒ Object
131 132 133 |
# File 'lib/jsonapi/relationship.rb', line 131 def _exclude_links @_exclude_links ||= [] end |
#belongs_to? ⇒ Boolean
108 109 110 111 112 |
# File 'lib/jsonapi/relationship.rb', line 108 def belongs_to? # :nocov: false # :nocov: end |
#exclude_link?(link) ⇒ Boolean
135 136 137 |
# File 'lib/jsonapi/relationship.rb', line 135 def exclude_link?(link) _exclude_links.include?(link.to_sym) end |
#exclude_links(exclude) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/jsonapi/relationship.rb', line 118 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_key ⇒ Object
53 54 55 56 57 |
# File 'lib/jsonapi/relationship.rb', line 53 def primary_key # :nocov: @primary_key ||= resource_klass._primary_key # :nocov: end |
#readonly? ⇒ Boolean
114 115 116 |
# File 'lib/jsonapi/relationship.rb', line 114 def readonly? @options[:readonly] end |
#relation_name(options) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/jsonapi/relationship.rb', line 95 def relation_name() case @relation_name when Symbol # :nocov: @relation_name # :nocov: when String @relation_name.to_sym when Proc @relation_name.call() end end |
#resource_klass ⇒ Object
59 60 61 |
# File 'lib/jsonapi/relationship.rb', line 59 def resource_klass @resource_klass ||= @parent_resource.resource_klass_for(@class_name) end |
#resource_types ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/jsonapi/relationship.rb', line 83 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_name ⇒ Object
63 64 65 66 67 |
# File 'lib/jsonapi/relationship.rb', line 63 def table_name # :nocov: @table_name ||= resource_klass._table_name # :nocov: end |
#type ⇒ Object
91 92 93 |
# File 'lib/jsonapi/relationship.rb', line 91 def type @type ||= resource_klass._type.to_sym end |