diff options
| author | hukl <hukl@berlin.ccc.de> | 2009-01-29 22:30:00 +0100 |
|---|---|---|
| committer | hukl <hukl@berlin.ccc.de> | 2009-01-29 22:30:00 +0100 |
| commit | 7edc07cb016bebb9b6c06e105f88f595d5ad00c3 (patch) | |
| tree | 4bb6e9ef4eb3d7f5ddb877d20bd2286e440f07c4 | |
| parent | be47a4bbeac59d85af16b5e83e10ff242628519d (diff) | |
added generation of unique names based on the position on the tree after adding a node as a children of another node
| -rw-r--r-- | app/models/node.rb | 10 | ||||
| -rw-r--r-- | config/environment.rb | 4 | ||||
| -rw-r--r-- | lib/awesome_patch.rb | 17 |
3 files changed, 30 insertions, 1 deletions
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 @@ | |||
| 1 | class Node < ActiveRecord::Base | 1 | class Node < ActiveRecord::Base |
| 2 | acts_as_nested_set | 2 | acts_as_nested_set |
| 3 | |||
| 4 | # returns array with pages up to root excluding root | ||
| 5 | def path_to_root | ||
| 6 | parent.nil? && [slug] || parent.path_to_root.push(slug) | ||
| 7 | end | ||
| 8 | |||
| 9 | def update_unique_name | ||
| 10 | path = self.path_to_root[1..-1] | ||
| 11 | self.unique_name = path.join("/") | ||
| 12 | end | ||
| 3 | end | 13 | 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| | |||
| 38 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. | 38 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. |
| 39 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] | 39 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] |
| 40 | # config.i18n.default_locale = :de | 40 | # config.i18n.default_locale = :de |
| 41 | end \ No newline at end of file | 41 | end |
| 42 | |||
| 43 | 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 @@ | |||
| 1 | module CollectiveIdea | ||
| 2 | module Acts #:nodoc: | ||
| 3 | module NestedSet | ||
| 4 | module InstanceMethods | ||
| 5 | alias_method :move_to_original, :move_to | ||
| 6 | |||
| 7 | def move_to target, position | ||
| 8 | move_to_original target, position | ||
| 9 | if self.class == Node | ||
| 10 | self.update_unique_name | ||
| 11 | self.save | ||
| 12 | end | ||
| 13 | end | ||
| 14 | end | ||
| 15 | end | ||
| 16 | end | ||
| 17 | end \ No newline at end of file | ||
