diff --git a/app/controllers/discussions_controller.rb b/app/controllers/discussions_controller.rb new file mode 100644 index 0000000..b772c94 --- /dev/null +++ b/app/controllers/discussions_controller.rb @@ -0,0 +1,79 @@ +class DiscussionsController < ApplicationController + # GET /discussions + # GET /discussions.xml + def index + @discussions = Discussion.find(:all) + + respond_to do |format| + format.html # index.rhtml + format.xml { render :xml => @discussions.to_xml } + end + end + + # GET /discussions/1 + # GET /discussions/1.xml + def show + @discussion = Discussion.find(params[:id]) + + respond_to do |format| + format.html # show.rhtml + format.xml { render :xml => @discussion.to_xml } + end + end + + # GET /discussions/new + def new + @discussion = Discussion.new + end + + # GET /discussions/1;edit + def edit + @discussion = Discussion.find(params[:id]) + end + + # POST /discussions + # POST /discussions.xml + def create + @discussion = Discussion.new(params[:discussion]) + + respond_to do |format| + if @discussion.save + flash[:notice] = 'Discussion was successfully created.' + format.html { redirect_to discussion_url(@discussion) } + format.xml { head :created, :location => discussion_url(@discussion) } + else + format.html { render :action => "new" } + format.xml { render :xml => @discussion.errors.to_xml } + end + end + end + + # PUT /discussions/1 + # PUT /discussions/1.xml + def update + @discussion = Discussion.find(params[:id]) + + respond_to do |format| + if @discussion.update_attributes(params[:discussion]) + flash[:notice] = 'Discussion was successfully updated.' + format.html { redirect_to discussion_url(@discussion) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @discussion.errors.to_xml } + end + end + end + + # DELETE /discussions/1 + # DELETE /discussions/1.xml + def destroy + @discussion = Discussion.find(params[:id]) + @discussion.destroy + + respond_to do |format| + format.html { redirect_to discussions_url } + format.xml { head :ok } + end + end +end diff --git a/app/helpers/discussions_helper.rb b/app/helpers/discussions_helper.rb new file mode 100644 index 0000000..46c02d0 --- /dev/null +++ b/app/helpers/discussions_helper.rb @@ -0,0 +1,2 @@ +module DiscussionsHelper +end diff --git a/app/models/discussion.rb b/app/models/discussion.rb new file mode 100644 index 0000000..52513ad --- /dev/null +++ b/app/models/discussion.rb @@ -0,0 +1,2 @@ +class Discussion < ActiveRecord::Base +end diff --git a/app/views/discussions/edit.rhtml b/app/views/discussions/edit.rhtml new file mode 100644 index 0000000..60358f6 --- /dev/null +++ b/app/views/discussions/edit.rhtml @@ -0,0 +1,12 @@ +
+ <%= submit_tag "Update" %> +
+<% end %> + +<%= link_to 'Show', discussion_path(@discussion) %> | +<%= link_to 'Back', discussions_path %> \ No newline at end of file diff --git a/app/views/discussions/index.rhtml b/app/views/discussions/index.rhtml new file mode 100644 index 0000000..96ec4ad --- /dev/null +++ b/app/views/discussions/index.rhtml @@ -0,0 +1,18 @@ +<%= link_to 'Show', discussion_path(discussion) %> | +<%= link_to 'Edit', edit_discussion_path(discussion) %> | +<%= link_to 'Destroy', discussion_path(discussion), :confirm => 'Are you sure?', :method => :delete %> | +
+ <%= submit_tag "Create" %> +
+<% end %> + +<%= link_to 'Back', discussions_path %> \ No newline at end of file diff --git a/app/views/discussions/show.rhtml b/app/views/discussions/show.rhtml new file mode 100644 index 0000000..c2862c1 --- /dev/null +++ b/app/views/discussions/show.rhtml @@ -0,0 +1,3 @@ + +<%= link_to 'Edit', edit_discussion_path(@discussion) %> | +<%= link_to 'Back', discussions_path %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d00b5f5..1bca0e7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ ActionController::Routing::Routes.draw do |map| - map.resources :beers, :breweries, :pages + map.resources :beers, :breweries, :pages, :discussions map.connect ':controller/:action/:id.:format' map.connect ':controller/:action/:id' diff --git a/db/migrate/006_create_discussions.rb b/db/migrate/006_create_discussions.rb new file mode 100644 index 0000000..1d1fc9c --- /dev/null +++ b/db/migrate/006_create_discussions.rb @@ -0,0 +1,12 @@ +class CreateDiscussions < ActiveRecord::Migration + def self.up + create_table :discussions do |t| + t.column :page_id, :integer + t.column :text, :text + end + end + + def self.down + drop_table :discussions + end +end diff --git a/test/fixtures/discussions.yml b/test/fixtures/discussions.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/discussions.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 +two: + id: 2 diff --git a/test/functional/discussions_controller_test.rb b/test/functional/discussions_controller_test.rb new file mode 100644 index 0000000..799ad6b --- /dev/null +++ b/test/functional/discussions_controller_test.rb @@ -0,0 +1,57 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'discussions_controller' + +# Re-raise errors caught by the controller. +class DiscussionsController; def rescue_action(e) raise e end; end + +class DiscussionsControllerTest < Test::Unit::TestCase + fixtures :discussions + + def setup + @controller = DiscussionsController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_should_get_index + get :index + assert_response :success + assert assigns(:discussions) + end + + def test_should_get_new + get :new + assert_response :success + end + + def test_should_create_discussion + old_count = Discussion.count + post :create, :discussion => { } + assert_equal old_count+1, Discussion.count + + assert_redirected_to discussion_path(assigns(:discussion)) + end + + def test_should_show_discussion + get :show, :id => 1 + assert_response :success + end + + def test_should_get_edit + get :edit, :id => 1 + assert_response :success + end + + def test_should_update_discussion + put :update, :id => 1, :discussion => { } + assert_redirected_to discussion_path(assigns(:discussion)) + end + + def test_should_destroy_discussion + old_count = Discussion.count + delete :destroy, :id => 1 + assert_equal old_count-1, Discussion.count + + assert_redirected_to discussions_path + end +end diff --git a/test/unit/discussion_test.rb b/test/unit/discussion_test.rb new file mode 100644 index 0000000..15d6c1f --- /dev/null +++ b/test/unit/discussion_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class DiscussionTest < Test::Unit::TestCase + fixtures :discussions + + # Replace this with your real tests. + def test_truth + assert true + end +end