Permalink
Browse files

added categories

  • Loading branch information...
avarteq committed May 20, 2010
1 parent 5cf23c6 commit e8301df25d4af73e9f1af620c111f523d1933fa3
@@ -0,0 +1,85 @@
+class CategoriesController < ApplicationController
+ # GET /categories
+ # GET /categories.xml
+ def index
+ @categories = Category.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @categories }
+ end
+ end
+
+ # GET /categories/1
+ # GET /categories/1.xml
+ def show
+ @category = Category.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @category }
+ end
+ end
+
+ # GET /categories/new
+ # GET /categories/new.xml
+ def new
+ @category = Category.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @category }
+ end
+ end
+
+ # GET /categories/1/edit
+ def edit
+ @category = Category.find(params[:id])
+ end
+
+ # POST /categories
+ # POST /categories.xml
+ def create
+ @category = Category.new(params[:category])
+
+ respond_to do |format|
+ if @category.save
+ flash[:notice] = 'Category was successfully created.'
+ format.html { redirect_to(@category) }
+ format.xml { render :xml => @category, :status => :created, :location => @category }
+ else
+ format.html { render :action => "new" }
+ format.xml { render :xml => @category.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /categories/1
+ # PUT /categories/1.xml
+ def update
+ @category = Category.find(params[:id])
+
+ respond_to do |format|
+ if @category.update_attributes(params[:category])
+ flash[:notice] = 'Category was successfully updated.'
+ format.html { redirect_to(@category) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @category.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /categories/1
+ # DELETE /categories/1.xml
+ def destroy
+ @category = Category.find(params[:id])
+ @category.destroy
+
+ respond_to do |format|
+ format.html { redirect_to(categories_url) }
+ format.xml { head :ok }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module CategoriesHelper
+end
View
@@ -0,0 +1,3 @@
+class Category < ActiveRecord::Base
+ has_and_belongs_to_many :posts
+end
View
@@ -1,5 +1,6 @@
class Post < ActiveRecord::Base
has_many :comments
+ has_and_belongs_to_many :categories
def self.posts_and_comments
find(:all, :conditions => {:published => true}, :include => [:comments])
@@ -0,0 +1,20 @@
+<h1>Editing category</h1>
+
+<% form_for(@category) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_area :description %>
+ </p>
+ <p>
+ <%= f.submit 'Update' %>
+ </p>
+<% end %>
+
+<%= link_to 'Show', @category %> |
+<%= link_to 'Back', categories_path %>
@@ -0,0 +1,22 @@
+<h1>Listing categories</h1>
+
+<table>
+ <tr>
+ <th>Title</th>
+ <th>Description</th>
+ </tr>
+
+<% @categories.each do |category| %>
+ <tr>
+ <td><%=h category.title %></td>
+ <td><%=h category.description %></td>
+ <td><%= link_to 'Show', category %></td>
+ <td><%= link_to 'Edit', edit_category_path(category) %></td>
+ <td><%= link_to 'Destroy', category, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New category', new_category_path %>
@@ -0,0 +1,19 @@
+<h1>New category</h1>
+
+<% form_for(@category) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </p>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_area :description %>
+ </p>
+ <p>
+ <%= f.submit 'Create' %>
+ </p>
+<% end %>
+
+<%= link_to 'Back', categories_path %>
@@ -0,0 +1,13 @@
+<p>
+ <b>Title:</b>
+ <%=h @category.title %>
+</p>
+
+<p>
+ <b>Description:</b>
+ <%=h @category.description %>
+</p>
+
+
+<%= link_to 'Edit', edit_category_path(@category) %> |
+<%= link_to 'Back', categories_path %>
@@ -1,5 +1,3 @@
comment_id = post_comments_id(@post_id)
page.insert_html :bottom, comment_id, :partial => 'blog/comment', :locals => {:comment => @comment}
-page.select("##{comment_id} li").last.visual_effect :highlight
-
-# page.visual_effect :highlight, last_el
+page.select("##{comment_id} li").last.visual_effect :highlight
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+ <title>Categories: <%= controller.action_name %></title>
+ <%= stylesheet_link_tag 'scaffold' %>
+</head>
+<body>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
+<%= yield %>
+
+</body>
+</html>
View
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
+ map.resources :categories
+
map.resources :blog, :controller => :blog
map.resources :posts
map.resources :comments
@@ -0,0 +1,20 @@
+class CreateCategories < ActiveRecord::Migration
+ def self.up
+ create_table :categories do |t|
+ t.string :title
+ t.text :description
+
+ t.timestamps
+ end
+
+ create_table :categories_posts, :id => false do |t|
+ t.integer :category_id
+ t.integer :post_id
+ end
+ end
+
+ def self.down
+ drop_table :categories
+ drop_table :categories_posts
+ end
+end
@@ -0,0 +1,9 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ title: MyString
+ description: MyText
+
+two:
+ title: MyString
+ description: MyText
@@ -0,0 +1,45 @@
+require 'test_helper'
+
+class CategoriesControllerTest < ActionController::TestCase
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:categories)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create category" do
+ assert_difference('Category.count') do
+ post :create, :category => { }
+ end
+
+ assert_redirected_to category_path(assigns(:category))
+ end
+
+ test "should show category" do
+ get :show, :id => categories(:one).to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, :id => categories(:one).to_param
+ assert_response :success
+ end
+
+ test "should update category" do
+ put :update, :id => categories(:one).to_param, :category => { }
+ assert_redirected_to category_path(assigns(:category))
+ end
+
+ test "should destroy category" do
+ assert_difference('Category.count', -1) do
+ delete :destroy, :id => categories(:one).to_param
+ end
+
+ assert_redirected_to categories_path
+ end
+end
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class CategoryTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CategoriesHelperTest < ActionView::TestCase
+end

0 comments on commit e8301df

Please sign in to comment.