Permalink
Browse files

Added taggings

  • Loading branch information...
1 parent c838887 commit c2a77595ea5e70576d78852ef24f1a6397b93fa1 @jimweirich committed Mar 8, 2012
@@ -2,6 +2,11 @@ class ArticlesController < ApplicationController
def index
@articles = Article.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @articles }
+ end
end
def show
View
@@ -1,8 +1,23 @@
class Article < ActiveRecord::Base
has_many :comments, :dependent => :destroy
+ has_many :taggings, :dependent => :destroy
+ has_many :tags, :through => :taggings
validates_presence_of :title
validates_presence_of :body
+ def tag_list
+ tags.map { |tag| tag.name }.join(", ")
+ end
+
+ def tag_list=(values)
+ taggings.clear
+ names = values.split(',').map { |name| name.strip }.uniq
+ names.each do |name|
+ tag = Tag.find_or_create_by_name(name)
+ taggings.build(tag: tag)
+ end
+ end
+
end
@@ -6,3 +6,6 @@ class Comment < ActiveRecord::Base
validates_presence_of :body
end
+
+
+
View
@@ -0,0 +1,8 @@
+class Tag < ActiveRecord::Base
+
+ has_many :taggings, :dependent => :destroy
+ has_many :articles, :through => :taggings
+
+ validates_presence_of :name
+
+end
@@ -0,0 +1,6 @@
+class Tagging < ActiveRecord::Base
+
+ belongs_to :article
+ belongs_to :tag
+
+end
@@ -10,6 +10,10 @@
<%= f.text_area :body %>
</p>
<p>
+ <%= f.label :tag_list %><br/>
+ <%= f.text_field :tag_list %>
+</p>
+<p>
<%= f.submit "Save" %>
</p>
<% end %>
@@ -1,6 +1,6 @@
<h1><%= @article.title %></h1>
-<p class="flash"><%= flash[:notice] %></p>
+<p>Tags: <%= @article.tag_list %></p>
<p><%= @article.body %></p>
@@ -0,0 +1,9 @@
+class CreateTags < ActiveRecord::Migration
+ def change
+ create_table :tags do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,10 @@
+class CreateTaggings < ActiveRecord::Migration
+ def change
+ create_table :taggings do |t|
+ t.integer :tag_id
+ t.integer :article_id
+
+ t.timestamps
+ end
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120307221354) do
+ActiveRecord::Schema.define(:version => 20120308170608) do
create_table "articles", :force => true do |t|
t.string "title"
@@ -28,4 +28,17 @@
t.datetime "updated_at", :null => false
end
+ create_table "taggings", :force => true do |t|
+ t.integer "tag_id"
+ t.integer "article_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "tags", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Tag do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Tagging do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit c2a7759

Please sign in to comment.