Commit 76bc05f55bb11dd128e54cf9bb4b976096fcfdb7

Authored by Rafael Manzo
1 parent 32178a64

Fixes for Configuration views

Validates ownership before display edit button
Partial moved from shared and refactored
Added Configuration link to the layout

Missing acceptance tests
app/controllers/mezuro_configurations_controller.rb
1 1 include OwnershipAuthentication
2 2  
3 3 class MezuroConfigurationsController < ApplicationController
4   - before_action :authenticate_user!,
5   - except: [:index, :show]
  4 + before_action :authenticate_user!, except: [:index, :show]
6 5 before_action :mezuro_configuration_owner?, only: [:edit, :update, :destroy]
7 6  
8 7 # GET /mezuro_configurations/new
9 8 def new
10   - @mezuro_configuration = MezuroConfiguration.new
  9 + @configuration = MezuroConfiguration.new
11 10 end
12 11  
13 12 # GET /mezuro_configurations
14 13 # GET /mezuro_configurations.json
15 14 def index
16   - @mezuro_configurations = MezuroConfiguration.all
  15 + @configurations = MezuroConfiguration.all
17 16 end
18 17  
19 18 # POST /mezuro_configurations
20 19 # POST /mezuro_configurations.json
21 20 def create
22   - @mezuro_configuration = MezuroConfiguration.new(mezuro_configuration_params)
  21 + @configuration = MezuroConfiguration.new(mezuro_configuration_params)
23 22 respond_to do |format|
24 23 create_and_redir(format)
25 24 end
... ... @@ -29,7 +28,7 @@ class MezuroConfigurationsController &lt; ApplicationController
29 28 # GET /mezuro_configurations/1.json
30 29 def show
31 30 set_mezuro_configuration
32   - @mezuro_configuration_metric_configurations = @mezuro_configuration.metric_configurations
  31 + @configuration_metric_configurations = @configuration.metric_configurations
33 32 end
34 33  
35 34 # GET /mezuro_configurations/1/edit
... ... @@ -41,8 +40,8 @@ class MezuroConfigurationsController &lt; ApplicationController
41 40  
42 41 def update
43 42 set_mezuro_configuration
44   - if @mezuro_configuration.update(mezuro_configuration_params)
45   - redirect_to(mezuro_configuration_path(@mezuro_configuration.id))
  43 + if @configuration.update(mezuro_configuration_params)
  44 + redirect_to(mezuro_configuration_path(@configuration.id))
46 45 else
47 46 render "edit"
48 47 end
... ... @@ -52,8 +51,8 @@ class MezuroConfigurationsController &lt; ApplicationController
52 51 # DELETE /mezuro_configurations/1.json
53 52 def destroy
54 53 set_mezuro_configuration
55   - current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(@mezuro_configuration.id).destroy
56   - @mezuro_configuration.destroy
  54 + current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(@configuration.id).destroy
  55 + @configuration.destroy
57 56 respond_to do |format|
58 57 format.html { redirect_to mezuro_configurations_url }
59 58 format.json { head :no_content }
... ... @@ -63,7 +62,7 @@ class MezuroConfigurationsController &lt; ApplicationController
63 62 private
64 63 # Use callbacks to share common setup or constraints between actions.
65 64 def set_mezuro_configuration
66   - @mezuro_configuration = MezuroConfiguration.find(params[:id])
  65 + @configuration = MezuroConfiguration.find(params[:id])
67 66 end
68 67  
69 68 # Never trust parameters from the scary internet, only allow the white list through.
... ... @@ -73,14 +72,14 @@ class MezuroConfigurationsController &lt; ApplicationController
73 72  
74 73 # Extracted code from create action
75 74 def create_and_redir(format)
76   - if @mezuro_configuration.save
77   - current_user.mezuro_configuration_ownerships.create mezuro_configuration_id: @mezuro_configuration.id
  75 + if @configuration.save
  76 + current_user.mezuro_configuration_ownerships.create mezuro_configuration_id: @configuration.id
78 77  
79   - format.html { redirect_to mezuro_configuration_path(@mezuro_configuration.id), notice: 'mezuro_configuration was successfully created.' }
80   - format.json { render action: 'show', status: :created, location: @mezuro_configuration }
  78 + format.html { redirect_to mezuro_configuration_path(@configuration.id), notice: 'mezuro_configuration was successfully created.' }
  79 + format.json { render action: 'show', status: :created, location: @configuration }
81 80 else
82 81 format.html { render action: 'new' }
83   - format.json { render json: @mezuro_configuration.errors, status: :unprocessable_entity }
  82 + format.json { render json: @configuration.errors, status: :unprocessable_entity }
84 83 end
85 84 end
86 85 end
... ...
app/helpers/mezuro_configurations_helper.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +module MezuroConfigurationsHelper
  2 + def configuration_owner?(configuration_id)
  3 + user_signed_in? && !current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(configuration_id).nil?
  4 + end
  5 +end
0 6 \ No newline at end of file
... ...
app/views/layouts/application.html.erb
... ... @@ -59,6 +59,7 @@
59 59 <ul class="nav">
60 60 <li><%= link_to 'Home', root_path %></li>
61 61 <li><%= link_to 'Project', projects_path %></li>
  62 + <li><%= link_to 'Configuration', mezuro_configurations_path %></li>
