Skip to content

Commit

Permalink
Merge pull request #386 from kbrock/parse
Browse files Browse the repository at this point in the history
trim object allocations while parsing ancestry col
  • Loading branch information
kbrock authored Apr 30, 2018
2 parents 47a8ecd + 8b74ee2 commit 9f226a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
19 changes: 19 additions & 0 deletions lib/ancestry/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,24 @@ def unscoped_where
yield self.ancestry_base_class.unscope(:where)
end
end

ANCESTRY_UNCAST_TYPES = [:string, :uuid, :text].freeze
if ActiveSupport::VERSION::STRING < "4.0"
def primary_key_is_an_integer?
if defined?(@primary_key_is_an_integer)
@primary_key_is_an_integer
else
@primary_key_is_an_integer = !ANCESTRY_UNCAST_TYPES.include?(columns_hash[primary_key.to_s].type)
end
end
else
def primary_key_is_an_integer?
if defined?(@primary_key_is_an_integer)
@primary_key_is_an_integer
else
@primary_key_is_an_integer = !ANCESTRY_UNCAST_TYPES.include?(type_for_attribute(primary_key))
end
end
end
end
end
18 changes: 4 additions & 14 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ def ancestry_changed?
changed.include?(self.ancestry_base_class.ancestry_column.to_s)
end

def parse_ancestry_column obj
obj.to_s.split('/').map { |id| cast_primary_key(id) }
end

def ancestor_ids
parse_ancestry_column(read_attribute(self.ancestry_base_class.ancestry_column))
end
Expand Down Expand Up @@ -288,16 +284,10 @@ def ancestry_callbacks_disabled?

private

def cast_primary_key(key)
if [:string, :uuid, :text].include? primary_key_type
key
else
key.to_i
end
end

def primary_key_type
@primary_key_type ||= column_for_attribute(self.class.primary_key).type
def parse_ancestry_column obj
obj_ids = obj.to_s.split('/')
obj_ids.map!(&:to_i) if self.class.primary_key_is_an_integer?
obj_ids
end

def unscoped_descendants
Expand Down

0 comments on commit 9f226a4

Please sign in to comment.