Commit f672063dd85571bd1ee4686f2a53c94f7622ac33

Authored by Thiago Ribeiro
Committed by Rafael Manzo
1 parent 4af98194

Fixed tests in project_controller.rb and closes #25

app/controllers/projects_controller.rb
... ... @@ -40,18 +40,14 @@ class ProjectsController < ApplicationController
40 40 def edit
41 41 set_project
42 42 @project_image = ProjectImage.find_by_project_id(@project.id)
43   - if !@project_image.nil?
44   - @project_image.check_no_image
45   - end
  43 + @project_image.check_no_image
46 44 end
47 45  
48 46 def update
49 47 set_project
50 48 @project_image = ProjectImage.find_by_project_id(@project.id)
51   - if !@project_image.nil?
52   - @project_image.update(project_params[:image_url])
53   - end
54 49 if @project.update(project_params)
  50 + @project_image.update(image_url: project_params[:image_url])
55 51 redirect_to(project_path(@project.id))
56 52 else
57 53 render "edit"
... ... @@ -71,25 +67,25 @@ class ProjectsController < ApplicationController
71 67 end
72 68  
73 69 private
74   - # Use callbacks to share common setup or constraints between actions.
75   - def set_project
76   - @project = Project.find(params[:id])
77   - end
  70 + # Use callbacks to share common setup or constraints between actions.
  71 + def set_project
  72 + @project = Project.find(params[:id])
  73 + end
78 74  
79   - # Never trust parameters from the scary internet, only allow the white list through.
80   - def project_params
81   - params[:project]
82   - end
  75 + # Never trust parameters from the scary internet, only allow the white list through.
  76 + def project_params
  77 + params[:project]
  78 + end
83 79  
84   - # Extracted code from create action
85   - def create_and_redir(format)
86   - if @project.save
87   - current_user.project_ownerships.create project_id: @project.id
88   - format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' }
89   - format.json { render action: 'show', status: :created, location: @project }
90   - else
91   - format.html { render action: 'new' }
92   - format.json { render json: @project.errors, status: :unprocessable_entity }
93   - end
  80 + # Extracted code from create action
  81 + def create_and_redir(format)
  82 + if @project.save
  83 + current_user.project_ownerships.create project_id: @project.id
  84 + format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' }
  85 + format.json { render action: 'show', status: :created, location: @project }
  86 + else
  87 + format.html { render action: 'new' }
  88 + format.json { render json: @project.errors, status: :unprocessable_entity }
94 89 end
  90 + end
95 91 end
... ...
app/models/project.rb
1 1 require "validators/kalibro_uniqueness_validator.rb"
2 2  
3 3 class Project < KalibroGatekeeperClient::Entities::Project
4   - include KalibroRecord
  4 + include KalibroRecord
5 5  
6   - attr_accessor :name,:image_url
7   - validates :name, presence: true, kalibro_uniqueness: true
  6 + attr_accessor :name,:image_url
  7 + validates :name, presence: true, kalibro_uniqueness: true
8 8  
9   - def repositories
10   - Repository.repositories_of(self.id)
11   - end
12   -
13   - def self.latest(count = 1)
14   - all.sort { |a,b| b.id <=> a.id }.first(count)
15   - end
  9 + def repositories
  10 + Repository.repositories_of(self.id)
  11 + end
16 12  
  13 + def self.latest(count = 1)
  14 + all.sort { |a,b| b.id <=> a.id }.first(count)
  15 + end
17 16 end
... ...
app/models/project_image.rb
... ... @@ -5,16 +5,16 @@ class ProjectImage &lt; ActiveRecord::Base
5 5  
6 6 def check_no_image
7 7 if !self.blank?
8   - if self.image_url == "no-image-available.png"
9   - self.image_url = ""
10   - end
  8 + if self.image_url == "no-image-available.png"
  9 + self.image_url = ""
  10 + end
11 11 end
12 12 end
13 13  
14 14 def check_url
15   - if self.image_url.blank?
16   - self.image_url = "no-image-available.png"
17   - end
  15 + if self.image_url.blank?
  16 + self.image_url = "no-image-available.png"
  17 + end
18 18 end
19 19  
20 20  
... ...
app/views/projects/_form.html.erb
1 1 <%= form_for(@project, :html => { role: 'form' }) do |f| %>
2 2 <%= render :partial => 'shared/form_errors', :locals => {:object => @project} %>
3   -
4   -<div class="row margin-left-none">
5   - <div class="form-table col-md-9">
6   - <div class="form-row">
7   - <div class="field-container">
8   - <%= f.label :name, class: 'control-label' %><br>
9   - <%= f.text_field :name, :required => true, class: 'text-field form-control' %>
  3 + <div class="row margin-left-none">
  4 + <div class="form-table col-md-9">
  5 + <div class="form-row">
  6 + <div class="field-container">
  7 + <%= f.label :name, class: 'control-label' %><br>
  8 + <%= f.text_field :name, :required => true, class: 'text-field form-control' %>
  9 + </div>
  10 + <div class="form-row">
  11 + <div class="field-container">
  12 + <%= f.label :description, class: 'control-label' %><br>
  13 + <%= f.text_area :description, class: 'text-area form-control' %>
  14 + </div>
  15 + </div>
  16 + <div class="form-row">
  17 + <div class="field-container">
  18 + <%= f.label "Image url", class: 'control-label' %><br>
  19 + <%= f.text_field :image_url, class: 'text-area' ,value: @project_image.image_url %>
  20 + </div>
  21 + </div>
10 22 </div>
11 23 </div>
12   -
13   - <div class="form-row">
14   - <div class="field-container">
15   - <%= f.label :description, class: 'control-label' %><br>
16   - <%= f.text_area :description, class: 'text-area form-control' %>
17   - </div>
18   - </div>
19   -
20   - <div class="form-row">
21   - <div class="field-container">
22   - <%= f.label "Image url", class: 'control-label' %><br>
23   - <%= f.text_field :image_url, class: 'text-area' ,value: @project_image.image_url %>
24   - </div>
  24 + <div class="row margin-left-none" style="margin-top: 20px">
  25 + <%= f.submit 'Save', class: 'btn btn-primary' %>
  26 + <%= link_to 'Back', projects_path, class: 'btn btn-default' %>
25 27 </div>
26   -
27   -
28   - </div>
29   -</div>
30   -<div class="row margin-left-none" style="margin-top: 20px">
31   - <%= f.submit 'Save', class: 'btn btn-primary' %>
32   - <%= link_to 'Back', projects_path, class: 'btn btn-default' %>
33   -</div>
34   -<% end %>
  28 + <% end %>
... ...
spec/controllers/projects_controller_spec.rb
... ... @@ -18,10 +18,10 @@ describe ProjectsController, :type =&gt; :controller do
18 18 end
19 19  
20 20 context 'with valid fields' do
21   - let(:project) { FactoryGirl.build(:project) }
22   - let(: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
  21 + let(:project) { FactoryGirl.build(:project) }
  22 + let(: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
23 23  
24   - before :each do
  24 + before :each do
25 25 Project.any_instance.expects(:save).returns(true)
26 26 end
27 27  
... ... @@ -62,8 +62,8 @@ describe ProjectsController, :type =&gt; :controller do
62 62 end
63 63  
64 64 describe 'show' do
65   - subject { FactoryGirl.build(:project) }
66   - let(:repository) { FactoryGirl.build(:repository) }
  65 + subject { FactoryGirl.build(:project) }
  66 + let(:repository) { FactoryGirl.build(:repository) }
67 67 before :each do
68 68 Project.expects(:find).with(subject.id.to_s).returns(subject)
69 69 subject.expects(:repositories).returns(repository)
... ... @@ -116,7 +116,7 @@ describe ProjectsController, :type =&gt; :controller do
116 116 delete :destroy, :id => @subject.id
117 117 end
118 118  
119   - it { is_expected.to redirect_to(projects_path) }
  119 + it { is_expected.to redirect_to(projects_path) }
120 120 end
121 121 end
122 122  
... ... @@ -142,6 +142,7 @@ describe ProjectsController, :type =&gt; :controller do
142 142 describe 'edit' do
143 143 before do
144 144 @subject = FactoryGirl.build(:project)
  145 + @project_image = FactoryGirl.create(:project_image)
145 146 end
146 147  
147 148 context 'with an User logged in' do
... ... @@ -149,7 +150,6 @@ describe ProjectsController, :type =&gt; :controller do
149 150 @user = FactoryGirl.create(:user)
150 151 @ownership = FactoryGirl.build(:project_ownership)
151 152 @ownerships = []
152   -
153 153 User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships)
154 154  
155 155 sign_in @user
... ... @@ -159,6 +159,7 @@ describe ProjectsController, :type =&gt; :controller do
159 159 before :each do
160 160 Project.expects(:find).with(@subject.id.to_s).returns(@subject)
161 161 @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)
  162 + ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image)
