From d0e857b79153d594516b13c2d8c023497896d64f Mon Sep 17 00:00:00 2001 From: Federico Ramallo + Galen Sanford Date: Fri, 27 Jan 2012 21:19:43 -0600 Subject: [PATCH] added destinations --- app/controllers/destinations_controller.rb | 83 +++++++++ app/helpers/destinations_helper.rb | 2 + app/models/destination.rb | 2 + app/views/destinations/index.html.haml | 11 ++ config/locales/diaspora/en.yml | 3 + config/routes.rb | 2 + .../20120128024410_create_destinations.rb | 15 ++ db/schema.rb | 10 +- .../destinations_controller_spec.rb | 164 ++++++++++++++++++ spec/helpers/destinations_helper_spec.rb | 15 ++ spec/models/destination_spec.rb | 5 + spec/requests/destinations_spec.rb | 11 ++ spec/routing/destinations_routing_spec.rb | 35 ++++ .../views/destinations/edit.html.haml_spec.rb | 22 +++ .../destinations/index.html.haml_spec.rb | 28 +++ spec/views/destinations/new.html.haml_spec.rb | 22 +++ .../views/destinations/show.html.haml_spec.rb | 21 +++ 17 files changed, 450 insertions(+), 1 deletion(-) create mode 100644 app/controllers/destinations_controller.rb create mode 100644 app/helpers/destinations_helper.rb create mode 100644 app/models/destination.rb create mode 100644 app/views/destinations/index.html.haml create mode 100644 db/migrate/20120128024410_create_destinations.rb create mode 100644 spec/controllers/destinations_controller_spec.rb create mode 100644 spec/helpers/destinations_helper_spec.rb create mode 100644 spec/models/destination_spec.rb create mode 100644 spec/requests/destinations_spec.rb create mode 100644 spec/routing/destinations_routing_spec.rb create mode 100644 spec/views/destinations/edit.html.haml_spec.rb create mode 100644 spec/views/destinations/index.html.haml_spec.rb create mode 100644 spec/views/destinations/new.html.haml_spec.rb create mode 100644 spec/views/destinations/show.html.haml_spec.rb diff --git a/app/controllers/destinations_controller.rb b/app/controllers/destinations_controller.rb new file mode 100644 index 00000000000..f0fb07c61f7 --- /dev/null +++ b/app/controllers/destinations_controller.rb @@ -0,0 +1,83 @@ +class DestinationsController < ApplicationController + # GET /destinations + # GET /destinations.xml + def index + @destinations = Destination.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @destinations } + end + end + + # GET /destinations/1 + # GET /destinations/1.xml + def show + @destination = Destination.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @destination } + end + end + + # GET /destinations/new + # GET /destinations/new.xml + def new + @destination = Destination.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @destination } + end + end + + # GET /destinations/1/edit + def edit + @destination = Destination.find(params[:id]) + end + + # POST /destinations + # POST /destinations.xml + def create + @destination = Destination.new(params[:destination]) + + respond_to do |format| + if @destination.save + format.html { redirect_to(@destination, :notice => 'Destination was successfully created.') } + format.xml { render :xml => @destination, :status => :created, :location => @destination } + else + format.html { render :action => "new" } + format.xml { render :xml => @destination.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /destinations/1 + # PUT /destinations/1.xml + def update + @destination = Destination.find(params[:id]) + + respond_to do |format| + if @destination.update_attributes(params[:destination]) + format.html { redirect_to(@destination, :notice => 'Destination was successfully updated.') } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @destination.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /destinations/1 + # DELETE /destinations/1.xml + def destroy + @destination = Destination.find(params[:id]) + @destination.destroy + + respond_to do |format| + format.html { redirect_to(destinations_url) } + format.xml { head :ok } + end + end +end diff --git a/app/helpers/destinations_helper.rb b/app/helpers/destinations_helper.rb new file mode 100644 index 00000000000..9f11439bf72 --- /dev/null +++ b/app/helpers/destinations_helper.rb @@ -0,0 +1,2 @@ +module DestinationsHelper +end diff --git a/app/models/destination.rb b/app/models/destination.rb new file mode 100644 index 00000000000..6012b67e0aa --- /dev/null +++ b/app/models/destination.rb @@ -0,0 +1,2 @@ +class Destination < ActiveRecord::Base +end diff --git a/app/views/destinations/index.html.haml b/app/views/destinations/index.html.haml new file mode 100644 index 00000000000..a38eaf2c720 --- /dev/null +++ b/app/views/destinations/index.html.haml @@ -0,0 +1,11 @@ +%h1 Destinations + +%ul + -@destinations.each do |destination| + %li + = link_to destination.title, destination_path(destination) + + +- if current_user.admin? + = link_to t('.new') , new_destination_path + diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index e7181742c98..606201cb93c 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -1055,6 +1055,9 @@ en: xrd_fetch_failed: "there was an error getting the xrd from account %{account}" not_enabled: "webfinger does not seem to be enabled for %{account}'s host" no_person_constructed: "No person could be constructed from this hcard." + destinations: + index: + new: "New destination" diff --git a/config/routes.rb b/config/routes.rb index 0aa11c945e2..782b0d85a52 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,8 @@ end end + resources :destinations + resources :places do resources :photos end diff --git a/db/migrate/20120128024410_create_destinations.rb b/db/migrate/20120128024410_create_destinations.rb new file mode 100644 index 00000000000..0aad9f42df7 --- /dev/null +++ b/db/migrate/20120128024410_create_destinations.rb @@ -0,0 +1,15 @@ +class CreateDestinations < ActiveRecord::Migration + def self.up + create_table :destinations do |t| + t.text :summary + t.string :title + t.string :photo_url + + t.timestamps + end + end + + def self.down + drop_table :destinations + end +end diff --git a/db/schema.rb b/db/schema.rb index 0b3ea5a1e0c..859edb41f75 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120127191343) do +ActiveRecord::Schema.define(:version => 20120128024410) do create_table "account_deletions", :force => true do |t| t.string "diaspora_handle" @@ -128,6 +128,14 @@ t.string "website" end + create_table "destinations", :force => true do |t| + t.text "summary" + t.string "title" + t.string "photo_url" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "invitations", :force => true do |t| t.text "message" t.integer "sender_id" diff --git a/spec/controllers/destinations_controller_spec.rb b/spec/controllers/destinations_controller_spec.rb new file mode 100644 index 00000000000..482d44f5f78 --- /dev/null +++ b/spec/controllers/destinations_controller_spec.rb @@ -0,0 +1,164 @@ +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 DestinationsController do + + # This should return the minimal set of attributes required to create a valid + # Destination. As you add validations to Destination, be sure to + # update the return value of this method accordingly. + def valid_attributes + {} + end + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # DestinationsController. Be sure to keep this updated too. + def valid_session + {} + end + + describe "GET index" do + it "assigns all destinations as @destinations" do + destination = Destination.create! valid_attributes + get :index, {}, valid_session + assigns(:destinations).should eq([destination]) + end + end + + describe "GET show" do + it "assigns the requested destination as @destination" do + destination = Destination.create! valid_attributes + get :show, {:id => destination.to_param}, valid_session + assigns(:destination).should eq(destination) + end + end + + describe "GET new" do + it "assigns a new destination as @destination" do + get :new, {}, valid_session + assigns(:destination).should be_a_new(Destination) + end + end + + describe "GET edit" do + it "assigns the requested destination as @destination" do + destination = Destination.create! valid_attributes + get :edit, {:id => destination.to_param}, valid_session + assigns(:destination).should eq(destination) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new Destination" do + expect { + post :create, {:destination => valid_attributes}, valid_session + }.to change(Destination, :count).by(1) + end + + it "assigns a newly created destination as @destination" do + post :create, {:destination => valid_attributes}, valid_session + assigns(:destination).should be_a(Destination) + assigns(:destination).should be_persisted + end + + it "redirects to the created destination" do + post :create, {:destination => valid_attributes}, valid_session + response.should redirect_to(Destination.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved destination as @destination" do + # Trigger the behavior that occurs when invalid params are submitted + Destination.any_instance.stub(:save).and_return(false) + post :create, {:destination => {}}, valid_session + assigns(:destination).should be_a_new(Destination) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Destination.any_instance.stub(:save).and_return(false) + post :create, {:destination => {}}, valid_session + response.should render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + it "updates the requested destination" do + destination = Destination.create! valid_attributes + # Assuming there are no other destinations in the database, this + # specifies that the Destination created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + Destination.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, {:id => destination.to_param, :destination => {'these' => 'params'}}, valid_session + end + + it "assigns the requested destination as @destination" do + destination = Destination.create! valid_attributes + put :update, {:id => destination.to_param, :destination => valid_attributes}, valid_session + assigns(:destination).should eq(destination) + end + + it "redirects to the destination" do + destination = Destination.create! valid_attributes + put :update, {:id => destination.to_param, :destination => valid_attributes}, valid_session + response.should redirect_to(destination) + end + end + + describe "with invalid params" do + it "assigns the destination as @destination" do + destination = Destination.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Destination.any_instance.stub(:save).and_return(false) + put :update, {:id => destination.to_param, :destination => {}}, valid_session + assigns(:destination).should eq(destination) + end + + it "re-renders the 'edit' template" do + destination = Destination.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Destination.any_instance.stub(:save).and_return(false) + put :update, {:id => destination.to_param, :destination => {}}, valid_session + response.should render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested destination" do + destination = Destination.create! valid_attributes + expect { + delete :destroy, {:id => destination.to_param}, valid_session + }.to change(Destination, :count).by(-1) + end + + it "redirects to the destinations list" do + destination = Destination.create! valid_attributes + delete :destroy, {:id => destination.to_param}, valid_session + response.should redirect_to(destinations_url) + end + end + +end diff --git a/spec/helpers/destinations_helper_spec.rb b/spec/helpers/destinations_helper_spec.rb new file mode 100644 index 00000000000..45b1acd4eee --- /dev/null +++ b/spec/helpers/destinations_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the DestinationsHelper. For example: +# +# describe DestinationsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe DestinationsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/destination_spec.rb b/spec/models/destination_spec.rb new file mode 100644 index 00000000000..98442520f17 --- /dev/null +++ b/spec/models/destination_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Destination do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/destinations_spec.rb b/spec/requests/destinations_spec.rb new file mode 100644 index 00000000000..02c246a9532 --- /dev/null +++ b/spec/requests/destinations_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe "Destinations" do + describe "GET /destinations" 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 destinations_path + response.status.should be(200) + end + end +end diff --git a/spec/routing/destinations_routing_spec.rb b/spec/routing/destinations_routing_spec.rb new file mode 100644 index 00000000000..1dfd88f4605 --- /dev/null +++ b/spec/routing/destinations_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe DestinationsController do + describe "routing" do + + it "routes to #index" do + get("/destinations").should route_to("destinations#index") + end + + it "routes to #new" do + get("/destinations/new").should route_to("destinations#new") + end + + it "routes to #show" do + get("/destinations/1").should route_to("destinations#show", :id => "1") + end + + it "routes to #edit" do + get("/destinations/1/edit").should route_to("destinations#edit", :id => "1") + end + + it "routes to #create" do + post("/destinations").should route_to("destinations#create") + end + + it "routes to #update" do + put("/destinations/1").should route_to("destinations#update", :id => "1") + end + + it "routes to #destroy" do + delete("/destinations/1").should route_to("destinations#destroy", :id => "1") + end + + end +end diff --git a/spec/views/destinations/edit.html.haml_spec.rb b/spec/views/destinations/edit.html.haml_spec.rb new file mode 100644 index 00000000000..48e40aaafe9 --- /dev/null +++ b/spec/views/destinations/edit.html.haml_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "destinations/edit" do + before(:each) do + @destination = assign(:destination, stub_model(Destination, + :summary => "MyText", + :title => "MyString", + :photo_url => "MyString" + )) + end + + it "renders the edit destination form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => destinations_path(@destination), :method => "post" do + assert_select "textarea#destination_summary", :name => "destination[summary]" + assert_select "input#destination_title", :name => "destination[title]" + assert_select "input#destination_photo_url", :name => "destination[photo_url]" + end + end +end diff --git a/spec/views/destinations/index.html.haml_spec.rb b/spec/views/destinations/index.html.haml_spec.rb new file mode 100644 index 00000000000..a7aae6b6906 --- /dev/null +++ b/spec/views/destinations/index.html.haml_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe "destinations/index" do + before(:each) do + assign(:destinations, [ + stub_model(Destination, + :summary => "MyText", + :title => "Title", + :photo_url => "Photo Url" + ), + stub_model(Destination, + :summary => "MyText", + :title => "Title", + :photo_url => "Photo Url" + ) + ]) + end + + it "renders a list of destinations" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => "MyText".to_s, :count => 2 + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => "Title".to_s, :count => 2 + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => "Photo Url".to_s, :count => 2 + end +end diff --git a/spec/views/destinations/new.html.haml_spec.rb b/spec/views/destinations/new.html.haml_spec.rb new file mode 100644 index 00000000000..9c626d64267 --- /dev/null +++ b/spec/views/destinations/new.html.haml_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "destinations/new" do + before(:each) do + assign(:destination, stub_model(Destination, + :summary => "MyText", + :title => "MyString", + :photo_url => "MyString" + ).as_new_record) + end + + it "renders new destination form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => destinations_path, :method => "post" do + assert_select "textarea#destination_summary", :name => "destination[summary]" + assert_select "input#destination_title", :name => "destination[title]" + assert_select "input#destination_photo_url", :name => "destination[photo_url]" + end + end +end diff --git a/spec/views/destinations/show.html.haml_spec.rb b/spec/views/destinations/show.html.haml_spec.rb new file mode 100644 index 00000000000..97bb2451789 --- /dev/null +++ b/spec/views/destinations/show.html.haml_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe "destinations/show" do + before(:each) do + @destination = assign(:destination, stub_model(Destination, + :summary => "MyText", + :title => "Title", + :photo_url => "Photo Url" + )) + end + + it "renders attributes in

" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/MyText/) + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/Title/) + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/Photo Url/) + end +end