62 63 <li><%= link_to 'Reading Group', reading_groups_path %></li>
63 64 </ul>
64 65 <ul class="nav" style="float: right;">
... ...
app/views/mezuro_configurations/_list.html.erb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +<table class="table table-hover">
  2 + <thead>
  3 + <tr>
  4 + <th>Name</th>
  5 + <th>Description</th>
  6 + <th colspan="2"></th>
  7 + </tr>
  8 + </thead>
  9 +
  10 + <tbody>
  11 + <%= render @configurations %>
  12 + </tbody>
  13 +</table>
0 14 \ No newline at end of file
... ...
app/views/mezuro_configurations/_mezuro_configuration.html.erb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<tr>
  2 + <td><%= mezuro_configuration.name %></td>
  3 + <td><%= mezuro_configuration.description %></td>
  4 + <td><%= link_to 'Show', mezuro_configuration_path(mezuro_configuration.id), class: 'btn btn-info' %></td>
  5 + <td>
  6 + <% if configuration_owner?(mezuro_configuration.id) %>
  7 + <%= link_to 'Edit', edit_mezuro_configuration_path(mezuro_configuration.id), class: 'btn btn-info' %>
  8 + <% end %>
  9 + </td>
  10 +</tr>
0 11 \ No newline at end of file
... ...
app/views/mezuro_configurations/index.html.erb
... ... @@ -13,4 +13,4 @@
13 13 </p>
14 14 <% end %>
15 15  
16   -<%= render partial: 'shared/mezuro_configuration_list', locals: {configurations: @configurations} %>
17 16 \ No newline at end of file
  17 +<%= render partial: 'list', locals: {configurations: @configurations} %>
18 18 \ No newline at end of file
... ...
app/views/shared/_mezuro_configuration_list.html.erb
... ... @@ -1,22 +0,0 @@
1   -<table class="table table-hover">
2   - <thead>
3   - <tr>
4   - <th>Name</th>
5   - <th>Description</th>
6   - <th colspan="2"></th>
7   - </tr>
8   - </thead>
9   -
10   - <tbody>
11   - <% configurations.each do |configuration| %>
12   - <tr>
13   - <td><%= configuration.name %></td>
14   - <td><%= configuration.description %></td>
15   - <td><%= link_to 'Show', mezuro_configuration_path(configuration.id), class: 'btn btn-info' %></td>
16   - <td>
17   - <%= link_to 'Edit', edit_mezuro_configuration_path(configuration.id), class: 'btn btn-info' %>
18   - </td>
19   - </tr>
20   - <% end %>
21   - </tbody>
22   -</table>
23 0 \ No newline at end of file
spec/controllers/mezuro_configurations_controller_spec.rb
... ... @@ -90,7 +90,7 @@ describe MezuroConfigurationsController do
90 90 @ownership.expects(:destroy)
91 91 @subject.expects(:destroy)
92 92  
93   - #Those two mocks looks the same but they are necessary since params[:id] is a String and @mezuro_configuration.id is an Integer :(
  93 + #Those two mocks looks the same but they are necessary since params[:id] is a String and @configuration.id is an Integer :(
94 94 @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership)
95 95 @ownerships.expects(:find_by_mezuro_configuration_id).with(@subject.id).returns(@ownership)
96 96  
... ... @@ -164,8 +164,8 @@ describe MezuroConfigurationsController do
164 164  
165 165 it { should render_template(:edit) }
166 166  
167   - it 'should assign to @mezuro_configuration the @subject' do
168   - assigns(:mezuro_configuration).should eq(@subject)
  167 + it 'should assign to @configuration the @subject' do
  168 + assigns(:configuration).should eq(@subject)
169 169 end
170 170 end
171 171  
... ...
spec/helpers/mezuro_configurations_helper_spec.rb 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +require 'spec_helper'
  2 +
  3 +describe MezuroConfigurationsHelper do
  4 + describe 'configuration_owner?' do
  5 + before :each do
  6 + @subject = FactoryGirl.build(:mezuro_configuration)
  7 + end
  8 +
  9 + context 'returns false if not logged in' do
  10 + before :each do
  11 + helper.expects(:user_signed_in?).returns(false)
  12 + end
  13 + it { helper.configuration_owner?(@subject.id).should be_false }
  14 + end
  15 +
  16 + context 'returns false if is not the owner' do
  17 + before :each do
  18 + helper.expects(:user_signed_in?).returns(true)
  19 + helper.expects(:current_user).returns(FactoryGirl.build(:user))
  20 +
  21 + @ownerships = []
  22 + @ownerships.expects(:find_by_mezuro_configuration_id).with(@subject.id).returns(nil)
  23 +
  24 + User.any_instance.expects(:mezuro_configuration_ownerships).returns(@ownerships)
  25 + end
  26 +
  27 + it { helper.configuration_owner?(@subject.id).should be_false }
  28 + end
  29 +
  30 + context 'returns true if user is the mezuro_configuration owner' do
  31 + before :each do
  32 + helper.expects(:user_signed_in?).returns(true)
  33 + helper.expects(:current_user).returns(FactoryGirl.build(:user))
  34 +
  35 + @ownership = FactoryGirl.build(:mezuro_configuration_ownership)
  36 + @ownerships = []
  37 + @ownerships.expects(:find_by_mezuro_configuration_id).with(@subject.id).returns(@ownership)
  38 + User.any_instance.expects(:mezuro_configuration_ownerships).returns(@ownerships)
  39 + end
  40 +
  41 + it { helper.configuration_owner?(@subject.id).should be_true }
  42 + end
  43 + end
  44 +end
0 45 \ No newline at end of file
... ...