From 11f2eab457da7af997fc5f4ec4d1d37110f5afe0 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 2 Aug 2017 17:21:20 -0500 Subject: [PATCH] Add record links tests --- lib/ams/inflector.rb | 10 +++ lib/ams/serializer.rb | 2 +- test/unit/serializer/record_links_test.rb | 78 +++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 test/unit/serializer/record_links_test.rb diff --git a/lib/ams/inflector.rb b/lib/ams/inflector.rb index 533497a0..2c8fae73 100644 --- a/lib/ams/inflector.rb +++ b/lib/ams/inflector.rb @@ -37,6 +37,16 @@ module AMS def pluralize(word) word + "s".freeze end + + def singularize(word) + if word.end_with?("es") + word[0..-3] + elsif word.end_with?("s") + word[0..-2] + else + word + end + end end end end diff --git a/lib/ams/serializer.rb b/lib/ams/serializer.rb index ef13641d..029fd4cd 100644 --- a/lib/ams/serializer.rb +++ b/lib/ams/serializer.rb @@ -453,7 +453,7 @@ module AMS end def foreign_key - "#{object.class.table_name.singularize}_id" + "#{Inflector.singularize(object.class.table_name)}_id" end end end diff --git a/test/unit/serializer/record_links_test.rb b/test/unit/serializer/record_links_test.rb new file mode 100644 index 00000000..400800bd --- /dev/null +++ b/test/unit/serializer/record_links_test.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true +require "test_helper" + +module AMS + class Serializer + class RecordLinksTest < Test + class ActiveRecordSerializer < ::AMS::Serializer; end + class ParentRecordSerializer < ActiveRecordSerializer + id_field :id + type :profiles + attribute :name + attribute :description, key: :summary + relation :child_records, type: :comments, to: :many + end + class ParentRecord < ParentModel + def self.table_name + "parent_records" + end + end + class ChildRecord < ChildModel + end + + def setup + super + @relation = ChildRecord.new(id: 2, name: "comment") + @object = ParentRecord.new( + id: 1, + name: "name", + description: "description", + child_models: [@relation] + ) + @serializer_class = ParentRecordSerializer + link_builder = Object.new + def link_builder.url_for(controller:, action:, params: {}, **options) + query = params.map { |k, v| v.is_a?(Hash) ? v.map { |vk, vv| "#{k}[#{vk}]=#{vv}" }.join("&") : "#{k}=#{v}" }.join("&") + optional = + if options.key?(:id) + "/#{options[:id]}" + else + "" + end + query = "?#{query}" unless query.empty? + "https://example.com/#{controller}/#{action}#{optional}#{query}" + end + @serializer_instance = @serializer_class.new(@object, link_builder: link_builder) + end + + def test_record_instance_as_json + expected = { + id: "1", type: :profiles, + attributes: { name: "name", summary: "description" }, + relationships: + { child_records: { links: { related: "https://example.com/comments/index?filter[parent_record_id]=1" } } }, + links: { self: "https://example.com/profiles/show/1" } + } + assert_equal expected, @serializer_instance.as_json + end + + def test_record_instance_to_json + expected = { + id: "1", type: :profiles, + attributes: { name: "name", summary: "description" }, + relationships: + { child_records: { links: { related: "https://example.com/comments/index?filter[parent_record_id]=1" } } }, + links: { self: "https://example.com/profiles/show/1" } + }.to_json + assert_equal expected, @serializer_instance.to_json + end + + def test_record_instance_dump + expected = { + id: "1", type: :profiles + }.to_json + assert_equal expected, @serializer_instance.dump(id: "1", type: :profiles) + end + end + end +end