Commit 8d5b096c970c816e8b21acb4560380b954d6963e
1 parent
f1bd1bbc
Exists in
master
and in
28 other branches
tUpdating path before save
Showing
2 changed files
with
24 additions
and
0 deletions
Show diff stats
app/models/article.rb
... | ... | @@ -154,6 +154,12 @@ class Article < ActiveRecord::Base |
154 | 154 | article.advertise = true |
155 | 155 | end |
156 | 156 | |
157 | + before_save do |article| | |
158 | + article.parent = article.parent_id ? Article.find(article.parent_id) : nil | |
159 | + parent_path = article.parent ? article.parent.path : nil | |
160 | + article.path = [parent_path, article.slug].compact.join('/') | |
161 | + end | |
162 | + | |
157 | 163 | # retrieves all articles belonging to the given +profile+ that are not |
158 | 164 | # sub-articles of any other article. |
159 | 165 | named_scope :top_level_for, lambda { |profile| | ... | ... |
test/unit/article_test.rb
... | ... | @@ -1782,4 +1782,22 @@ class ArticleTest < ActiveSupport::TestCase |
1782 | 1782 | assert_equal license, article.license |
1783 | 1783 | end |
1784 | 1784 | |
1785 | + should 'update path if parent is changed' do | |
1786 | + f1 = Folder.create!(:name => 'Folder 1', :profile => profile) | |
1787 | + f2 = Folder.create!(:name => 'Folder 2', :profile => profile) | |
1788 | + article = TinyMceArticle.create!(:name => 'Sample Article', :parent_id => f1.id, :profile => profile) | |
1789 | + assert_equal [f1.path,article.slug].join('/'), article.path | |
1790 | + | |
1791 | + article.parent = f2 | |
1792 | + article.save! | |
1793 | + assert_equal [f2.path,article.slug].join('/'), article.path | |
1794 | + | |
1795 | + article.parent = nil | |
1796 | + article.save! | |
1797 | + assert_equal article.slug, article.path | |
1798 | + | |
1799 | + article.update_attributes({:parent_id => f2.id}) | |
1800 | + assert_equal [f2.path,article.slug].join('/'), article.path | |
1801 | + end | |
1802 | + | |
1785 | 1803 | end | ... | ... |