From 070aa7b3e55cce9726bf578218fbd33ff752dc6d Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 3 Dec 2007 08:47:21 +0000 Subject: [PATCH] mostly implemented users git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@51 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7 --- app/controllers/peoples_controller.rb | 94 ++++++++++++++++++++++ app/helpers/peoples_helper.rb | 15 ++++ app/models/people.rb | 6 ++ app/views/layouts/application.rhtml | 1 + app/views/peoples/edit.rhtml | 10 +++ app/views/peoples/index.rhtml | 14 ++++ app/views/peoples/new.rhtml | 11 +++ app/views/peoples/show.rhtml | 7 ++ config/routes.rb | 2 + db/migrate/007_create_peoples.rb | 11 +++ test/fixtures/pages.yml | 5 ++ test/fixtures/peoples.yml | 4 + test/functional/peoples_controller_test.rb | 58 +++++++++++++ test/unit/people_test.rb | 10 +++ 14 files changed, 248 insertions(+) create mode 100644 app/controllers/peoples_controller.rb create mode 100644 app/helpers/peoples_helper.rb create mode 100644 app/models/people.rb create mode 100644 app/views/peoples/edit.rhtml create mode 100644 app/views/peoples/index.rhtml create mode 100644 app/views/peoples/new.rhtml create mode 100644 app/views/peoples/show.rhtml create mode 100644 db/migrate/007_create_peoples.rb create mode 100644 test/fixtures/peoples.yml create mode 100644 test/functional/peoples_controller_test.rb create mode 100644 test/unit/people_test.rb diff --git a/app/controllers/peoples_controller.rb b/app/controllers/peoples_controller.rb new file mode 100644 index 0000000..cb37c4b --- /dev/null +++ b/app/controllers/peoples_controller.rb @@ -0,0 +1,94 @@ +class PeoplesController < ApplicationController + append_before_filter :fetch_people_and_page, :only => [ :show, :edit, + :update, :destroy ] + + # GET /peoples + # GET /peoples.xml + def index + @secondary_title = 'Browsing all Peoples' + @pages, @peoples = paginate :people, :per_page => 25, :order => 'title ASC', + :singular_name => 'people' + @tags = Page.tags(:limit => 25, :order => "name DESC", + :owner_type => 'People') + respond_to do |format| + format.html # index.rhtml + format.xml { render :xml => @people.to_xml } + end + end + + # GET /peoples/1 + # GET /peoples/1.xml + def show + @secondary_title = 'Detailed Information' + respond_to do |format| + format.html # show.rhtml + format.xml { render :xml => @people.to_xml } + end + end + + # GET /peoples/new + def new + @secondary_title = 'Sign up for BarleySodas!' + @people = People.new + @page = Page.new + end + + # GET /peoples/1;edit + def edit + end + + # POST /peoples + # POST /peoples.xml + def create + @people = People.new(params[:people]) + @page = Page.new(params[:page]) + @people.page = @page + respond_to do |format| + if @people.save + flash[:notice] = 'People was successfully created.' + format.html { redirect_to people_url(@people.page.title_for_url) } + format.xml { head :created, + :location => people_url(@people.page.title_for_url) } + else + format.html { render :action => "new" } + format.xml { render :xml => @people.errors.to_xml } + end + end + end + + # PUT /peoples/1 + # PUT /peoples/1.xml + def update + @people.attributes = params[:people] + @page.attributes = params[:page] + respond_to do |format| + if @people.update_attributes(params[:people]) + flash[:notice] = 'People was successfully updated.' + format.html { redirect_to people_url(@people.page.title_for_url) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @people.errors.to_xml } + end + end + end + + # DELETE /peoples/1 + # DELETE /peoples/1.xml + def destroy + @people.destroy + respond_to do |format| + format.html { redirect_to peoples_url } + format.xml { head :ok } + end + end + + protected + + def fetch_people_and_page + @people = People.find_by_title(Page.title_from_url(params[:id]), + :include => [ 'page' ]) + raise ActiveRecord::RecordNotFound.new if @people.nil? + @page = @people.page + end +end diff --git a/app/helpers/peoples_helper.rb b/app/helpers/peoples_helper.rb new file mode 100644 index 0000000..574fe22 --- /dev/null +++ b/app/helpers/peoples_helper.rb @@ -0,0 +1,15 @@ +module PeoplesHelper + def new_people_link + link_to 'Sign Up!', new_people_path, { :title => 'Sign Up!' } + end + + def show_people_link(people) + link_to people.title, people_path(people.page.title_for_url), + { :title => people.title } + end + + def edit_people_link(people) + link_to 'Edit People', edit_people_path(people.page.title_for_url), + { :title => "Edit #{people.title}" } + end +end diff --git a/app/models/people.rb b/app/models/people.rb new file mode 100644 index 0000000..cc1e6e8 --- /dev/null +++ b/app/models/people.rb @@ -0,0 +1,6 @@ +## +# This model represents a user in the system. +# +class People < ActiveRecord::Base + has_one_tuxwiki_page :owner_class => 'People' +end diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml index e7bd882..160031c 100644 --- a/app/views/layouts/application.rhtml +++ b/app/views/layouts/application.rhtml @@ -32,6 +32,7 @@ <%= link_to_unless_current 'Browse Beers', beers_path -%>
<%= link_to_unless_current 'Browse Breweries', breweries_path -%>
<%= link_to_unless_current 'Discussions', discussions_path -%>
+ <%= link_to_unless_current 'Peoples', peoples_path -%>

