Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_file/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def actual_root_path
end
protected :actual_root_path

[:find, :find_by_id, :all, :where, :method_missing].each do |method|
[:find, :find_by_id, :all, :where, :method_missing, :find_each].each do |method|
define_method(method) do |*args, &block|
reload unless data_loaded
return super(*args, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def all(options = {})
relation
end

delegate :where, :find, :find_by, :find_by!, :find_by_id, :count, :pluck, :ids, :pick, :first, :last, :order, to: :all
delegate :where, :find_each, :find, :find_by, :find_by!, :find_by_id, :count, :pluck, :ids, :pick, :first, :last, :order, to: :all

def transaction
yield
Expand Down
2 changes: 2 additions & 0 deletions lib/active_hash/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Relation
delegate :empty?, :length, :first, :second, :third, :last, to: :records
delegate :sample, to: :records

alias find_each each

attr_reader :conditions, :order_values, :klass, :all_records

def initialize(klass, all_records, conditions = nil, order_values = nil)
Expand Down
17 changes: 17 additions & 0 deletions spec/active_file/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ class Bar < ActiveFile::Base
end
end

describe ".find_each" do
before do
class Country
def self.load_file()
[{"name"=>"Niger", "id"=>1}, {"name"=>"Peru", "id"=>2}]
end
end
end

it "iterates over the data" do
first_letters = ""
expect do
Country.find_each { |country| first_letters += country.name[0] }
end.to change { first_letters }.to("NP")
end
end

describe ".set_filename" do
before do
Country.set_filename "foo-izzle"
Expand Down
20 changes: 20 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,26 @@ class Region < ActiveHash::Base
end
end

describe ".find_each" do
before do
Country.field :name
Country.field :language
Country.data = [
{:id => 1, :name => "US", :language => 'English'},
{:id => 2, :name => "Canada", :language => 'English'},
{:id => 3, :name => "Mexico", :language => 'Spanish'}
]
end

it "iterates over data" do
logs = []
Country.where(language: 'English').find_each do |country|
logs << "visited #{country.name}"
end
expect(logs).to eq(["visited US", "visited Canada"])
end
end

describe ".invert_where" do
before do
Country.field :name
Expand Down