Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
added acts_as_taggable_redux
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Adams committed Sep 14, 2008
1 parent 11d1152 commit 03ef2e7
Show file tree
Hide file tree
Showing 55 changed files with 1,027 additions and 15 deletions.
15 changes: 15 additions & 0 deletions app/controllers/admin/tags_controller.rb
@@ -0,0 +1,15 @@
class Admin::TagsController < Admin::BaseController
before_filter :load_tag, :only => [:show]

protected
def load_tag
@tag = Tag.find(params[:id])
end

public
def index
end

def show
end
end
2 changes: 2 additions & 0 deletions app/helpers/admin_helper.rb
@@ -0,0 +1,2 @@
module AdminHelper
end
8 changes: 8 additions & 0 deletions app/helpers/tag_helper.rb
@@ -0,0 +1,8 @@
module TagHelper
def link_to_tagged_item tagged_item
case tagged_item
when Ansuz::JAdams::BlogPost
link_to tagged_item, admin_blog_post_path(tagged_item)
end
end
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Expand Up @@ -17,6 +17,9 @@
require 'digest/sha1'
class User < ActiveRecord::Base
include SavageBeast::UserInit
# acts_as_taggable_redux support
acts_as_tagger

# Virtual attribute for the unencrypted password
attr_accessor :password

Expand Down
2 changes: 2 additions & 0 deletions app/views/admin/tags/index.html.erb
@@ -0,0 +1,2 @@
<%= title "Tags" -%>
<%= tag_cloud -%>
4 changes: 4 additions & 0 deletions app/views/admin/tags/show.html.erb
@@ -0,0 +1,4 @@
<%= title "Showing Tag: #{@tag}" -%>
<% @tag.tagged.each do |tagged_item| -%>
<%= link_to_tagged_item tagged_item -%>
<% end -%>
1 change: 1 addition & 0 deletions app/views/layouts/admin.html.erb
Expand Up @@ -15,6 +15,7 @@
<%= stylesheet_link_tag 'tree' %>
<%= stylesheet_link_tag 'jqmodal' %>
<%= stylesheet_link_tag 'flashes' %>
<%= stylesheet_link_tag 'acts_as_taggable_stylesheet' %>
<%= javascript_include_tag :defaults, 'jquery', 'jqModal', 'jquery.growl.js', 'ansuz/growls' %>
<%= javascript_include_tag 'show_and_hide' %>
<%= javascript_include_tag 'fckeditor/fckeditor' %>
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -19,6 +19,7 @@
<!--[if lt IE 7.]><%= javascript_include_tag 'pngfix.js' %><![endif]-->
</head>
<body>
<%= display_standard_flashes -%>
<div id='wrapper'>
<div id="main">
<div id='header'>
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/page.html.erb
Expand Up @@ -14,6 +14,7 @@
<%= stylesheet_link_tag 'base' -%>
<%= stylesheet_link_tag 'sprite' -%>
<%= stylesheet_link_tag 'lightbox' -%>
<%= stylesheet_link_tag 'acts_as_taggable_stylesheet' %>
<%= javascript_include_tag :defaults, 'effects', 'builder', 'lightbox', 'niftycube', 'handle_rounded_corners', 'jquery', 'jqModal', 'jquery.growl.js', 'ansuz/growls' -%>
<!--[if lt IE 7.]><%= javascript_include_tag 'pngfix.js' %><![endif]-->
</head>
Expand All @@ -32,7 +33,10 @@
<a href="http://github.com/knewter/ansuz/tree/master">you</a>.
</span>
</div>
<%= render :partial => "page/top_nav" -%>
<div id='sidebar'>
<%= render :partial => "page/top_nav" -%>
<%= yield :sidebar -%>
</div>
<div id='content'>
<%= render :partial => "page/breadcrumb" if (@page && (@page != @root)) -%>
<%= @content_for_layout -%>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -6,13 +6,15 @@
map.from_plugin :ansuz_user_manager

map.resources :users
map.resources :tags
map.namespace :admin do |admin|
admin.resources :pages, :member => [:shift_order]
admin.resources :page_plugins
admin.resources :plugins
admin.resource :account
admin.connect 'account/:action/:id', :controller => 'account'
admin.resource :site_settings
admin.resources :tags
end
map.connect '/admin', :controller => 'admin/pages'

