Skip to content

Instantly share code, notes, and snippets.

@travisdmathis
Last active August 29, 2015 14:12
Show Gist options
  • Save travisdmathis/fc2811bfe8383eecabe7 to your computer and use it in GitHub Desktop.
Save travisdmathis/fc2811bfe8383eecabe7 to your computer and use it in GitHub Desktop.
def compare_child_names_to_new_folder_name(folder_name, children)
children.each do |child|
if folder_name == child.name
return false
else
return true
end
end
end
def is_catalog_unique?(catalogs, user, folder_name)
folder_name = folder_name.gsub("+", " ")
if catalogs.blank?
@catalog = user.root_catalog
children = @catalog.live_children
self.compare_child_names_to_new_folder_name(folder_name, children)
else
catalogs.each do |catalog_id|
@catalog = user.role.catalogs.find_by_id(catalog_id)
children = @catalog.live_children
self.compare_child_names_to_new_folder_name(folder_name, children)
end
end
end
@beauby
Copy link

beauby commented Dec 29, 2014

def is_catalog_unique?(catalogs, user, folder_name)
  folder_name = folder_name.gsub("+", " ")

  if catalogs.blank?
     @catalog = user.root_catalog
     children = @catalog.live_children

      break false if children.any? { |child| child == folder_name }
  else
    catalogs.each do |catalog_id|
      @catalog = user.role.catalogs.find_by_id(catalog_id)
      children = @catalog.live_children

      break false if children.any? { |child| child == folder_name }
    end
  end
end

@beauby
Copy link

beauby commented Dec 29, 2014

   def is_catalog_unique?(catalogs, user, folder_name)
     folder_name = folder_name.gsub("+", " ")
     catalogs = if catalogs.blank? then [user.root_catalog] else catalogs.map { |c_id| user.role.catalogs.find_by_id(c_id) } end
     catalogs.all? do |catalog|
         !catalog.live_children.any? { |child| child == folder_name }
     end
   end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment