Commit 54bf07cf7cc906650e207aea2c0707304025059d

Authored by Rafael Manzo
1 parent c1479fef

Reading creation (missing validations)

Signed-off by: Fellipe Souto Sampaio <fllsouto@gmail.com>
app/controllers/readings_controller.rb
... ... @@ -4,4 +4,41 @@ class ReadingsController &lt; ApplicationController
4 4 before_action :authenticate_user!, except: [:show, :index]
5 5 # before_action :reading_group_owner?, except: [:show]
6 6  
  7 + def new
  8 + @reading_group_id = params[:reading_group_id]
  9 + @reading = Reading.new
  10 + end
  11 +
  12 + def create
  13 + @reading = Reading.new(reading_params)
  14 + @reading.group_id = params[:reading_group_id]
  15 + respond_to do |format|
  16 + create_and_redir(format)
  17 + end
  18 + end
  19 +
  20 + private
  21 +
  22 + # Never trust parameters from the scary internet, only allow the white list through.
  23 + def reading_params
  24 + params[:reading]
  25 + end
  26 +
  27 + # Duplicated code on create and update actions extracted here
  28 + def failed_action(format, destiny_action)
  29 + @reading_group_id = params[:reading_group_id]
  30 +
  31 + format.html { render action: destiny_action }
  32 + format.json { render json: @reading.errors, status: :unprocessable_entity }
  33 + end
  34 +
  35 + # Code extracted from create action
  36 + def create_and_redir(format)
  37 + if @reading.save
  38 + format.html { redirect_to reading_group_path(@reading.group_id), notice: 'Reading was successfully created.' }
  39 + else
  40 + failed_action(format, 'new')
  41 + end
  42 + end
  43 +
7 44 end
... ...
app/views/readings/_form.html.erb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +<%= render :partial => 'shared/form_errors', :locals => {:object => @reading} %>
  2 +
  3 +<div class="form-group">
  4 + <%= f.label :label, class: 'control-label' %><br>
  5 + <%= f.text_field :label, class: 'form-control' %>
  6 +</div>
  7 +
  8 +<div class="form-group">
  9 + <%= f.label :grade, class: 'control-label' %><br>
  10 + <%= f.text_field :grade, class: 'form-control' %>
  11 +</div>
  12 +
  13 +<div class="form-group">
  14 + <%= f.label :color, class: 'control-label' %><br>
  15 + <%= f.text_field :color, class: 'form-control' %>
  16 +</div>
  17 +
  18 +<%= f.submit 'Save', class: 'btn btn-primary' %>
... ...
app/views/readings/new.html.erb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<div class="page-header">
  2 + <h1>New Reading</h1>
  3 +</div>
  4 +
  5 +<%= form_for(@reading, :url => reading_group_readings_path(@reading_group_id)) do |f| %>
  6 + <%= render partial: 'form', locals: {f: f} %>
  7 + <%= link_to 'Back', reading_group_path(@reading_group_id), class: 'btn btn-default' %>
  8 +<% end %>
... ...
features/reading/new.feature 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +# TODO: refactor when the ReadingGroup get finished
  2 +Feature: New reading
  3 + In order to be able to create new readings
  4 + As a metric specialist
  5 + I should be able to fill up a form with its informations and submit it
  6 +
  7 + @kalibro_restart @wip
  8 + Scenario: with valid fields
  9 + Given I am a regular user
  10 + And I am signed in
  11 + And I have a sample reading group
  12 + And I am at the New Reading page
  13 + And I fill the Label field with "Good"
  14 + And I fill the Grade field with "10"
  15 + And I fill the Color field with "00FF00"
  16 + When I press the Save button
  17 + Then I should see "Reading was successfully created"
... ...
features/step_definitions/reading_steps.rb
... ... @@ -2,3 +2,7 @@ Given(/^I have a sample reading within the sample reading group$/) do
2 2 @reading = FactoryGirl.create(:reading, {group_id: @reading_group.id, id: nil})
3 3 end
4 4  
  5 +Given(/^I am at the New Reading page$/) do
  6 + visit new_reading_group_reading_url(@reading_group.id)
  7 +end
  8 +
... ...
spec/controllers/readings_controller_spec.rb 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +require 'spec_helper'
  2 +
  3 +describe ReadingsController do
  4 + let(:reading_group) { FactoryGirl.build(:reading_group) }
  5 +
  6 + describe 'new' do
  7 + before :each do
  8 + sign_in FactoryGirl.create(:user)
  9 + get :new, reading_group_id: reading_group.id
  10 + end
  11 +
  12 + it { should respond_with(:success) }
  13 + it { should render_template(:new) }
  14 + end
  15 +
  16 + describe 'create' do
  17 + let(:reading) { FactoryGirl.build(:reading) }
  18 + let(:reading_params) { Hash[FactoryGirl.attributes_for(:reading).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with symbols and integers
  19 +
  20 + before do
  21 + sign_in FactoryGirl.create(:user)
  22 + end
  23 +
  24 + context 'with valid fields' do
  25 + before :each do
  26 + Reading.any_instance.expects(:save).returns(true)
  27 +
  28 + post :create, reading_group_id: reading_group.id, reading: reading_params
  29 + end
  30 +
  31 + it { should respond_with(:redirect) }
  32 + end
  33 +
  34 + context 'with invalid fields' do
  35 + before :each do
  36 + Reading.any_instance.expects(:save).returns(false)
  37 +
  38 + post :create, reading_group_id: reading_group.id, reading: reading_params
  39 + end
  40 +
  41 + it { should render_template(:new) }
  42 + end
  43 + end
  44 +end
0 45 \ No newline at end of file
... ...