Expand Down
29 changes: 29 additions & 0 deletions db/migrate/20080914031652_add_acts_as_taggable_tables.rb
@@ -0,0 +1,29 @@
class AddActsAsTaggableTables < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.column :name, :string
t.column :taggings_count, :integer, :default => 0, :null => false
end
add_index :tags, :name
add_index :tags, :taggings_count

create_table :taggings do |t|
t.column :tag_id, :integer
t.column :taggable_id, :integer
t.column :taggable_type, :string
t.column :user_id, :integer
end

# Find objects for a tag
add_index :taggings, [:tag_id, :taggable_type]
add_index :taggings, [:user_id, :tag_id, :taggable_type]
# Find tags for an object
add_index :taggings, [:taggable_id, :taggable_type]
add_index :taggings, [:user_id, :taggable_id, :taggable_type]
end

def self.down
drop_table :tags
drop_table :taggings
end
end
7 changes: 7 additions & 0 deletions public/stylesheets/acts_as_taggable_stylesheet.css
@@ -0,0 +1,7 @@
.hTagcloud .popularity { margin:0; padding:0; }
.hTagcloud .popularity li { display:inline; text-decoration:none; }
.hTagcloud .popularity .popular { font-size:1.0em; }
.hTagcloud .popularity .v-popular { font-size:1.2em; }
.hTagcloud .popularity .vv-popular { font-size:1.4em; }
.hTagcloud .popularity .vvv-popular { font-size:1.6em; }
.hTagcloud .popularity .vvvv-popular { font-size:1.8em; }
2 changes: 1 addition & 1 deletion public/stylesheets/base.css
Expand Up @@ -51,7 +51,7 @@ body{
background-color: #111;
margin-right: 10px;
}
#navigation{
#sidebar{
width: 200px;
float: left;
margin: 0 10px 0 0;
Expand Down
6 changes: 5 additions & 1 deletion themes/default-pink/layouts/page.html.erb
Expand Up @@ -14,6 +14,7 @@
<%= theme_stylesheet_link_tag 'base' -%>
<%= stylesheet_link_tag 'sprite' -%>
<%= stylesheet_link_tag 'lightbox' -%>
<%= stylesheet_link_tag 'acts_as_taggable_stylesheet' %>
<%= javascript_include_tag :defaults, 'effects', 'builder', 'lightbox', 'niftycube', 'handle_rounded_corners', 'jquery', 'jqModal', 'jquery.growl.js', 'ansuz/growls' -%>
<!--[if lt IE 7.]><%= javascript_include_tag 'pngfix.js' %><![endif]-->
</head>
Expand All @@ -32,7 +33,10 @@
<a href="http://github.com/knewter/ansuz/tree/master">you</a>.
</span>
</div>
<%= render :partial => "page/top_nav" -%>
<div id='sidebar'>
<%= render :partial => "page/top_nav" -%>
<%= yield :sidebar -%>
</div>
<div id='content'>
<%= render :partial => "page/breadcrumb" if (@page && (@page != @root)) -%>
<%= @content_for_layout -%>
Expand Down
2 changes: 1 addition & 1 deletion themes/default-pink/stylesheets/base.css
Expand Up @@ -52,7 +52,7 @@ body{
background-color: #111;
margin-right: 10px;
}
#navigation{
#sidebar{
width: 200px;
float: left;
margin: 0 10px 0 0;
Expand Down
5 changes: 4 additions & 1 deletion themes/default/layouts/page.html.erb
Expand Up @@ -32,7 +32,10 @@
<a href="http://github.com/knewter/ansuz/tree/master">you</a>.
</span>
</div>
<%= render :partial => "page/top_nav" -%>
<div id='sidebar'>
<%= render :partial => "page/top_nav" -%>
<%= yield :sidebar -%>
</div>
<div id='content'>
<%= render :partial => "page/breadcrumb" if (@page && (@page != @root)) -%>
<%= @content_for_layout -%>
Expand Down
2 changes: 1 addition & 1 deletion themes/default/stylesheets/base.css
Expand Up @@ -51,7 +51,7 @@ body{
background-color: #111;
margin-right: 10px;
}
#navigation{
#sidebar{
width: 200px;
float: left;
margin: 0 10px 0 0;
Expand Down
20 changes: 20 additions & 0 deletions vendor/plugins/acts_as_taggable_redux/MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2007 monki(Wesley Beary)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
143 changes: 143 additions & 0 deletions vendor/plugins/acts_as_taggable_redux/README
@@ -0,0 +1,143 @@
ActsAsTaggableRedux
===================

