Skip to content

Instantly share code, notes, and snippets.

@tanraya
Last active December 21, 2015 16:08
Show Gist options
  • Save tanraya/6331019 to your computer and use it in GitHub Desktop.
Save tanraya/6331019 to your computer and use it in GitHub Desktop.
#########################################################################################
# Manage websites
class Dashboard::WebsitesController < ApplicationController
before_action :find_website, only: %w(show edit update destroy)
def index
@websites = current_user.websites
end
def show
end
def new
@website = current_user.websites.new
end
def edit
end
def create
@website = current_user.websites.new(website_params)
@website.save
respond_with @website, location: dashboard_websites_path
end
def update
@website.update_attributes(website_params)
respond_with @website, location: dashboard_websites_path
end
def destroy
@website.destroy
redirect_to dashboard_websites_path
end
protected
def find_website
@website = current_user.websites.find(params[:id])
end
def website_params
params.require(:website).permit(:hostname)
end
end
#########################################################################################
# And controller specs
describe Dashboard::WebsitesController do
let(:website) { mock_model(Website) }
let(:stub_find) { controller.stub_chain(:current_user, :websites, :find).and_return(website) }
let(:stub_new) { controller.stub_chain(:current_user, :websites, :new).and_return(website) }
describe "#index" do
before do
controller.stub_chain(:current_user, :websites).and_return([website])
get :index
end
it { should respond_with(:success) }
it { should render_template(:index) }
specify { assigns(:websites).should eq([website]) }
end
describe "#show" do
before do
stub_find
get :show, :id => website
end
it { should respond_with(:success) }
it { should render_template(:show) }
specify { assigns(:website).should eq(website) }
end
describe "#edit" do
before do
stub_find
get :edit, :id => website
end
it { should respond_with(:success) }
it { should render_template(:edit) }
specify { assigns(:website).should eq(website) }
end
describe "#new" do
before do
stub_new
get :new
end
it { should respond_with(:success) }
it { should render_template(:new) }
specify { assigns(:website).should eq(website) }
end
describe "#destroy" do
before do
stub_find
delete :destroy, :id => website
end
specify { assigns(:website).should eq(website) }
it { should redirect_to(action: 'index') }
end
describe '#create' do
let(:http_params) { ActionController::Parameters.new(website: { hostname: 'http://example.com' }) }
before { stub_new }
context 'when saves successfully' do
before do
website.stub(:save).and_return(true)
post :create, http_params
end
it { should redirect_to(action: 'index') }
end
context 'when fails to save' do
before do
website.stub(:save).and_return(false)
website.stub(:errors).and_return(:some => "error here")
post :create, http_params
end
specify { assigns[:website].should be_eql(website) }
it { should render_template('new') }
end
end
describe '#update' do
let(:http_params) { ActionController::Parameters.new(:id => website, website: { hostname: 'http://example.com' }) }
before { stub_find }
context 'when updates successfully' do
before do
website.stub(:update_attributes).and_return(true)
patch :update, http_params
end
it { should redirect_to(action: 'index') }
end
context 'when fails to update' do
before do
website.stub(:update_attributes).and_return(false)
website.stub(:errors).and_return(:some => "error here")
patch :update, http_params
end
specify { assigns[:website].should eq(website) }
it { should render_template('edit') }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment