Commit 6c452183a861ff42b031f5ad4560ea82609bbfec
Committed by
Rafael Manzo
1 parent
26975fe9
Exists in
colab
and in
4 other branches
Project creation implemented
Showing
9 changed files
with
147 additions
and
0 deletions
Show diff stats
app/controllers/projects_controller.rb
| 1 | class ProjectsController < ApplicationController | 1 | class ProjectsController < ApplicationController |
| 2 | 2 | ||
| 3 | + # GET /projects/new | ||
| 4 | + def new | ||
| 5 | + @project = Project.new | ||
| 6 | + end | ||
| 7 | + | ||
| 8 | + # POST /projects | ||
| 9 | + # POST /projects.json | ||
| 10 | + def create | ||
| 11 | + @project = Project.new(project_params) | ||
| 12 | + | ||
| 13 | + respond_to do |format| | ||
| 14 | + if @project.save | ||
| 15 | + format.html { redirect_to @project, notice: 'Project was successfully created.' } | ||
| 16 | + format.json { render action: 'show', status: :created, location: @project } | ||
| 17 | + else | ||
| 18 | + format.html { render action: 'new' } | ||
| 19 | + format.json { render json: @project.errors, status: :unprocessable_entity } | ||
| 20 | + end | ||
| 21 | + end | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + private | ||
| 25 | + # Use callbacks to share common setup or constraints between actions. | ||
| 26 | + def set_project | ||
| 27 | + @project = Project.find(params[:id]) | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | + # Never trust parameters from the scary internet, only allow the white list through. | ||
| 31 | + def project_params | ||
| 32 | + params[:project] | ||
| 33 | + end | ||
| 34 | + | ||
| 3 | end | 35 | end |
app/views/home/index.html.erb
| 1 | <% if user_signed_in? %> | 1 | <% if user_signed_in? %> |
| 2 | <%= link_to('Edit', edit_user_registration_path) %> | 2 | <%= link_to('Edit', edit_user_registration_path) %> |
| 3 | + <%= link_to('New Project', new_project_path) %> | ||
| 3 | <%= link_to('Logout', destroy_user_session_path, :method => :delete) %> | 4 | <%= link_to('Logout', destroy_user_session_path, :method => :delete) %> |
| 4 | <% else %> | 5 | <% else %> |
| 5 | <%= link_to('Login', new_user_session_path) %> | 6 | <%= link_to('Login', new_user_session_path) %> |
| @@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
| 1 | + <% #raise @project.inspect %> | ||
| 2 | +<%= form_for(@project) do |f| %> | ||
| 3 | + <%= render :partial => 'shared/form_errors', :locals => {:object => @project} %> | ||
| 4 | + | ||
| 5 | + <div class="field"> | ||
| 6 | + <%= f.label :name %><br> | ||
| 7 | + <%= f.text_field :name %> | ||
| 8 | + </div> | ||
| 9 | + | ||
| 10 | + <div class="field"> | ||
| 11 | + <%= f.label :description %><br> | ||
| 12 | + <%= f.text_area :description %> | ||
| 13 | + </div> | ||
| 14 | + | ||
| 15 | + <div class="actions"> | ||
| 16 | + <%= f.submit %> | ||
| 17 | + </div> | ||
| 18 | +<% end %> |
| @@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
| 1 | +<% if object.errors.any? || object.kalibro_errors.any? %> | ||
| 2 | + <div id="error_explanation"> | ||
| 3 | + <h2><%= pluralize(object.errors.count + object.kalibro_errors.count, "error") %> prohibited this project from being saved:</h2> | ||
| 4 | + | ||
| 5 | + <ul> | ||
| 6 | + <% object.errors.full_messages.each do |msg| %> | ||
| 7 | + <li><%= msg %></li> | ||
| 8 | + <% end %> | ||
| 9 | + | ||
| 10 | + <% object.kalibro_errors.each do |msg| %> | ||
| 11 | + <li><%= msg %></li> | ||
| 12 | + <% end %> | ||
| 13 | + </ul> | ||
| 14 | + </div> | ||
| 15 | +<% end %> | ||
| 0 | \ No newline at end of file | 16 | \ No newline at end of file |
| @@ -0,0 +1,48 @@ | @@ -0,0 +1,48 @@ | ||
| 1 | +require 'spec_helper' | ||
| 2 | + | ||
| 3 | +describe ProjectsController do | ||
| 4 | + | ||
| 5 | + describe 'new' do | ||
| 6 | + before :each do | ||
| 7 | + get :new | ||
| 8 | + end | ||
| 9 | + | ||
| 10 | + it { should respond_with(:success) } | ||
| 11 | + it { should render_template(:new) } | ||
| 12 | + end | ||
| 13 | + | ||
| 14 | + describe 'create' do | ||
| 15 | + | ||
| 16 | + context 'with a valid fields' do | ||
| 17 | + before :each do | ||
| 18 | + @subject = FactoryGirl.build(:project) | ||
| 19 | + | ||
| 20 | + Project.expects(:new).at_least_once.with(@subject.to_hash).returns(@subject) | ||
| 21 | + Project.any_instance.expects(:save).returns(true) | ||
| 22 | + | ||
| 23 | + post :create, :project => @subject.to_hash | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + it 'should redirect to the show view' do | ||
| 27 | + pending("Probably incompatibility between Rails 4 and RSpec. It isn't expecting an slash at the end." ) do | ||
| 28 | + response.should redirect_to project_path(@subject) | ||
| 29 | + end | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + it { should respond_with(:redirect) } | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + context 'with an invalid field' do | ||
| 36 | + before :each do | ||
| 37 | + @subject = FactoryGirl.build(:project) | ||
| 38 | + | ||
| 39 | + Project.expects(:new).at_least_once.with(@subject.to_hash).returns(@subject) | ||
| 40 | + Project.any_instance.expects(:save).returns(false) | ||
| 41 | + | ||
| 42 | + post :create, :project => @subject.to_hash | ||
| 43 | + end | ||
| 44 | + | ||
| 45 | + it { should render_template(:new) } | ||
| 46 | + end | ||
| 47 | + end | ||
| 48 | +end |