Commit 6c452183a861ff42b031f5ad4560ea82609bbfec

Authored by Rafael Manzo
Committed by Rafael Manzo
1 parent 26975fe9

Project creation implemented

app/controllers/projects_controller.rb
1 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 35 end
... ...
app/models/project.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class Project < KalibroEntities::Entities::Project
  2 + include ActiveModel::Validations
  3 + include ActiveModel::Conversion
  4 + extend ActiveModel::Naming
  5 +
  6 + attr_accessor :name
  7 +
  8 + def persisted?
  9 + false
  10 + end
  11 +end
... ...
app/views/home/index.html.erb
1 1 <% if user_signed_in? %>
2 2 <%= link_to('Edit', edit_user_registration_path) %>
  3 + <%= link_to('New Project', new_project_path) %>
3 4 <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
4 5 <% else %>
5 6 <%= link_to('Login', new_user_session_path) %>
... ...
app/views/projects/_form.html.erb 0 → 100644
... ... @@ -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 %>
... ...
app/views/projects/new.html.erb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +<h1>New Project</h1>
  2 +
  3 +<%= render 'form' %>
  4 +
  5 +<%= link_to 'Back', root_path %>
... ...
app/views/shared/_form_errors.html.erb 0 → 100644
... ... @@ -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 16 \ No newline at end of file
... ...
spec/controllers/projects_controller_spec.rb 0 → 100644
... ... @@ -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
... ...
spec/factories/projects.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +# Read about factories at https://github.com/thoughtbot/factory_girl
  2 +
  3 +FactoryGirl.define do
  4 + factory :project do
  5 + end
  6 +end
... ...
spec/models/project_spec.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Project do
  4 + describe 'methods' do
  5 + describe 'persisted?' do
  6 + it 'should return false' do
  7 + subject.persisted?.should eq(false)
  8 + end
  9 + end
  10 + end
  11 +end
... ...