162 163  
163 164 get :edit, :id => @subject.id
164 165 end
... ... @@ -194,8 +195,10 @@ describe ProjectsController, :type =&gt; :controller do
194 195  
195 196 describe 'update' do
196 197 before do
197   - @subject = FactoryGirl.build(:project)
198   - @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
  198 + @project_image = FactoryGirl.create(:project_image)
  199 + @subject = FactoryGirl.create(:project)
  200 + @subject_params = Hash[FactoryGirl.attributes_for(:project).map { |k,v| [k.to_s, v.to_s] }]
  201 +
199 202 end
200 203  
201 204 context 'when the user is logged in' do
... ... @@ -207,9 +210,10 @@ describe ProjectsController, :type =&gt; :controller do
207 210 before do
208 211 @ownership = FactoryGirl.build(:project_ownership)
209 212 @ownerships = []
210   -
211 213 @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)
212 214 User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships)
  215 + ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image)
  216 +
213 217 end
214 218  
215 219 context 'with valid fields' do
... ... @@ -221,7 +225,6 @@ describe ProjectsController, :type =&gt; :controller do
221 225 context 'rendering the show' do
222 226 before :each do
223 227 Project.expects(:exists?).returns(true)
224   -
225 228 post :update, :id => @subject.id, :project => @subject_params
226 229 end
227 230  
... ... @@ -241,7 +244,7 @@ describe ProjectsController, :type =&gt; :controller do
241 244  
242 245 context 'with an invalid field' do
243 246 before :each do
244   - Project.expects(:find).with(@subject.id.to_s).returns(@subject)
  247 + #Project.expects(:find).with(@subject.id.to_s).returns(@subject)
245 248 Project.any_instance.expects(:update).with(@subject_params).returns(false)
246 249  
247 250 post :update, :id => @subject.id, :project => @subject_params
... ...
spec/factories/project_images.rb
1 1 # Read about factories at https://github.com/thoughtbot/factory_girl
2 2  
3 3 FactoryGirl.define do
4   - factory :project_image do
5   - image_url "MyString"
6   - end
  4 + factory :project_image do
  5 + project_id 1
  6 + image_url "Example"
  7 + end
  8 +
  9 + factory :project_no_image, class: ProjectImage do
  10 + image_url nil
  11 + end
  12 +
7 13 end
... ...
spec/models/project_image_spec.rb
... ... @@ -3,5 +3,23 @@ require &#39;rails_helper&#39;
3 3 RSpec.describe ProjectImage, :type => :model do
4 4 describe 'associations' do
5 5 it { is_expected.to belong_to(:project) }
  6 + it { should belong_to(:project) }
6 7 end
  8 +
  9 + describe 'methods' do
  10 +
  11 + it 'should validate url blank' do
  12 + @project_no_image = FactoryGirl.create(:project_no_image)
  13 + expect(@project_no_image.image_url).to eq("no-image-available.png")
  14 + end
  15 +
  16 + it "Can be assigns" do
  17 + expect(ProjectImage.new).to be_an_instance_of(ProjectImage)
  18 + end
  19 +
  20 + it 'should validate url field' do
  21 + @project_no_image = FactoryGirl.create(:project_no_image)
  22 + expect(@project_no_image.check_no_image).to eq("")
  23 + end
  24 + end
7 25 end
... ...