From 2a8128356a1d98191a67662b73a0f97d2f430aea Mon Sep 17 00:00:00 2001 From: Gerald Bauer Date: Tue, 19 Jul 2016 19:30:49 +0200 Subject: [PATCH] use markdown --- tagging.md | 109 ++++++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 55 deletions(-) diff --git a/tagging.md b/tagging.md index 84d5bd3..2289524 100644 --- a/tagging.md +++ b/tagging.md @@ -71,7 +71,7 @@ Discover Examples [Semantic Web](http://en.wikipedia.org/wiki/Semantic_web) -h1. Add Tags To Your Web App +# Add Tags To Your Web App Step 0: Install Taggable Plug-In @@ -82,38 +82,38 @@ Step 2: Mark Your ActiveRecord Classes (Bookmark, Photo, Book etcetera) As Tagga Step 3: That's it. -h1. Step 1 - Add Database Tables (Tags, Taggings) +# Step 1 - Add Database Tables (Tags, Taggings) The Tags Table -{{{ +``` create_table :tags do |t| t.column :name, :string # The tag name end -}}} +``` The Taggings (Polymorphic Join) Table -{{{ +``` create_table :taggings do |t| t.column :tag_id, :integer # foreign key; id of the tag t.column :taggable_id, :integer # foreign key; id of the taggable object t.column :taggable_type, :string # taggable object type (bookmark, photo, book, etcetera) t.column :created_at, :datetime # timestamp end -}}} +``` -h1. Step 2 - Mark Your ActiveRecord Classes As Taggable +# Step 2 - Mark Your ActiveRecord Classes As Taggable -{{{ +``` class Photo < ActiveRecord::Base acts_as_taggable ... end -}}} +``` -h3. Methods Added To Your ActiveRecord Model Classes +### Methods Added To Your ActiveRecord Model Classes * tag_list * find_tagged_with( tags, options = {} ) @@ -121,44 +121,44 @@ h3. Methods Added To Your ActiveRecord Model Classes Get Tags: -{{{ +``` >> p = Photo.find(:first) >> p.tag_list.to_s => "wedding vancouver summer" -}}} +``` Store Tags: -{{{ +``` >> p = Photo.find(:first) >> p.tag_list = "wedding vanouver summer" >> p.save -}}} +``` Add or Remove Tags: -{{{ +``` >> p.tag_list.add( "fun" ) >> p.tag_list.remove( "wedding" ) -}}} +``` Find Photos Tagged With: -{{{ +``` >> photos = Photo.find_tagged_with( 'summer' ) -}}} +``` -Note: You can use options such as @:order@, @:limit@, @:offset@, etcetera +Note: You can use options such as `:order`, `:limit`, `:offset`, etcetera Get Tag Counts for Tag Cloud: -{{{ +``` >> tags = Photo.tag_counts -}}} +``` -h1. Add Tag Clouds To Your Web App +# Add Tag Clouds To Your Web App Step 1: Add the Tag Cloud Helper to your App @@ -167,11 +167,11 @@ Step 2: Write the Controllers and Views Step 3: That's it. -h1. Step 1: Add the Tag Cloud Helper to Your App +# Step 1: Add the Tag Cloud Helper to Your App -@public/stylesheets/cloud.css@: +`public/stylesheets/cloud.css`: -{{{ +``` .cloud1 {font-size: 100%;} .cloud2 {font-size: 120%;} .cloud3 {font-size: 140%;} @@ -179,11 +179,11 @@ h1. Step 1: Add the Tag Cloud Helper to Your App .cloud5 {font-size: 180%;} .cloud6 {font-size: 200%;} .cloud7 {font-size: 220%;} -}}} +``` -@app/helpers/application_helper.rb@: +`app/helpers/application_helper.rb`: -{{{ +``` def tag_cloud( tags ) classes = %w(cloud1 cloud2 cloud3 cloud4 cloud5 cloud6 cloud7) @@ -199,66 +199,65 @@ def tag_cloud( tags ) yield t.name, classes[(t.count.to_i - min) / divisor] } end -}}} +``` -h1. Step 2: Write the Tags Controller and View +# Step 2: Write the Tags Controller and View -@app/controllers/tags_controller.rb@: +`app/controllers/tags_controller.rb`: -{{{ +``` class TagsController < ApplicationController def index @tags = Photo.tag_counts( :order => "name" ) end end -}}} +``` -@app/views/tags/index.rhtml@: +`app/views/tags/index.rhtml`: -{{{ +``` <%% tag_cloud @tags do |name, css_class| %> <%%= link_to name, {:action => :tag, :id => name}, :class => css_class %> <%% end %> -}}} +``` -h1. Plugin Optimizations & Options +# Plugin Optimizations & Options Create Database Index for Taggings Table -{{{ +``` add_index :taggings, :tag_id add_index :taggings, [:taggable_id, :taggable_type] -}}} +``` Add Cached Tag List Table Column -{{{ +``` t.column :cached_tag_list, :string # tells plugin to cache taglist -}}} +``` Tag Seperator/Delimiter -{{{ +``` TagList.delimiter = " " # use space or comma -}}} +``` -h1. Rails Tagging Plugin Choices +# Rails Tagging Plugin Choices -* @acts_as_taggable@ Gem by Dirk Elmendorf -* @acts_as_taggable@ Plugin by David Heinemeier Hansson (Prototype - Not Production Ready) -* @acts_as_taggable_on_steroids@ Plugin by Jonathan Viney -* @acts_as_taggable_redux@ Plugin by Wesley Beary -* @scalable_acts_as_taggable@ Plugin by Matthew Carr -* @has_many_polymorphs@ Plugin +* `acts_as_taggable` Gem by Dirk Elmendorf +* `acts_as_taggable` Plugin by David Heinemeier Hansson (Prototype - Not Production Ready) +* `acts_as_taggable_on_steroids` Plugin by Jonathan Viney +* `acts_as_taggable_redux` Plugin by Wesley Beary +* `scalable_acts_as_taggable` Plugin by Matthew Carr +* `has_many_polymorphs` Plugin -h1. Free Online Articles and Chapters About Tagging -* Oracle Tech Net Article: "Tagging with Oracle and Ruby on Rails":http://www.oracle.com/technology/pub/articles/kern-rails-tagging.html by Matt Kern (June 2007) -* Practical Rails Social Networking Sites Book by Alan Bradburne (June 2007): Free Sample Chapter 10 - "Adding Tags to the Photo Gallery":http://www.apress.com/book/view/1590598415 +# Free Online Articles and Chapters About Tagging -h1. That's it - Thanks! +* Oracle Tech Net Article: [Tagging with Oracle and Ruby on Rails](http://www.oracle.com/technology/pub/articles/kern-rails-tagging.html) by Matt Kern (June 2007) +* Practical Rails Social Networking Sites Book by Alan Bradburne (June 2007): Free Sample Chapter 10 - [Adding Tags to the Photo Gallery](http://www.apress.com/book/view/1590598415) -{{ google_analytics :code => 'UA-397343-10' }} +# That's it - Thanks!