lib/jsonapionify/api/resource/definitions/relationships.rb in jsonapionify-0.9.0 vs lib/jsonapionify/api/resource/definitions/relationships.rb in jsonapionify-0.9.1

- old
+ new

@@ -6,22 +6,58 @@ extend JSONAPIonify::InheritedAttributes inherited_array_attribute :relationship_definitions end end - def relates_to_many(name, resource: nil, &block) - define_relationship(name, Relationship::Many, resource: resource, &block) + def relates_to_many(name, count_attribute: false, include_count: true, **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, + include: include_count + ) if count_attribute + end end - def relates_to_one(name, resource: nil, &block) - define_relationship(name, Relationship::One, resource: resource, &block) + def relates_to_one(name, **opts, &block) + opts[:resource] ||= name.to_s.pluralize.to_sym + define_relationship(name, Relationship::One, **opts, &block) end - def define_relationship(name, klass, resource: nil, &block) + def define_relationship_counter(rel_name, name, include: true) + before :response do |context| + if (context.scope.is_a?(ActiveRecord::Relation) || context.scope.is_a?(ActiveRecord::Base)) && context.scope._reflect_on_association(rel_name) + context.scope = context.scope.includes(rel_name) + end if context.fields[type&.to_sym].include? name.to_sym + end if include + attribute name.to_sym, types.Integer, "The number of #{rel_name}.", write: false do |_, instance, context| + rel = context.resource.class.relationship(rel_name) + blank_fields = context.fields.map { |k, _| [k, {}] }.to_h + rel_context = rel.new( + request: context.request, + context_overrides: { + owner: instance, + fields: blank_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 + + 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, resource: resource, &block).tap do |new_relationship| + klass.new(self, name, **opts, &block).tap do |new_relationship| relationship_definitions.delete new_relationship relationship_definitions << new_relationship end end @@ -32,10 +68,10 @@ 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::RelationshipNotDefined, "Relationship not defined: #{name}" unless relationship_definition + raise Errors::RelationshipNotFound, "Relationship not found: #{name}" unless relationship_definition const_set const_name, relationship_definition.resource_class end end end