@@ -1546,37 +1546,118 @@ class PoroResource < JSONAPI::BasicResource
1546
1546
root_resource
1547
1547
1548
1548
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.
1549
1594
def find ( filters , options = { } )
1550
- records = find_breeds ( filters , options )
1595
+ records = find_records ( filters , options )
1551
1596
resources_for ( records , options [ :context ] )
1552
1597
end
1553
1598
1554
1599
# Records
1555
1600
def find_fragments ( filters , options = { } )
1556
1601
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 )
1559
1604
fragments [ rid ] = JSONAPI ::ResourceFragment . new ( rid )
1560
1605
end
1561
1606
fragments
1562
1607
end
1563
1608
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
1564
1632
def find_by_key ( key , options = { } )
1565
- record = find_breed_by_key ( key , options )
1633
+ record = find_record_by_key ( key , options )
1566
1634
resource_for ( record , options [ :context ] )
1567
1635
end
1568
1636
1569
1637
def find_to_populate_by_keys ( keys , options = { } )
1570
1638
find_by_keys ( keys , options )
1571
1639
end
1572
1640
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
1573
1645
def find_by_keys ( keys , options = { } )
1574
- records = find_breeds_by_keys ( keys , options )
1646
+ records = find_records_by_keys ( keys , options )
1575
1647
resources_for ( records , options [ :context ] )
1576
1648
end
1649
+ end
1650
+ end
1577
1651
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 = { } )
1580
1661
breeds = [ ]
1581
1662
id_filter = filters [ :id ]
1582
1663
id_filter = [ id_filter ] unless id_filter . nil? || id_filter . is_a? ( Array )
@@ -1586,26 +1667,18 @@ def find_breeds(filters, options = {})
1586
1667
breeds
1587
1668
end
1588
1669
1589
- def find_breed_by_key ( key , options = { } )
1670
+ def find_record_by_key ( key , options = { } )
1590
1671
$breed_data. breeds [ key . to_i ]
1591
1672
end
1592
1673
1593
- def find_breeds_by_keys ( keys , options = { } )
1674
+ def find_records_by_keys ( keys , options = { } )
1594
1675
breeds = [ ]
1595
1676
keys . each do |key |
1596
1677
breeds . push ( $breed_data. breeds [ key . to_i ] )
1597
1678
end
1598
1679
breeds
1599
1680
end
1600
1681
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
1609
1682
1610
1683
def _save
1611
1684
super
0 commit comments