From 1e455329edcc3d9e35a9fa82102d38b33251896b Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 22 Feb 2008 00:05:11 +0000 Subject: [PATCH] ActionItem172: generating URL hashes instead of actual URL's, since Profile#url is almost always used in link_to calls --- app/models/article.rb | 10 +--------- app/models/change_password.rb | 5 ++--- app/models/profile.rb | 8 ++------ app/models/recent_documents_block.rb | 1 + app/models/rss_feed.rb | 7 ++++--- app/models/tags_block.rb | 3 ++- app/views/cms/view.rhtml | 2 +- test/unit/article_test.rb | 22 ++++++++-------------- test/unit/profile_test.rb | 8 ++++---- test/unit/rss_feed_test.rb | 12 +++++++----- 10 files changed, 32 insertions(+), 46 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index 7f52d8e..e08c41f 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -69,14 +69,6 @@ class Article < ActiveRecord::Base name end - def public_path(with_profile = true) - elements = [path] - if with_profile - elements.unshift(profile.identifier) - end - "/" + elements.join('/') - end - def self.short_description if self == Article _('Article') @@ -98,7 +90,7 @@ class Article < ActiveRecord::Base end def url - self.profile.url + self.public_path(false) + self.profile.url.merge(:page => path.split('/')) end def allow_children? diff --git a/app/models/change_password.rb b/app/models/change_password.rb index 1e57af3..42c6e79 100644 --- a/app/models/change_password.rb +++ b/app/models/change_password.rb @@ -2,8 +2,6 @@ class ChangePassword < Task - include Noosfero::URL - serialize :data, Hash def data self[:data] ||= {} @@ -69,10 +67,11 @@ class ChangePassword < Task _('Your password was changed successfully.') end + include ActionController::UrlWriter def task_created_message hostname = self.requestor.environment.default_hostname code = self.code - url = generate_url(:host => hostname, :controller => 'account', :action => 'new_password', :code => code) + url = url_for(:host => hostname, :controller => 'account', :action => 'new_password', :code => code) lambda do _("In order to change your password, please visit the following address:\n\n%s") % url diff --git a/app/models/profile.rb b/app/models/profile.rb index 346064a..faf7bf9 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -183,7 +183,6 @@ class Profile < ActiveRecord::Base false end - include ActionController::UrlWriter def url generate_url(url_options.merge(:controller => 'content_viewer', :action => 'view_page', :page => [])) end @@ -197,14 +196,11 @@ class Profile < ActiveRecord::Base end def generate_url(options) - url_for(url_options.merge(options)) + url_options.merge(options) end def url_options - options = { :host => self.environment.default_hostname, :profile => self.identifier} - options.merge!(:port => 3000) if ENV['RAILS_ENV'] == 'development' - - options + { :host => self.environment.default_hostname, :profile => self.identifier} end # FIXME this can be SLOW diff --git a/app/models/recent_documents_block.rb b/app/models/recent_documents_block.rb index 95fde72..6e75ea9 100644 --- a/app/models/recent_documents_block.rb +++ b/app/models/recent_documents_block.rb @@ -6,6 +6,7 @@ class RecentDocumentsBlock < Block settings_items :limit + include ActionController::UrlWriter def content docs = if self.limit.nil? diff --git a/app/models/rss_feed.rb b/app/models/rss_feed.rb index 0aaba64..2e1177e 100644 --- a/app/models/rss_feed.rb +++ b/app/models/rss_feed.rb @@ -52,6 +52,7 @@ class RssFeed < Article 'text/xml' end + include ActionController::UrlWriter def data articles = if (self.include == 'parent_and_children') && self.parent @@ -68,7 +69,7 @@ class RssFeed < Article xml.rss(:version=>"2.0") do xml.channel do xml.title(_("%s's RSS feed") % (self.profile.name)) - xml.link(self.profile.url) + xml.link(url_for(self.profile.url)) xml.description(_("%s's content published at %s") % [self.profile.name, self.profile.environment.name]) xml.language("pt_BR") for article in articles @@ -83,8 +84,8 @@ class RssFeed < Article # rfc822 xml.pubDate(article.created_on.rfc2822) # link to article - xml.link(article.url) - xml.guid(article.url) + xml.link(url_for(article.url)) + xml.guid(url_for(article.url)) end end end diff --git a/app/models/tags_block.rb b/app/models/tags_block.rb index 67f05d0..fb28ca2 100644 --- a/app/models/tags_block.rb +++ b/app/models/tags_block.rb @@ -2,6 +2,7 @@ class TagsBlock < Block include TagsHelper include BlockHelper + include ActionController::UrlWriter def self.description _('Block listing content count by tag') @@ -16,7 +17,7 @@ class TagsBlock < Block block_title(_('Tags')) + "\n
\n"+ tag_cloud( owner.tags, :id, - owner.generate_url(:controller => 'profile', :action => 'tag') + '/', + owner.generate_url(:controller => 'profile', :action => 'tag'), :max_size => 18, :min_size => 9 ) + "\n
\n"; end diff --git a/app/views/cms/view.rhtml b/app/views/cms/view.rhtml index b41bcde..663bd47 100644 --- a/app/views/cms/view.rhtml +++ b/app/views/cms/view.rhtml @@ -59,7 +59,7 @@ <%= _('"%{article}", last changed by %{author}') % { :article => @article.name, :author => (@article.last_changed_by ? @article.last_changed_by.name : _('Unknown User')) } %>
  • - <%= _('Public address of this article: %s') % link_to(@article.public_path, @article.public_path) %> + <%= _('Public address of this article: %s') % link_to(@article.url, @article.url) %>
  • <%= _('Tags:') %> <%= @article.tag_list %> diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index b9e7159..ad88a8b 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -106,19 +106,6 @@ class ArticleTest < Test::Unit::TestCase assert a4.errors.invalid?(:slug) end - should 'calculate public path' do - # top level - a = profile.articles.build(:name => 'aaa') - a.save! - assert_equal "/#{profile.identifier}/aaa", a.public_path - - # child articles - b = profile.articles.build(:name => 'bbb') - b.parent = a - b.save! - assert_equal "/#{profile.identifier}/aaa/bbb", b.public_path - end - should 'record who did the last change' do a = profile.articles.build(:name => 'test') @@ -172,7 +159,14 @@ class ArticleTest < Test::Unit::TestCase article = profile.articles.build(:name => 'myarticle') article.save! - assert_equal(profile.url + "/myarticle", article.url) + assert_equal(profile.url.merge(:page => ['myarticle']), article.url) + end + + should 'provide a url to itself having a parent topic' do + parent = profile.articles.build(:name => 'parent'); parent.save! + child = profile.articles.build(:name => 'child', :parent => parent); child.save! + + assert_equal(profile.url.merge(:page => [ 'parent', 'child']), child.url) end should 'associate with categories' do diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 9690e91..448aaa8 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -247,23 +247,23 @@ class ProfileTest < Test::Unit::TestCase should 'provide url to itself' do profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) - assert_equal 'http://mycolivre.net/testprofile', profile.url + assert_equal({ :host => 'mycolivre.net', :profile => 'testprofile', :controller => 'content_viewer', :action => 'view_page', :page => []}, profile.url) end should 'provide URL to admin area' do profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) - assert_equal 'http://mycolivre.net/myprofile/testprofile', profile.admin_url + assert_equal({ :host => 'mycolivre.net', :profile => 'testprofile', :controller => 'profile_editor', :action => 'index'}, profile.admin_url) end should 'provide URL to public profile' do profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) - assert_equal 'http://mycolivre.net/profile/testprofile', profile.public_profile_url + assert_equal({ :host => 'mycolivre.net', :profile => 'testprofile', :controller => 'profile', :action => 'index' }, profile.public_profile_url) end should 'generate URL' do profile = Profile.create!(:name => "Test Profile", :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) - assert_equal 'http://mycolivre.net/profile/testprofile/friends', profile.generate_url(:controller => 'profile', :action => 'friends') + assert_equal({ :host => 'mycolivre.net', :profile => 'testprofile', :controller => 'profile', :action => 'friends' }, profile.generate_url(:controller => 'profile', :action => 'friends')) end should 'provide URL options' do diff --git a/test/unit/rss_feed_test.rb b/test/unit/rss_feed_test.rb index a104bc2..7e39205 100644 --- a/test/unit/rss_feed_test.rb +++ b/test/unit/rss_feed_test.rb @@ -32,7 +32,7 @@ class RssFeedTest < Test::Unit::TestCase feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! - + rss = feed.data assert_match /article 1<\/title>/, rss assert_match /<item><title>article 2<\/title>/, rss @@ -48,7 +48,7 @@ class RssFeedTest < Test::Unit::TestCase feed = RssFeed.new(:name => 'testfeed') feed.profile = profile feed.save! - + rss = feed.data assert_no_match /<item><title>testfeed<\/title>/, rss end @@ -112,7 +112,9 @@ class RssFeedTest < Test::Unit::TestCase feed.profile = profile feed.save! - assert_match "<link>#{profile.url}</link>", feed.data + profile.environment.expects(:default_hostname).returns('mysite.net').at_least_once + + assert_match "<link>http://mysite.net/testuser</link>", feed.data end should 'provide link to each article' do @@ -123,8 +125,8 @@ class RssFeedTest < Test::Unit::TestCase feed.save! data = feed.data - assert_match "<link>#{art.url}</link>", data - assert_match "<guid>#{art.url}</guid>", data + assert_match "<link>http://#{art.profile.environment.default_hostname}/testuser/myarticle</link>", data + assert_match "<guid>http://#{art.profile.environment.default_hostname}/testuser/myarticle</guid>", data end should 'be able to indicate maximum number of items' do -- libgit2 0.21.2