<%= yield :sidebar %> diff --git a/app/views/peoples/edit.rhtml b/app/views/peoples/edit.rhtml new file mode 100644 index 0000000..e7af0a6 --- /dev/null +++ b/app/views/peoples/edit.rhtml @@ -0,0 +1,10 @@ +<%= error_messages_for :people %> + +<% form_for(:people, :url => people_path(@people), :html => { :method => :put }) do |f| %> +

+ <%= submit_tag "Update" %> +

+<% end %> + +<%= link_to 'Show', people_path(@people) %> | +<%= link_to 'Back', peoples_path %> \ No newline at end of file diff --git a/app/views/peoples/index.rhtml b/app/views/peoples/index.rhtml new file mode 100644 index 0000000..d1c5b9f --- /dev/null +++ b/app/views/peoples/index.rhtml @@ -0,0 +1,14 @@ + + +<%= render :partial => 'shared/pagination_links' %> + +<% content_for :sidebar do -%> + <%= link_to 'Create a new person', new_people_path %>
+ <%= render :partial => 'shared/tag_cloud' %> +<% end -%> \ No newline at end of file diff --git a/app/views/peoples/new.rhtml b/app/views/peoples/new.rhtml new file mode 100644 index 0000000..0b2b07b --- /dev/null +++ b/app/views/peoples/new.rhtml @@ -0,0 +1,11 @@ +

New people

+ +<%= error_messages_for :people %> + +<% form_for(:people, :url => peoples_path) do |f| %> +

+ <%= submit_tag "Create" %> +

+<% end %> + +<%= link_to 'Back', peoples_path %> \ No newline at end of file diff --git a/app/views/peoples/show.rhtml b/app/views/peoples/show.rhtml new file mode 100644 index 0000000..01a4ca2 --- /dev/null +++ b/app/views/peoples/show.rhtml @@ -0,0 +1,7 @@ +<%= render :partial => 'pages/page' %> + +<% content_for :sidebar do -%> + <%= new_people_link -%>
+ <%= edit_people_link(@people) %>
+ <%= link_to 'Destroy', people_path(@people.page.title_for_url), :confirm => 'Are you sure?', :method => :delete %>
+<% end -%> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1bca0e7..0d30358 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw do |map| + map.resources :peoples + map.resources :beers, :breweries, :pages, :discussions map.connect ':controller/:action/:id.:format' diff --git a/db/migrate/007_create_peoples.rb b/db/migrate/007_create_peoples.rb new file mode 100644 index 0000000..5593e85 --- /dev/null +++ b/db/migrate/007_create_peoples.rb @@ -0,0 +1,11 @@ +class CreatePeoples < ActiveRecord::Migration + def self.up + create_table :peoples do |t| + t.column :title, :string + end + end + + def self.down + drop_table :peoples + end +end diff --git a/test/fixtures/pages.yml b/test/fixtures/pages.yml index 353add0..945b6bc 100644 --- a/test/fixtures/pages.yml +++ b/test/fixtures/pages.yml @@ -13,3 +13,8 @@ homepage: id: 3 title: HomePage redcloth: Homepage Text +personal_description: + id: 4 + title: penguin coder + owner_id: 1 + owner_type: People diff --git a/test/fixtures/peoples.yml b/test/fixtures/peoples.yml new file mode 100644 index 0000000..7b2da6c --- /dev/null +++ b/test/fixtures/peoples.yml @@ -0,0 +1,4 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 + title: penguin coder diff --git a/test/functional/peoples_controller_test.rb b/test/functional/peoples_controller_test.rb new file mode 100644 index 0000000..0cf8f0f --- /dev/null +++ b/test/functional/peoples_controller_test.rb @@ -0,0 +1,58 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'peoples_controller' + +# Re-raise errors caught by the controller. +class PeoplesController; def rescue_action(e) raise e end; end + +class PeoplesControllerTest < Test::Unit::TestCase + fixtures :peoples + + def setup + @controller = PeoplesController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_should_get_index + get :index + assert_response :success + assert assigns(:peoples) + end + + def test_should_get_new + get :new + assert_response :success + end + + def test_should_create_people + old_count = People.count + post :create, :people => { :title => '1' } + assert_equal old_count+1, People.count + + assert_redirected_to people_path(assigns(:people).page.title_for_url) + end + + def test_should_show_people + get :show, :id => 'penguin_coder' + assert_response :success + end + + def test_should_get_edit + get :edit, :id => 'penguin_coder' + assert_response :success + end + + def test_should_update_people + put :update, :id => 'penguin_coder', + :people => { :title => 'penguin coder' } + assert_redirected_to people_path(assigns(:people).page.title_for_url) + end + + def test_should_destroy_people + old_count = People.count + delete :destroy, :id => 'penguin_coder' + assert_equal old_count-1, People.count + + assert_redirected_to peoples_path + end +end diff --git a/test/unit/people_test.rb b/test/unit/people_test.rb new file mode 100644 index 0000000..c852633 --- /dev/null +++ b/test/unit/people_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class PeopleTest < Test::Unit::TestCase + fixtures :peoples + + # Replace this with your real tests. + def test_truth + assert true + end +end