Skip to content

count should support a block #316

@ashleyHutton

Description

@ashleyHutton

Description

Hi! Thanks for this gem - it has been a go-to for readonly data in our projects.

We ran into an issue in our application where calling count with a block on a relation returned the count of elements in the relation, not the count of elements for which the block returns true.

Would it be possible for count to fallback to Enumerable#count if given a block? Rails introduced this in 5.1 for ActiveRecord::Relation and I think it'd made sense for ActiveHash::Relation to behave the same.

Code to Reproduce

class Example < ActiveHash::Base
  self.data = [
    { id: 1, active: true },
    { id: 2, active: true },
    { id: 3, active: false }
  ]
end

Example.count
# 3
Example.count { |e| e.active }
# 3
Example.all.to_a.count { |e| e.active }
# 2

# Desired behavior:
# Example.count { |e| e.active }
# 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions