Commit f5686b5e

mo k <mo@mokhan.ca>
2011-08-08 03:41:08
start building category listings.
1 parent 32d83ed
app/controllers/categories_controller.rb
@@ -0,0 +1,83 @@
+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
+        format.html { redirect_to(@category, :notice => 'Category was successfully created.') }
+        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])
+        format.html { redirect_to(@category, :notice => 'Category was successfully updated.') }
+        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
app/helpers/categories_helper.rb
@@ -0,0 +1,2 @@
+module CategoriesHelper
+end
app/models/category.rb
@@ -1,3 +1,4 @@
 class Category < ActiveRecord::Base
   has_and_belongs_to_many :creations, :join_table => 'creations_categories'
+  attr_accessor :name, :slug
 end
app/views/categories/_form.html.erb
@@ -0,0 +1,25 @@
+<%= form_for(@category) do |f| %>
+  <% if @category.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
+
+      <ul>
+      <% @category.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :name %><br />
+    <%= f.text_field :name %>
+  </div>
+  <div class="field">
+    <%= f.label :slug %><br />
+    <%= f.text_field :slug %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
app/views/categories/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing category</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @category %> |
+<%= link_to 'Back', categories_path %>
app/views/categories/index.html.erb
@@ -0,0 +1,25 @@
+<h1>Listing categories</h1>
+
+<table>
+  <tr>
+    <th>Name</th>
+    <th>Slug</th>
+    <th></th>
+    <th></th>
+    <th></th>
+  </tr>
+
+<% @categories.each do |category| %>
+  <tr>
+    <td><%= category.name %></td>
+    <td><%= category.slug %></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 %>
app/views/categories/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New category</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', categories_path %>
app/views/categories/show.html.erb
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <b>Name:</b>
+  <%= @category.name %>
+</p>
+
+<p>
+  <b>Slug:</b>
+  <%= @category.slug %>
+</p>
+
+
+<%= link_to 'Edit', edit_category_path(@category) %> |
+<%= link_to 'Back', categories_path %>
config/routes.rb
@@ -1,11 +1,11 @@
 Cake::Application.routes.draw do
-  get "search/index"
-
   resources :creations
   resources :authentications
+  resources :categories
   get "home/index"
   get "profiles/index"
   get "profiles/show"
+  get "search/index"
 
   devise_for :users, :controllers => {:registrations => 'registrations'}
   match '/auth/:provider/callback' => 'authentications#create'
@@ -13,68 +13,5 @@ Cake::Application.routes.draw do
   match 'artists' => 'profiles#index', :as => 'all_profiles', :method => 'GET'
   match 'artists/:id' => 'profiles#show', :as => 'profile', :method => 'GET'
   match 'profiles/show/:id' => 'profiles#show', :as => 'profile', :method => 'GET'
-
-  # authenticate :user do
-  #   root :to => "home#index"
-  # end
-  # root :to => "devise:sessions#new"
   root :to => "home#index"
-
-  # The priority is based upon order of creation:
-  # first created -> highest priority.
-
-  # Sample of regular route:
-  #   match 'products/:id' => 'catalog#view'
-  # Keep in mind you can assign values other than :controller and :action
-
-  # Sample of named route:
-  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
-  # This route can be invoked with purchase_url(:id => product.id)
-
-  # Sample resource route (maps HTTP verbs to controller actions automatically):
-  #   resources :products
-
-  # Sample resource route with options:
-  #   resources :products do
-  #     member do
-  #       get 'short'
-  #       post 'toggle'
-  #     end
-  #
-  #     collection do
-  #       get 'sold'
-  #     end
-  #   end
-
-  # Sample resource route with sub-resources:
-  #   resources :products do
-  #     resources :comments, :sales
-  #     resource :seller
-  #   end
-
-  # Sample resource route with more complex sub-resources
-  #   resources :products do
-  #     resources :comments
-  #     resources :sales do
-  #       get 'recent', :on => :collection
-  #     end
-  #   end
-
-  # Sample resource route within a namespace:
-  #   namespace :admin do
-  #     # Directs /admin/products/* to Admin::ProductsController
-  #     # (app/controllers/admin/products_controller.rb)
-  #     resources :products
-  #   end
-
-  # You can have the root of your site routed with "root"
-  # just remember to delete public/index.html.
-  #root :to => "home#index"
-
-  # See how all your routes lay out with "rake routes"
-
-  # This is a legacy wild controller route that's not recommended for RESTful applications.
-  # Note: This route will make all actions in every controller accessible via GET requests.
-  # match ':controller(/:action(/:id(.:format)))'
-  #
 end
