|
| 1 | +RSpec.shared_context 'group class' do |
| 2 | + |
| 3 | + # Person, Group Classes and serializers |
| 4 | + before(:context) do |
| 5 | + # models |
| 6 | + class Person |
| 7 | + attr_accessor :id, :first_name, :last_name |
| 8 | + end |
| 9 | + |
| 10 | + class Group |
| 11 | + attr_accessor :id, :name, :groupees # Let's assume groupees can be Person or Group objects |
| 12 | + end |
| 13 | + |
| 14 | + # serializers |
| 15 | + class PersonSerializer |
| 16 | + include FastJsonapi::ObjectSerializer |
| 17 | + set_type :person |
| 18 | + attributes :first_name, :last_name |
| 19 | + end |
| 20 | + |
| 21 | + class GroupSerializer |
| 22 | + include FastJsonapi::ObjectSerializer |
| 23 | + set_type :group |
| 24 | + attributes :name |
| 25 | + has_many :groupees, polymorphic: true |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + |
| 30 | + # Namespaced PersonSerializer |
| 31 | + before(:context) do |
| 32 | + # namespaced model stub |
| 33 | + module AppName |
| 34 | + module V1 |
| 35 | + class PersonSerializer |
| 36 | + include FastJsonapi::ObjectSerializer |
| 37 | + # to test if compute_serializer_name works |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + # Movie and Actor struct |
| 44 | + before(:context) do |
| 45 | + PersonStruct = Struct.new( |
| 46 | + :id, :first_name, :last_name |
| 47 | + ) |
| 48 | + |
| 49 | + GroupStruct = Struct.new( |
| 50 | + :id, :name, :groupees, :groupee_ids |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + after(:context) do |
| 55 | + classes_to_remove = %i[ |
| 56 | + Person |
| 57 | + PersonSerializer |
| 58 | + Group |
| 59 | + GroupSerializer |
| 60 | + AppName::V1::PersonSerializer |
| 61 | + PersonStruct |
| 62 | + GroupStruct |
| 63 | + ] |
| 64 | + classes_to_remove.each do |klass_name| |
| 65 | + Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + let(:group_struct) do |
| 70 | + group = GroupStruct.new |
| 71 | + group[:id] = 1 |
| 72 | + group[:name] = 'Group 1' |
| 73 | + group[:groupees] = [] |
| 74 | + |
| 75 | + person = PersonStruct.new |
| 76 | + person[:id] = 1 |
| 77 | + person[:last_name] = "Last Name 1" |
| 78 | + person[:first_name] = "First Name 1" |
| 79 | + |
| 80 | + child_group = GroupStruct.new |
| 81 | + child_group[:id] = 2 |
| 82 | + child_group[:name] = 'Group 2' |
| 83 | + |
| 84 | + group.groupees = [person, child_group] |
| 85 | + group |
| 86 | + end |
| 87 | + |
| 88 | + let(:group) do |
| 89 | + group = Group.new |
| 90 | + group.id = 1 |
| 91 | + group.name = 'Group 1' |
| 92 | + |
| 93 | + person = Person.new |
| 94 | + person.id = 1 |
| 95 | + person.last_name = "Last Name 1" |
| 96 | + person.first_name = "First Name 1" |
| 97 | + |
| 98 | + child_group = Group.new |
| 99 | + child_group.id = 2 |
| 100 | + child_group.name = 'Group 2' |
| 101 | + |
| 102 | + group.groupees = [person, child_group] |
| 103 | + group |
| 104 | + end |
| 105 | + |
| 106 | + def build_groups(count) |
| 107 | + group_count = 0 |
| 108 | + person_count = 0 |
| 109 | + |
| 110 | + count.times.map do |i| |
| 111 | + group = Group.new |
| 112 | + group.id = group_count + 1 |
| 113 | + group.name = "Test Group #{group.id}" |
| 114 | + group_count = group.id |
| 115 | + |
| 116 | + person = Person.new |
| 117 | + person.id = person_count + 1 |
| 118 | + person.last_name = "Last Name #{person.id}" |
| 119 | + person.first_name = "First Name #{person.id}" |
| 120 | + person_count = person.id |
| 121 | + |
| 122 | + child_group = Group.new |
| 123 | + child_group.id = group_count + 1 |
| 124 | + child_group.name = "Test Group #{child_group.id}" |
| 125 | + group_count = child_group.id |
| 126 | + |
| 127 | + group.groupees = [person, child_group] |
| 128 | + group |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments