Module: JSONAPIonify::Api::Resource::Definitions::Relationships
- Defined in:
- lib/jsonapionify/api/resource/definitions/relationships.rb
Class Method Summary collapse
Instance Method Summary collapse
- #define_relationship(name, klass, **opts, &block) ⇒ Object
- #define_relationship_counter(rel_name, name, hidden) ⇒ Object
- #relates_to_many(name, count_attribute: false, count_hidden: :list, **opts, &block) ⇒ Object
- #relates_to_one(name, **opts, &block) ⇒ Object
- #relationship(name) ⇒ Object
- #relationships ⇒ Object
Class Method Details
.extended(klass) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/jsonapionify/api/resource/definitions/relationships.rb', line 4 def self.extended(klass) klass.class_eval do extend JSONAPIonify::InheritedAttributes inherited_array_attribute :relationship_definitions end end |
Instance Method Details
#define_relationship(name, klass, **opts, &block) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/jsonapionify/api/resource/definitions/relationships.rb', line 45 def define_relationship(name, klass, **opts, &block) const_name = name.to_s.camelcase + 'Relationship' remove_const(const_name) if const_defined? const_name klass.new(self, name, **opts, &block).tap do |new_relationship| relationship_definitions.delete new_relationship relationship_definitions << new_relationship end end |
#define_relationship_counter(rel_name, name, hidden) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jsonapionify/api/resource/definitions/relationships.rb', line 26 def define_relationship_counter(rel_name, name, hidden) attribute name.to_sym, types.Integer, "The number of #{rel_name}.", hidden: hidden do |_, instance, context| rel = context.resource.class.relationship(rel_name) rel_context = rel.new( request: context.request, context_overrides: { owner: instance, fields: {}, params: {} } ).exec { |c| c } count = rel_context.collection.uniq.count case count when Hash count.values.reduce(:+) when Fixnum count else error :internal_server_error end end end |
#relates_to_many(name, count_attribute: false, count_hidden: :list, **opts, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/jsonapionify/api/resource/definitions/relationships.rb', line 11 def relates_to_many(name, count_attribute: false, count_hidden: :list, **opts, &block) define_relationship(name, Relationship::Many, **opts, &block).tap do define_relationship_counter( name, count_attribute === true ? "#{name.to_s.singularize}_count" : count_attribute.to_s, count_hidden ) if count_attribute end end |
#relates_to_one(name, **opts, &block) ⇒ Object
21 22 23 24 |
# File 'lib/jsonapionify/api/resource/definitions/relationships.rb', line 21 def relates_to_one(name, **opts, &block) opts[:resource] ||= name.to_s.pluralize.to_sym define_relationship(name, Relationship::One, **opts, &block) end |
#relationship(name) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/jsonapionify/api/resource/definitions/relationships.rb', line 58 def relationship(name) name = name.to_sym const_name = name.to_s.camelcase + 'Relationship' return const_get(const_name, false) if const_defined? const_name relationship_definition = relationship_definitions.find { |rel| rel.name == name } raise Errors::RelationshipNotFound, "Relationship not found: #{name}" unless relationship_definition const_set const_name, relationship_definition.resource_class end |
#relationships ⇒ Object
54 55 56 |
# File 'lib/jsonapionify/api/resource/definitions/relationships.rb', line 54 def relationships relationship_definitions end |