Commit cc9d5cd9c877c335912104f4be1ad53dca3202a7

Authored by Guilherme Rojas V. de Lima + Rafael Reggiani Manzo
Committed by Rafael Manzo
1 parent 6c452183

Showing and Listing projects

app/controllers/projects_controller.rb
... ... @@ -5,6 +5,12 @@ class ProjectsController < ApplicationController
5 5 @project = Project.new
6 6 end
7 7  
  8 + # GET /projects
  9 + # GET /projects.json
  10 + def index
  11 + @projects = Project.all
  12 + end
  13 +
8 14 # POST /projects
9 15 # POST /projects.json
10 16 def create
... ... @@ -12,7 +18,7 @@ class ProjectsController < ApplicationController
12 18  
13 19 respond_to do |format|
14 20 if @project.save
15   - format.html { redirect_to @project, notice: 'Project was successfully created.' }
  21 + format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' }
16 22 format.json { render action: 'show', status: :created, location: @project }
17 23 else
18 24 format.html { render action: 'new' }
... ... @@ -21,6 +27,12 @@ class ProjectsController < ApplicationController
21 27 end
22 28 end
23 29  
  30 + # GET /project/1
  31 + # GET /project/1.json
  32 + def show
  33 + set_project
  34 + end
  35 +
24 36 private
25 37 # Use callbacks to share common setup or constraints between actions.
26 38 def set_project
... ...
app/models/project.rb
... ... @@ -2,8 +2,7 @@ class Project < KalibroEntities::Entities::Project
2 2 include ActiveModel::Validations
3 3 include ActiveModel::Conversion
4 4 extend ActiveModel::Naming
5   -
6   - attr_accessor :name
  5 + delegate :url_helpers, to: 'Rails.application.routes'
7 6  
8 7 def persisted?
9 8 false
... ...
app/views/projects/index.html.erb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +<h1>Listing Projects</h1>
  2 +
  3 +<table>
  4 + <thead>
  5 + <tr>
  6 + <th>Name</th>
  7 + <th>Description</th>
  8 + <th></th>
  9 + </tr>
  10 + </thead>
  11 +
  12 + <tbody>
  13 + <% @projects.each do |project| %>
  14 + <tr>
  15 + <td><%= project.name %></td>
  16 + <td><%= project.description %></td>
  17 + <td><%= link_to 'Show', project_path(project.id) %><%=project.inspect%></td>
  18 + </tr>
  19 + <% end %>
  20 + </tbody>
  21 +</table>
  22 +
  23 +<br>
  24 +
  25 +<%= link_to 'New Project', new_project_path %>
... ...
app/views/projects/show.html.erb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +<p>
  2 + <strong>Name:</strong>
  3 + <%= @project.name %>
  4 +</p>
  5 +
  6 +<p>
  7 + <strong>Description:</strong>
  8 + <%= @project.description %>
  9 +</p>
  10 +
  11 +<%= link_to 'Back', root_path %>
... ...
spec/controllers/projects_controller_spec.rb
... ... @@ -16,11 +16,12 @@ describe ProjectsController do
16 16 context 'with a valid fields' do
17 17 before :each do
18 18 @subject = FactoryGirl.build(:project)
19   -
20   - Project.expects(:new).at_least_once.with(@subject.to_hash).returns(@subject)
  19 + @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
  20 +
  21 + Project.expects(:new).at_least_once.with(@subject_params).returns(@subject)
21 22 Project.any_instance.expects(:save).returns(true)
22 23  
23   - post :create, :project => @subject.to_hash
  24 + post :create, :project => @subject_params
24 25 end
25 26  
26 27 it 'should redirect to the show view' do
... ... @@ -35,14 +36,35 @@ describe ProjectsController do
35 36 context 'with an invalid field' do
36 37 before :each do
37 38 @subject = FactoryGirl.build(:project)
  39 + @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
38 40  
39   - Project.expects(:new).at_least_once.with(@subject.to_hash).returns(@subject)
  41 + Project.expects(:new).at_least_once.with(@subject_params).returns(@subject)
40 42 Project.any_instance.expects(:save).returns(false)
41 43  
42   - post :create, :project => @subject.to_hash
  44 + post :create, :project => @subject_params
43 45 end
44 46  
45 47 it { should render_template(:new) }
46 48 end
47 49 end
  50 +
  51 + describe 'show' do
  52 + before :each do
  53 + @subject = FactoryGirl.build(:project)
  54 + Project.expects(:find).with(@subject.id.to_s).returns(@subject)
  55 + get :show, :id => @subject.id
  56 + end
  57 +
  58 + it { should render_template(:show) }
  59 + end
  60 +
  61 + describe 'index' do
  62 + before :each do
  63 + @subject = FactoryGirl.build(:project)
  64 + Project.expects(:all).returns([@subject])
  65 + get :index
  66 + end
  67 +
  68 + it { should render_template(:index) }
  69 + end
48 70 end
... ...
spec/factories/projects.rb
... ... @@ -2,5 +2,8 @@
2 2  
3 3 FactoryGirl.define do
4 4 factory :project do
  5 + id 1
  6 + name "QT Calculator"
  7 + description "A simple calculator for us."
5 8 end
6 9 end
... ...