Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give objects without instance variables to users #125

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 6 additions & 3 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def all(options={})
if options.has_key?(:conditions)
where(options[:conditions])
else
@records || []
@records && @records.clone || []
end
end

Expand All @@ -161,15 +161,18 @@ def where(options)
if (ids = (options.delete(:id) || options.delete("id")))
candidates = Array.wrap(ids).map { |id| find_by_id(id) }
end
return candidates if options.blank?
return candidates.clone if options.blank?

(candidates || @records || []).select do |record|
match_options?(record, options)
end.map do |record|
record.clone
end
end

def find_by(options)
where(options).first
result = where(options).first
result && result.clone || nil
end

def match_options?(record, options)
Expand Down
27 changes: 27 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ class Region < ActiveHash::Base
record.first.id.should == 1
record.first.name.should == 'US'
end

it "returns objects without instance variables" do
record = Country.all(:conditions => {:name => 'US'}).first
record.instance_variable_set(:@temporary, "value")
record.instance_variables.should include(:@temporary)

new_instance = Country.all(:conditions => {:name => 'US'}).first
new_instance.instance_variables.should_not include(:@temporary)
end
end

describe ".where" do
Expand Down Expand Up @@ -276,6 +285,15 @@ class Region < ActiveHash::Base
it "filters records for multiple values" do
expect(Country.where(:name => %w(US Canada)).map(&:name)).to match_array(%w(US Canada))
end

it "returns objects without instance variables" do
record = Country.where(:name => 'US').first
record.instance_variable_set(:@temporary, "value")
record.instance_variables.should include(:@temporary)

new_instance = Country.where(:name => 'US').first
new_instance.instance_variables.should_not include(:@temporary)
end
end

describe ".find_by" do
Expand Down Expand Up @@ -336,6 +354,15 @@ class Region < ActiveHash::Base
it "returns nil when not matched in candidates" do
expect(Country.find_by(:name => "UK")).to be_nil
end

it "returns objects without instance variables" do
record = Country.find_by(:id => '1')
record.instance_variable_set(:@temporary, "value")
record.instance_variables.should include(:@temporary)

new_instance = Country.find_by(:id => '1')
new_instance.instance_variables.should_not include(:@temporary)
end
end

describe ".count" do
Expand Down