Skip to content

Commit

Permalink
Classification: Use nil for no parent
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Jun 13, 2019
1 parent d1f1db7 commit 21aec89
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
20 changes: 11 additions & 9 deletions app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Classification < ApplicationRecord
scope :read_only, -> { where(:read_only => true) }
scope :writeable, -> { where(:read_only => false) }

scope :is_category, -> { where(:parent_id => 0) }
scope :is_entry, -> { where.not(:parent_id => 0) }
scope :is_category, -> { where(:parent_id => nil) }
scope :is_entry, -> { where.not(:parent_id => nil) }

scope :with_writable_parents, -> { includes(:parent).where(:parents_classifications => { :read_only => false}) }

Expand Down Expand Up @@ -313,7 +313,7 @@ def to_tag
end

def category?
parent_id == 0
parent_id.nil?
end

def category
Expand All @@ -334,11 +334,13 @@ def find_entry_by_name(name, region_id = my_region_number)
self.class.find_by_name(name, region_id, ns, self)
end

def self.find_by_name(name, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = 0)
# @param parent_id [Integer|Parent] node for the parent. This is only passed in for find_entry_by_name
def self.find_by_name(name, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = nil)
find_by_names([name], region_id, ns, parent_id).first
end

def self.find_by_names(names, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = 0)
# @param parent_id [Integer|Parent] node for the parent. This is only passed in for find_entry_by_name
def self.find_by_names(names, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = nil)
tag_names = names.map { |name| name2tag(name, parent_id, ns) }
# NOTE: tags is a subselect - not an array of ids
tags = Tag.in_region(region_id).where(:name => tag_names).select(:id)
Expand Down Expand Up @@ -503,8 +505,8 @@ def validate_uniqueness_on_tag_name
errors.add("name", "has already been taken") if Classification.exists?(cond)
end

def self.name2tag(name, parent_id = 0, ns = DEFAULT_NAMESPACE)
if parent_id == 0
def self.name2tag(name, parent_id = nil, ns = DEFAULT_NAMESPACE)
if parent_id.nil?
File.join(ns, name)
else
c = parent_id.kind_of?(Classification) ? parent_id : Classification.find(parent_id)
Expand All @@ -525,12 +527,12 @@ def tag2name(tag)
end

def find_tag
tag_name = Classification.name2tag(name, parent_id, ns)
tag_name = Classification.name2tag(name, parent, ns)
Tag.in_region(region_id).find_by(:name => tag_name)
end

def save_tag
tag_name = Classification.name2tag(name, parent_id, ns)
tag_name = Classification.name2tag(name, parent, ns)
if tag_id.present? || tag.present?
tag.update_attributes(:name => tag_name) unless tag.name == tag_name
else
Expand Down
1 change: 0 additions & 1 deletion spec/factories/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
factory(:category) do
sequence(:name) { |n| "category_#{seq_padded_for_sorting(n)}" }
sequence(:description) { |n| "category #{seq_padded_for_sorting(n)}" }
parent_id { 0 }
end
end
1 change: 0 additions & 1 deletion spec/factories/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
factory :classification do
sequence(:name) { |n| "category_#{seq_padded_for_sorting(n)}" }
sequence(:description) { |n| "category #{seq_padded_for_sorting(n)}" }
parent_id { 0 }
end

factory :classification_tag, :class => :Classification do
Expand Down

0 comments on commit 21aec89

Please sign in to comment.