Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

creating games and adding specs

  • Loading branch information...
commit db2aeb79cfa119dda0799a368756685afec89e15 1 parent 3fcb639
@cairo140 authored
View
1  app/controllers/games_controller.rb
@@ -1,3 +1,2 @@
class GamesController < ResourcesController
- load_and_authorize_resource
end
View
24 app/controllers/resources_controller.rb
@@ -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
4 app/models/game.rb
@@ -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
View
7 app/views/games/_form.html.haml
@@ -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'
View
5 app/views/games/index.html.haml
@@ -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
View
4 app/views/games/show.html.haml
@@ -0,0 +1,4 @@
+.fluid-sidebar
+ = link_to 'Games', games_path
+.fluid-content
+ %h1= @game.name
View
2  app/views/resources/new.html.haml
@@ -0,0 +1,2 @@
+.fluid-content
+ = render :partial => 'form'
View
30 spec/requests/games/creating_games_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.