Permalink
Browse files

adding tag support, rss, updating about

  • Loading branch information...
1 parent 3875ea9 commit a2ff33a0bb799dbe0623a054f9ec9f99e1b9b65a @actsasflinn committed Sep 4, 2008
@@ -0,0 +1,47 @@
+class TaggingsController < ApplicationController
+ # POST /taggings
+ # POST /taggings.xml
+ def create
+ @tagging = Tagging.new(params[:tagging])
+ @tagging.tag = Tag.find_or_create_by_name(params[:name])
+
+ respond_to do |format|
+ if @tagging.save
+ @tagging.tag.taggings_count += 1 # counter cache column doesn't get updated with filter
+ flash[:notice] = 'Tagging was successfully created.'
+ format.html { redirect_to(@tagging) }
+ format.xml { render :xml => @tagging, :status => :created, :location => @tagging }
+ format.json { render :json => @tagging, :callback => params[:callback] }
+ format.js do
+ render :update do |page|
+ page.insert_html(:bottom, :taggings, :partial => "tagging")
+ page[@tagging].visual_effect(:highlight, :duration => 5.0)
+ page[:taggings_count].replace_html pluralize(@tagging.snippet.taggings.count, 'Tags')
+ end
+ end
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @tagging.errors, :status => :unprocessable_entity }
+ format.json { render :json => @tagging.errors, :callback => params[:callback] }
+ format.js { render(:update){ |page| page[:new_tagging].visual_effect(:shake) } }
+ end
+ end
+ end
+
+ # DELETE /taggings/1
+ # DELETE /taggings/1.xml
+ def destroy
+ @tagging = Tagging.find(params[:id])
+ @tagging.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(taggings_url) }
+ format.xml { head :ok }
+ format.json { render :json => @tagging, :callback => params[:callback] }
+ format.js { render(:update){ |page|
+ page[@tagging].visual_effect(:fade)
+ page[:taggings_count].replace_html pluralize(@tagging.snippet.taggings.count, 'Tags')
+ } }
+ end
+ end
+end
@@ -0,0 +1,13 @@
+class TagsController < ApplicationController
+ # GET /tags
+ # GET /tags.xml
+ def index
+ @tags = Tag.paginate(:page => params[:page], :order => "taggings_count desc")
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @tags }
+ format.json { render :json => @tags, :callback => params[:callback] }
+ end
+ end
+end
@@ -8,4 +8,8 @@ def line_numbers(count, lines = [])
count.times{ |i| lines << content_tag(:span, i + 1) }
lines.join(tag(:br))
end
+
+ def tag_score_class(score)
+ "tag_#{score}"
+ end
end
@@ -0,0 +1,2 @@
+module TaggingsHelper
+end
@@ -0,0 +1,2 @@
+module TagsHelper
+end
@@ -10,18 +10,22 @@ class Snippet < ActiveRecord::Base
# Plugins
define_index do
indexes :body
- indexes language.name, :as => :name
+ indexes language.name, :as => :language
+ indexes tags.name, :as => :tag
end
# Relationships
belongs_to :language, :counter_cache => true
+ has_many :taggings
+ has_many :tags, :through => :taggings
# Validations
validates_length_of :body, :minimum => 1
validates_presence_of :language_id
# Scopes
named_scope :language, lambda{ |language_id| { :conditions => language_id.blank? ? nil : { :language_id => language_id } } }
+ named_scope :tag, lambda{ |tag_id| { :conditions => tag_id.blank? ? nil : { "taggings.tag_id" => tag_id } } }
# Callbacks
before_save :format_body
View
@@ -0,0 +1,31 @@
+class Tag < ActiveRecord::Base
+ include ActionView::Helpers::SanitizeHelper
+
+ # Plugins
+ define_index do
+ indexes :name
+ end
+
+ # Relationships
+ has_many :taggings
+ has_many :snippets, :through => :taggings
+
+ # Validations
+ validates_uniqueness_of :name
+
+ # Callbacks
+ before_save :strip_tags_name
+
+ # Scopes
+ named_scope :without_snippet_id, lambda{ |snippet_id| { :conditions => ["taggings.snippet_id != ? OR taggings_count = 0", snippet_id], :include => :taggings } }
+ named_scope :with_snippets, :conditions => "taggings_count > 0"
+
+ def strip_tags_name
+ self.name = strip_tags(self.name)
+ end
+
+ def score
+ tag_score = self.taggings_count / 5 # base on 5
+ tag_score > 10 ? 10 : tag_score # limit the score to 10
+ end
+end
@@ -0,0 +1,8 @@
+class Tagging < ActiveRecord::Base
+ # Relationships
+ belongs_to :tag, :counter_cache => true
+ belongs_to :snippet, :counter_cache => true
+
+ # Validations
+ validates_uniqueness_of :tag_id, :scope => :snippet_id
+end
@@ -0,0 +1,20 @@
+# index.rss.builder
+xml.instruct! :xml, :version => "1.0"
+xml.rss :version => "2.0" do
+ xml.channel do
+ xml.title "Snippy"
+ xml.description "Code snippets"
+ xml.link snippets_path(:only_path => false)
+
+ for snippet in @snippets
+ xml.item do
+ xml.title snippet.preview.split("\n").first
+ xml.description snippet.formatted_preview
+ xml.pubDate snippet.created_at.to_s(:rfc822)
+ xml.link snippet_path(snippet)
+ xml.guid snippet_path(snippet)
+ end
+ end
+ end
+end
+
@@ -0,0 +1,5 @@
+<% remote_form_for(:tagging, :url => taggings_path, :html => { :id => 'new_tagging', :class => "clearfix" }) do |form| %>
+ <%= form.hidden_field :snippet_id, :value => snippet_id %>
+ <%= text_field_tag :name %>
+ <%= form.submit 'New' %>
+<% end %>
@@ -0,0 +1,5 @@
+<% div_for(tagging) do %>
+ <%= link_to_remote('-', :url => tagging_path(tagging), :method => :delete, :html => { :class => "remove" }) %>
+ <%= link_to(tagging.tag.name, tag_snippets_path(tagging.tag), :class => tag_score_class(tagging.tag.score)) %>
+ <%= total_for(tagging.tag.taggings_count) -%>
+<% end %>
@@ -0,0 +1,15 @@
+<div id="tags_page">
+ <span class="right"><%= will_paginate(@tags) -%></span>
+
+ <h1><%=h @tag.blank? ? 'All Tags' : @tag.name %>
+ <%= total_for(@tags.total_entries) -%></h1>
+
+ <div id="tags">
+ <% @tags.each do |tag| %>
+ <% div_for(tag) do %>
+ <%= link_to(tag.name, tag_snippets_path(tag)) -%>
+ <%= total_for(tag.taggings_count) -%>
+ <% end %>
+ <% end %>
+ </div>
+</div>
@@ -0,0 +1,14 @@
+class CreateTags < ActiveRecord::Migration
+ def self.up
+ create_table :tags do |t|
+ t.string :name
+ t.integer :taggings_count, :default => 0
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :tags
+ end
+end
@@ -0,0 +1,14 @@
+class CreateTaggings < ActiveRecord::Migration
+ def self.up
+ create_table :taggings do |t|
+ t.integer :tag_id
+ t.integer :snippet_id
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :taggings
+ end
+end
View
@@ -0,0 +1,47 @@
+# This file is auto-generated from the current state of the database. Instead of editing this file,
+# please use the migrations feature of Active Record to incrementally modify your database, and
+# then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your database schema. If you need
+# to create the application database on another system, you should be using db:schema:load, not running
+# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20080705043830) do
+
+ create_table "languages", :force => true do |t|
+ t.string "name"
+ t.string "slug"
+ t.integer "snippets_count", :limit => 11, :default => 0
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "snippets", :force => true do |t|
+ t.text "body"
+ t.text "formatted_body"
+ t.text "formatted_preview"
+ t.integer "language_id", :limit => 11
+ t.string "theme", :default => "clean"
+ t.integer "taggings_count", :limit => 11, :default => 0
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "taggings", :force => true do |t|
+ t.integer "tag_id", :limit => 11
+ t.integer "snippet_id", :limit => 11
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "tags", :force => true do |t|
+ t.string "name"
+ t.integer "taggings_count", :limit => 11, :default => 0
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end

0 comments on commit a2ff33a

Please sign in to comment.