From 7edc07cb016bebb9b6c06e105f88f595d5ad00c3 Mon Sep 17 00:00:00 2001 From: hukl Date: Thu, 29 Jan 2009 22:30:00 +0100 Subject: added generation of unique names based on the position on the tree after adding a node as a children of another node --- app/models/node.rb | 10 ++++++++++ config/environment.rb | 4 +++- lib/awesome_patch.rb | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 lib/awesome_patch.rb diff --git a/app/models/node.rb b/app/models/node.rb index 82183c9..e2ff973 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -1,3 +1,13 @@ class Node < ActiveRecord::Base acts_as_nested_set + + # returns array with pages up to root excluding root + def path_to_root + parent.nil? && [slug] || parent.path_to_root.push(slug) + end + + def update_unique_name + path = self.path_to_root[1..-1] + self.unique_name = path.join("/") + end end diff --git a/config/environment.rb b/config/environment.rb index c53ab90..aad0815 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -38,4 +38,6 @@ Rails::Initializer.run do |config| # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] # config.i18n.default_locale = :de -end \ No newline at end of file +end + +require 'awesome_patch' \ No newline at end of file diff --git a/lib/awesome_patch.rb b/lib/awesome_patch.rb new file mode 100644 index 0000000..1694ccf --- /dev/null +++ b/lib/awesome_patch.rb @@ -0,0 +1,17 @@ +module CollectiveIdea + module Acts #:nodoc: + module NestedSet + module InstanceMethods + alias_method :move_to_original, :move_to + + def move_to target, position + move_to_original target, position + if self.class == Node + self.update_unique_name + self.save + end + end + end + end + end +end \ No newline at end of file -- cgit v1.3