Skip to content

Commit 08edbc8

Browse files
authored
Merge pull request #1298 from bf4/better_poro_test
RFC: Better Poro Resource Test
2 parents 52e52d2 + 95393cd commit 08edbc8

File tree

1 file changed

+90
-17
lines changed

1 file changed

+90
-17
lines changed

test/fixtures/active_record.rb

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,37 +1546,118 @@ class PoroResource < JSONAPI::BasicResource
15461546
root_resource
15471547

15481548
class << self
1549+
def find_records(filters, options)
1550+
fail NotImplementedError, <<~EOF
1551+
Should be something like
1552+
def find_records(filters, options)
1553+
breeds = []
1554+
id_filter = filters[:id]
1555+
id_filter = [id_filter] unless id_filter.nil? || id_filter.is_a?(Array)
1556+
$breed_data.breeds.values.each do |breed|
1557+
breeds.push(breed) unless id_filter && !id_filter.include?(breed.id)
1558+
end
1559+
breeds
1560+
end
1561+
EOF
1562+
end
1563+
1564+
def find_record_by_key(key, options = {})
1565+
fail NotImplementedError, <<~EOF
1566+
Should be something like
1567+
def find_record_by_key(key, options = {})
1568+
$breed_data.breeds[key.to_i]
1569+
end
1570+
EOF
1571+
end
1572+
1573+
def find_records_by_keys(keys, options = {})
1574+
fail NotImplementedError, <<~EOF
1575+
Should be something like
1576+
def find_records_by_keys(keys, options = {})
1577+
breeds = []
1578+
keys.each do |key|
1579+
breeds.push($breed_data.breeds[key.to_i])
1580+
end
1581+
breeds
1582+
end
1583+
EOF
1584+
end
1585+
1586+
# Finds Resources using the `filters`. Pagination and sort options are used when provided
1587+
#
1588+
# @param filters [Hash] the filters hash
1589+
# @option options [Hash] :context The context of the request, set in the controller
1590+
# @option options [Hash] :sort_criteria The `sort criteria`
1591+
# @option options [Hash] :include_directives The `include_directives`
1592+
#
1593+
# @return [Array<Resource>] the Resource instances matching the filters, sorting and pagination rules.
15491594
def find(filters, options = {})
1550-
records = find_breeds(filters, options)
1595+
records = find_records(filters, options)
15511596
resources_for(records, options[:context])
15521597
end
15531598

15541599
# Records
15551600
def find_fragments(filters, options = {})
15561601
fragments = {}
1557-
find_breeds(filters, options).each do |breed|
1558-
rid = JSONAPI::ResourceIdentity.new(BreedResource, breed.id)
1602+
find_records(filters, options).each do |record|
1603+
rid = JSONAPI::ResourceIdentity.new(resource_klass, record.id)
15591604
fragments[rid] = JSONAPI::ResourceFragment.new(rid)
15601605
end
15611606
fragments
15621607
end
15631608

1609+
def resource_klass
1610+
self
1611+
end
1612+
1613+
# Counts Resources found using the `filters`
1614+
#
1615+
# @param filters [Hash] the filters hash
1616+
# @option options [Hash] :context The context of the request, set in the controller
1617+
#
1618+
# @return [Integer] the count
1619+
def count(filters, options = {})
1620+
fail NotImplementedError, <<~EOF
1621+
Should be something like
1622+
def count(filters, options)
1623+
0
1624+
end
1625+
EOF
1626+
end
1627+
1628+
# Returns the single Resource identified by `key`
1629+
#
1630+
# @param key the primary key of the resource to find
1631+
# @option options [Hash] :context The context of the request, set in the controller
15641632
def find_by_key(key, options = {})
1565-
record = find_breed_by_key(key, options)
1633+
record = find_record_by_key(key, options)
15661634
resource_for(record, options[:context])
15671635
end
15681636

15691637
def find_to_populate_by_keys(keys, options = {})
15701638
find_by_keys(keys, options)
15711639
end
15721640

1641+
# Returns an array of Resources identified by the `keys` array
1642+
#
1643+
# @param keys [Array<key>] Array of primary keys to find resources for
1644+
# @option options [Hash] :context The context of the request, set in the controller
15731645
def find_by_keys(keys, options = {})
1574-
records = find_breeds_by_keys(keys, options)
1646+
records = find_records_by_keys(keys, options)
15751647
resources_for(records, options[:context])
15761648
end
1649+
end
1650+
end
15771651

1578-
#
1579-
def find_breeds(filters, options = {})
1652+
class BreedResource < PoroResource
1653+
1654+
attribute :name, format: :title
1655+
1656+
# This is unneeded, just here for testing
1657+
routing_options param: :id
1658+
1659+
class << self
1660+
def find_records(filters, options = {})
15801661
breeds = []
15811662
id_filter = filters[:id]
15821663
id_filter = [id_filter] unless id_filter.nil? || id_filter.is_a?(Array)
@@ -1586,26 +1667,18 @@ def find_breeds(filters, options = {})
15861667
breeds
15871668
end
15881669

1589-
def find_breed_by_key(key, options = {})
1670+
def find_record_by_key(key, options = {})
15901671
$breed_data.breeds[key.to_i]
15911672
end
15921673

1593-
def find_breeds_by_keys(keys, options = {})
1674+
def find_records_by_keys(keys, options = {})
15941675
breeds = []
15951676
keys.each do |key|
15961677
breeds.push($breed_data.breeds[key.to_i])
15971678
end
15981679
breeds
15991680
end
16001681
end
1601-
end
1602-
1603-
class BreedResource < PoroResource
1604-
1605-
attribute :name, format: :title
1606-
1607-
# This is unneeded, just here for testing
1608-
routing_options param: :id
16091682

16101683
def _save
16111684
super

0 commit comments

Comments
 (0)