{"id":18,"date":"2007-02-14T00:29:40","date_gmt":"2007-02-13T23:29:40","guid":{"rendered":"http:\/\/schuerig.de\/michael\/blog\/index.php\/2007\/02\/14\/popular-requests\/"},"modified":"2021-11-24T09:20:09","modified_gmt":"2021-11-24T08:20:09","slug":"popular-requests","status":"publish","type":"post","link":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2007\/02\/14\/popular-requests\/","title":{"rendered":"Popular Requests"},"content":{"rendered":"<p>It was late last night. And the night before, I admit it. I also admit that I don&#8217;t know much about what <em>Web 2.0<\/em> is all about, but there are two things that I do know: rounded corners are involved and, of course tags and tag clouds.<\/p>\n<p>Tagging is work, therefore <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ipso_facto\"><em>ipso facto<\/em><\/a> to be avoided, better still: automated.<\/p>\n<p>Let&#8217;s say you want to show people which part of your site are popular. How about a tag cloud of the most requested locations? Well, the first thing to do would be to collect which requests are happening at all. Also, you&#8217;d need to attach a sensible, displayable title to the raw request. For the sake of this little demonstration, I&#8217;m going to assume that in your template &#8212; <code>list.rhtml<\/code>, <code>edit.rhtml<\/code>, and brethren &#8212; you have an instance variable <code>@title<\/code>. It could be either defined in the template itself or transplanted there from its controller, it doesn&#8217;t matter.<\/p>\n<p>Given all this, you could hypothetically write code like this.<\/p>\n<p><code><\/p>\n<pre>\r\nclass ApplicationController < ActionController::Base\r\n  collect_popular_requests :max_age =&gt; 7.days, :max_count =&gt; 10000,\r\n    :title =&gt; proc { |controller| controller.response.template.instance_variable_get(:@title) }\r\n<\/pre>\n<p><\/code><\/p>\n<p>Therein further assuming that at most the 10000 most recent requests are kept if they are no older than 7 days. Now that we have hypothetically collected the data to base our tag cloud on, we have to display it somehow. Again, let's assume it could be done with a route like this<\/p>\n<p><code><\/p>\n<pre>\r\nmap.popular '\/', :controller => 'most_popular', :action => 'index'\r\n<\/pre>\n<p><\/code><\/p>\n<p>a controller like that<\/p>\n<p><code><\/p>\n<pre>\r\nclass MostPopularController < ApplicationController\r\n  helper BoilerPlate::PopularRequestsHelper\r\n  collect_popular_requests :\\off # Ignore this place\r\n\r\n  def index\r\n    @popular_requests = popular_requests_list.most_popular(30)\r\n  end\r\nend\r\n<\/pre>\n<p><\/code><\/p>\n<p>and for good measure a view (<code>index.rhtml<\/code>)<\/p>\n<p><code><\/p>\n<pre>\r\n&lt;ul class=\"tagcloud\"&gt;\r\n&lt;% alpha_reqs = @popular_requests.sort_by(&:title) -%&gt;\r\n&lt;% alpha_reqs.each do |req| -%&gt;\r\n  &lt;%= popular_request_tag(req, :wrap =&gt; 'li', :min_size =&gt; 30, :max_size =&gt; 400) %&gt;\r\n&lt;% end -%&gt;\r\n&lt;\/ul&gt;\r\n<\/pre>\n<p><\/code><\/p>\n<p>and some CSS for good style<\/p>\n<p><code><\/p>\n<pre>\r\n.tagcloud {\r\n  text-align: center;\r\n  width: 70%;\r\n}\r\n.tagcloud li {\r\n  display: inline-block;\r\n  display: -moz-inline-box;\r\n  white-space: nowrap;\r\n  vertical-align: middle;\r\n  line-height: 1.2em;\r\n  padding: 0 0.2em;\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n<p>Well, if you've made it this far, you might even consider doing all of this for real. For a good start, I suggest you download <a href=\"http:\/\/schuerig.de\/michael\/blog\/wp-content\/uploads\/2007\/02\/popular_requests.tar.gz\">this Rails plugin<\/a>, install it, create a database table with<\/p>\n<p><code><\/p>\n<pre>\r\n$ script\/generate popular_requests AddPopularRequests\r\n$ rake db:migrate\r\n<\/pre>\n<p><\/code><\/p>\n<p>and start playing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It was late last night. And the night before, I admit it. I also admit that I don&#8217;t know much about what Web 2.0 is all about, but there are two things that I do know: rounded corners are involved &hellip; <a href=\"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/2007\/02\/14\/popular-requests\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-rails","category-ruby"],"_links":{"self":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":1,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":145,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/145"}],"wp:attachment":[{"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.schuerig.de\/michael\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}