Allows for user owned tags to be added to multiple classes, and makes tags easier to work with.


Prerequisites
=============

Install Edge Rails before you get started so you get RESTful routing.

ActsAsTaggableRedux depends on database tables to store tagging information. Create the migration for these tables with this command:

rake acts_as_taggable:db:create

Then run the migration to create the tables with this command:

rake db:migrate

Also you will need to add this to your user model:
acts_as_tagger

OPTIONAL: The helper functions assume the pressence of a tags controller, that is what the tag clouds and tags will link to.

OPTIONAL: To pretty up tag clouds and lists you can generate an example stylesheet with this command:

rake acts_as_taggable:stylesheet:create

and then include this in your layouts that have tag clouds:

<%= stylesheet_link_tag 'acts_as_taggable_stylesheet' %>


Example
=======

The following is an example of how you might integrate tags with an Item model.

config/routes.rb
may.resource :items, :tags


app/views/items/new.erb
<h1>New Item</h1>

<% form_for(:item, @item) do |f| -%>

<%= error_message_for :item %>

<b>Tags:</b> <%= f.text_field :tag_list -%>

<%= submit_tag "Save" -%>

<% end -%>

if you want users to own taggings change the tags line to this
<b>Tags:</b> <%= f.text_field :tag_list, :value => @item.tag_list(user) -%>
and add this line beneath it
<%= f.hidden_field :user_id, :value => user.id -%>

app/views/items/show.erb
Item tagged with:
<% item.tags.each do |tag| -%>
<%= link_to_tag(tag) %>
<% end -%>

app/views/items/edit.erb
<h1>New Item</h1>

<% form_for(:item, @item, :html => { :method => :post }) do |f| -%>

<%= error_messages_for :item %>

<b>Tags:</b> <%= f.text_field :tag_list -%>

<%= submit_tag "Save" -%>

<% end -%>


app/controllers/items_controller.rb
class ItemController < ApplicationController
def new
@item = Item.new
end

def create
@item = Item.new(params[:item])

respond_to do |format|
if @item.save
flash[:notice] = 'Item was successfully created.'
format.html { redirect_to item_url(@item) }
format.xml { head :created, :location => item_url(@item) }
else
format.html { render :action => "new" }
format.xml { render :xml => @item.errors.to_xml }
end
end
end

def show
@item = Item.find(params[:id], :include => :tags)
end

def edit
@item = Item.find(params[:id])
end

def update
@item = Item.find(params[:id])

respond_to do |format|
if @item.update_attributes(params[:item])
flash[:notice] = 'Item was successfully updated.'
format.html { redirect_to item_url(@item) }
format.xml { head :updated, :location => item_url(@item) }
end
format.html { render :action => "edit"}
format.xml { render :xml => @item.errors.to_xml}
end
end
end



Tag clouds
==========

Tag clouds are created by a helper function, and depend on the counter cache to get fast accurate counts. To ensure this keeps working properly, don't add new tags to a taggable in any way other than using the tag.tag(taggable) style. This will ensure that the caches don't lose track. Also, see the prerequisites for installing the stylesheet so that the tag cloud actually looks like a tag cloud. Otherwise, just pop into a view that you want the tag cloud to appear and type this:

<%= tag_cloud %>



Contributing
============

Welcoming all pull requests on github.com at http://github.com/monki/acts_as_taggable_redux/tree/master or git://github.com/monki/acts_as_taggable_redux.git



Copyright (c) 2008 monki(Wesley Beary), released under the MIT license
22 changes: 22 additions & 0 deletions vendor/plugins/acts_as_taggable_redux/Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the acts_as_taggable_redux plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the acts_as_taggable_redux plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ActsAsTaggableRedux'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
@@ -0,0 +1,7 @@
class ActsAsTaggableStylesheetGenerator < Rails::Generator::Base
def manifest
record do |m|
m.file "acts_as_taggable_stylesheet.css", "public/stylesheets/acts_as_taggable_stylesheet.css"
end
end
end

0 comments on commit 03ef2e7

Please sign in to comment.