Permalink
Browse files

creating games and adding specs

  • Loading branch information...
1 parent 3fcb639 commit db2aeb79cfa119dda0799a368756685afec89e15 @cairo140 committed Jan 29, 2012
@@ -1,3 +1,2 @@
class GamesController < ResourcesController
- load_and_authorize_resource
end
@@ -1,5 +1,6 @@
class ResourcesController < ApplicationController
check_authorization
+ load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
if user_signed_in?
@@ -11,24 +12,21 @@ class ResourcesController < ApplicationController
respond_to :html
- def index
- end
-
- def show
- end
-
- def new
+ def create
+ resource.save
+ respond_with resource
end
- def edit
- end
+ protected
- def create
+ def resource
+ instance_variable_get(instance_variable_name)
end
- def update
+ # This works mostly by accident and is hard to test. Fortunately, if it breaks
+ # it will break damn near everything.
+ def instance_variable_name
+ "@#{self.class.name.sub("Controller", "").underscore.split('/').last.singularize}"
end
- def destroy
- end
end
View
@@ -1,4 +1,8 @@
class Game < ActiveRecord::Base
has_many :user_games
has_many :users, :through => :user_games
+
+ attr_accessible :name
+
+ validates :name, :presence => true, :uniqueness => true
end
@@ -0,0 +1,7 @@
+= simple_form_for @game do |f|
+ = f.error_notification
+ %fieldset
+ .inputs
+ = f.input :name
+ .form-actions
+ = f.button :submit, "Create game", :class => 'btn large primary'
@@ -1,7 +1,8 @@
.fluid-sidebar
%h1 Games
.fluid-content
- - @games.each do |game|
- = link_to game.name, game
+ %ul
+ - @games.each do |game|
+ %li= link_to game.name, game
- if can? :manage, Game
= link_to 'Add game', new_game_path
@@ -0,0 +1,4 @@
+.fluid-sidebar
+ = link_to 'Games', games_path
+.fluid-content
+ %h1= @game.name
@@ -0,0 +1,2 @@
+.fluid-content
+ = render :partial => 'form'
@@ -13,6 +13,36 @@
it 'should see a link to add a game' do
page.should have_content('Add game')
end
+
+ describe 'clicking the add game link' do
+ before { click_on 'Add game' }
+
+ it 'should see a form to create a game' do
+ page.should have_button('Create game')
+ end
+
+ describe 'filling out the form and submitting with valid input' do
+ before do
+ fill_in 'Name', :with => (@name = Faker::Lorem.words)
+ click_on 'Create game'
+ end
+
+ it 'should show the newly created game with a success message' do
+ page.should have_content(@name)
+ end
+ end
+
+ describe 'filling out the form and submitting without specifying a name' do
+ before do
+ click_on 'Create game'
+ end
+
+ it 'should kick the user back to the form with an error' do
+ page.should have_content('can\'t be blank')
+ page.should have_button('Create game')
+ end
+ end
+ end
end
end

0 comments on commit db2aeb7

Please sign in to comment.