Skip to content

Commit

Permalink
Fix bug where records can't be added to an originally empty ActiveJSO…
Browse files Browse the repository at this point in the history
…N datastore

Simple fix would be to replace `@records = nil` with `@records = []`,
but the suggested approach as a better impact on the code base, avoiding
repetitions such as `@records || []`.
  • Loading branch information
davidstosik committed Oct 7, 2020
1 parent 1cec394 commit 361ca56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def data

def data=(array_of_hashes)
mark_dirty
@records = nil
records.clear
reset_record_index
self._data = array_of_hashes
if array_of_hashes
Expand All @@ -135,13 +135,12 @@ def exists?(record)
end

def insert(record)
@records ||= []
record[:id] ||= next_id
validate_unique_id(record) if dirty
mark_dirty

add_to_record_index({ record.id.to_s => @records.length })
@records << record
add_to_record_index({ record.id.to_s => records.length })
records << record
end

def next_id
Expand All @@ -153,6 +152,12 @@ def next_id
end
end

def records
@records ||= []
end

private :records

def record_index
@record_index ||= {}
end
Expand Down Expand Up @@ -193,7 +198,7 @@ def create!(attributes = {})
end

def all(options = {})
ActiveHash::Relation.new(self, @records || [], options[:conditions] || {})
ActiveHash::Relation.new(self, records, options[:conditions] || {})
end

delegate :where, :find, :find_by, :find_by!, :find_by_id, :count, :pluck, :pick, :first, :last, :order, to: :all
Expand All @@ -211,7 +216,7 @@ def transaction
def delete_all
mark_dirty
reset_record_index
@records = []
records.clear
end

def fields(*args)
Expand Down
2 changes: 1 addition & 1 deletion lib/enum/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def insert(record)

def delete_all
if @enum_accessors.present?
@records.each do |record|
records.each do |record|
constant = constant_for(record, @enum_accessors)
remove_const(constant) if const_defined?(constant, false)
end
Expand Down

0 comments on commit 361ca56

Please sign in to comment.