spec/controllers/categories_controller_spec.rb
@@ -0,0 +1,157 @@
+require 'spec_helper'
+
+# This spec was generated by rspec-rails when you ran the scaffold generator.
+# It demonstrates how one might use RSpec to specify the controller code that
+# was generated by Rails when you ran the scaffold generator.
+#
+# It assumes that the implementation code is generated by the rails scaffold
+# generator.  If you are using any extension libraries to generate different
+# controller code, this generated spec may or may not pass.
+#
+# It only uses APIs available in rails and/or rspec-rails.  There are a number
+# of tools you can use to make these specs even more expressive, but we're
+# sticking to rails and rspec-rails APIs to keep things simple and stable.
+#
+# Compared to earlier versions of this generator, there is very limited use of
+# stubs and message expectations in this spec.  Stubs are only used when there
+# is no simpler way to get a handle on the object needed for the example.
+# Message expectations are only used when there is no simpler way to specify
+# that an instance is receiving a specific message.
+
+describe CategoriesController do
+
+  # This should return the minimal set of attributes required to create a valid
+  # Category. As you add validations to Category, be sure to
+  # update the return value of this method accordingly.
+  def valid_attributes
+    {}
+  end
+
+  describe "GET index" do
+    it "assigns all categories as @categories" do
+      category = Category.create! valid_attributes
+      get :index
+      assigns(:categories).should eq([category])
+    end
+  end
+
+  describe "GET show" do
+    it "assigns the requested category as @category" do
+      category = Category.create! valid_attributes
+      get :show, :id => category.id.to_s
+      assigns(:category).should eq(category)
+    end
+  end
+
+  describe "GET new" do
+    it "assigns a new category as @category" do
+      get :new
+      assigns(:category).should be_a_new(Category)
+    end
+  end
+
+  describe "GET edit" do
+    it "assigns the requested category as @category" do
+      category = Category.create! valid_attributes
+      get :edit, :id => category.id.to_s
+      assigns(:category).should eq(category)
+    end
+  end
+
+  describe "POST create" do
+    describe "with valid params" do
+      it "creates a new Category" do
+        expect {
+          post :create, :category => valid_attributes
+        }.to change(Category, :count).by(1)
+      end
+
+      it "assigns a newly created category as @category" do
+        post :create, :category => valid_attributes
+        assigns(:category).should be_a(Category)
+        assigns(:category).should be_persisted
+      end
+
+      it "redirects to the created category" do
+        post :create, :category => valid_attributes
+        response.should redirect_to(Category.last)
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns a newly created but unsaved category as @category" do
+        # Trigger the behavior that occurs when invalid params are submitted
+        Category.any_instance.stub(:save).and_return(false)
+        post :create, :category => {}
+        assigns(:category).should be_a_new(Category)
+      end
+
+      it "re-renders the 'new' template" do
+        # Trigger the behavior that occurs when invalid params are submitted
+        Category.any_instance.stub(:save).and_return(false)
+        post :create, :category => {}
+        response.should render_template("new")
+      end
+    end
+  end
+
+  describe "PUT update" do
+    describe "with valid params" do
+      it "updates the requested category" do
+        category = Category.create! valid_attributes
+        # Assuming there are no other categories in the database, this
+        # specifies that the Category created on the previous line
+        # receives the :update_attributes message with whatever params are
+        # submitted in the request.
+        Category.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
+        put :update, :id => category.id, :category => {'these' => 'params'}
+      end
+
+      it "assigns the requested category as @category" do
+        category = Category.create! valid_attributes
+        put :update, :id => category.id, :category => valid_attributes
+        assigns(:category).should eq(category)
+      end
+
+      it "redirects to the category" do
+        category = Category.create! valid_attributes
+        put :update, :id => category.id, :category => valid_attributes
+        response.should redirect_to(category)
+      end
+    end
+
+    describe "with invalid params" do
+      it "assigns the category as @category" do
+        category = Category.create! valid_attributes
+        # Trigger the behavior that occurs when invalid params are submitted
+        Category.any_instance.stub(:save).and_return(false)
+        put :update, :id => category.id.to_s, :category => {}
+        assigns(:category).should eq(category)
+      end
+
+      it "re-renders the 'edit' template" do
+        category = Category.create! valid_attributes
+        # Trigger the behavior that occurs when invalid params are submitted
+        Category.any_instance.stub(:save).and_return(false)
+        put :update, :id => category.id.to_s, :category => {}
+        response.should render_template("edit")
+      end
+    end
+  end
+
+  describe "DELETE destroy" do
+    it "destroys the requested category" do
+      category = Category.create! valid_attributes
+      expect {
+        delete :destroy, :id => category.id.to_s
+      }.to change(Category, :count).by(-1)
+    end
+
+    it "redirects to the categories list" do
+      category = Category.create! valid_attributes
+      delete :destroy, :id => category.id.to_s
+      response.should redirect_to(categories_url)
+    end
+  end
+
+end
spec/helpers/categories_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the CategoriesHelper. For example:
+#
+# describe CategoriesHelper do
+#   describe "string concat" do
+#     it "concats two strings with spaces" do
+#       helper.concat_strings("this","that").should == "this that"
+#     end
+#   end
+# end
+describe CategoriesHelper do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
spec/requests/categories_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe "Categories" do
+  describe "GET /categories" do
+    it "works! (now write some real specs)" do
+      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
+      get categories_path
+      response.status.should be(200)
+    end
+  end
+end
spec/routing/categories_routing_spec.rb
@@ -0,0 +1,35 @@
+require "spec_helper"
+
+describe CategoriesController do
+  describe "routing" do
+
+    it "routes to #index" do
+      get("/categories").should route_to("categories#index")
+    end
+
+    it "routes to #new" do
+      get("/categories/new").should route_to("categories#new")
+    end
+
+    it "routes to #show" do
+      get("/categories/1").should route_to("categories#show", :id => "1")
+    end
+
+    it "routes to #edit" do
+      get("/categories/1/edit").should route_to("categories#edit", :id => "1")
+    end
+
+    it "routes to #create" do
+      post("/categories").should route_to("categories#create")
+    end
+
+    it "routes to #update" do
+      put("/categories/1").should route_to("categories#update", :id => "1")
+    end
+
+    it "routes to #destroy" do
+      delete("/categories/1").should route_to("categories#destroy", :id => "1")
+    end
+
+  end
+end