From 76bc05f55bb11dd128e54cf9bb4b976096fcfdb7 Mon Sep 17 00:00:00 2001
From: Rafael Reggiani Manzo
Date: Tue, 28 Jan 2014 11:02:50 -0200
Subject: [PATCH] Fixes for Configuration views
---
app/controllers/mezuro_configurations_controller.rb | 31 +++++++++++++++----------------
app/helpers/mezuro_configurations_helper.rb | 5 +++++
app/views/layouts/application.html.erb | 1 +
app/views/mezuro_configurations/_list.html.erb | 13 +++++++++++++
app/views/mezuro_configurations/_mezuro_configuration.html.erb | 10 ++++++++++
app/views/mezuro_configurations/index.html.erb | 2 +-
app/views/shared/_mezuro_configuration_list.html.erb | 22 ----------------------
spec/controllers/mezuro_configurations_controller_spec.rb | 6 +++---
spec/helpers/mezuro_configurations_helper_spec.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 92 insertions(+), 42 deletions(-)
create mode 100644 app/helpers/mezuro_configurations_helper.rb
create mode 100644 app/views/mezuro_configurations/_list.html.erb
create mode 100644 app/views/mezuro_configurations/_mezuro_configuration.html.erb
delete mode 100644 app/views/shared/_mezuro_configuration_list.html.erb
create mode 100644 spec/helpers/mezuro_configurations_helper_spec.rb
diff --git a/app/controllers/mezuro_configurations_controller.rb b/app/controllers/mezuro_configurations_controller.rb
index 595d317..fa8bff0 100644
--- a/app/controllers/mezuro_configurations_controller.rb
+++ b/app/controllers/mezuro_configurations_controller.rb
@@ -1,25 +1,24 @@
include OwnershipAuthentication
class MezuroConfigurationsController < ApplicationController
- before_action :authenticate_user!,
- except: [:index, :show]
+ before_action :authenticate_user!, except: [:index, :show]
before_action :mezuro_configuration_owner?, only: [:edit, :update, :destroy]
# GET /mezuro_configurations/new
def new
- @mezuro_configuration = MezuroConfiguration.new
+ @configuration = MezuroConfiguration.new
end
# GET /mezuro_configurations
# GET /mezuro_configurations.json
def index
- @mezuro_configurations = MezuroConfiguration.all
+ @configurations = MezuroConfiguration.all
end
# POST /mezuro_configurations
# POST /mezuro_configurations.json
def create
- @mezuro_configuration = MezuroConfiguration.new(mezuro_configuration_params)
+ @configuration = MezuroConfiguration.new(mezuro_configuration_params)
respond_to do |format|
create_and_redir(format)
end
@@ -29,7 +28,7 @@ class MezuroConfigurationsController < ApplicationController
# GET /mezuro_configurations/1.json
def show
set_mezuro_configuration
- @mezuro_configuration_metric_configurations = @mezuro_configuration.metric_configurations
+ @configuration_metric_configurations = @configuration.metric_configurations
end
# GET /mezuro_configurations/1/edit
@@ -41,8 +40,8 @@ class MezuroConfigurationsController < ApplicationController
def update
set_mezuro_configuration
- if @mezuro_configuration.update(mezuro_configuration_params)
- redirect_to(mezuro_configuration_path(@mezuro_configuration.id))
+ if @configuration.update(mezuro_configuration_params)
+ redirect_to(mezuro_configuration_path(@configuration.id))
else
render "edit"
end
@@ -52,8 +51,8 @@ class MezuroConfigurationsController < ApplicationController
# DELETE /mezuro_configurations/1.json
def destroy
set_mezuro_configuration
- current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(@mezuro_configuration.id).destroy
- @mezuro_configuration.destroy
+ current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(@configuration.id).destroy
+ @configuration.destroy
respond_to do |format|
format.html { redirect_to mezuro_configurations_url }
format.json { head :no_content }
@@ -63,7 +62,7 @@ class MezuroConfigurationsController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_mezuro_configuration
- @mezuro_configuration = MezuroConfiguration.find(params[:id])
+ @configuration = MezuroConfiguration.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
@@ -73,14 +72,14 @@ class MezuroConfigurationsController < ApplicationController
# Extracted code from create action
def create_and_redir(format)
- if @mezuro_configuration.save
- current_user.mezuro_configuration_ownerships.create mezuro_configuration_id: @mezuro_configuration.id
+ if @configuration.save
+ current_user.mezuro_configuration_ownerships.create mezuro_configuration_id: @configuration.id
- format.html { redirect_to mezuro_configuration_path(@mezuro_configuration.id), notice: 'mezuro_configuration was successfully created.' }
- format.json { render action: 'show', status: :created, location: @mezuro_configuration }
+ format.html { redirect_to mezuro_configuration_path(@configuration.id), notice: 'mezuro_configuration was successfully created.' }
+ format.json { render action: 'show', status: :created, location: @configuration }
else
format.html { render action: 'new' }
- format.json { render json: @mezuro_configuration.errors, status: :unprocessable_entity }
+ format.json { render json: @configuration.errors, status: :unprocessable_entity }
end
end
end
diff --git a/app/helpers/mezuro_configurations_helper.rb b/app/helpers/mezuro_configurations_helper.rb
new file mode 100644
index 0000000..fa66301
--- /dev/null
+++ b/app/helpers/mezuro_configurations_helper.rb
@@ -0,0 +1,5 @@
+module MezuroConfigurationsHelper
+ def configuration_owner?(configuration_id)
+ user_signed_in? && !current_user.mezuro_configuration_ownerships.find_by_mezuro_configuration_id(configuration_id).nil?
+ end
+end
\ No newline at end of file
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index f4fe471..a08ae2c 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -59,6 +59,7 @@
- <%= link_to 'Home', root_path %>
- <%= link_to 'Project', projects_path %>
+ - <%= link_to 'Configuration', mezuro_configurations_path %>
- <%= link_to 'Reading Group', reading_groups_path %>
diff --git a/app/views/mezuro_configurations/_list.html.erb b/app/views/mezuro_configurations/_list.html.erb
new file mode 100644
index 0000000..3230ce0
--- /dev/null
+++ b/app/views/mezuro_configurations/_list.html.erb
@@ -0,0 +1,13 @@
+
+
+
+ Name |
+ Description |
+ |
+
+
+
+
+ <%= render @configurations %>
+
+
\ No newline at end of file
diff --git a/app/views/mezuro_configurations/_mezuro_configuration.html.erb b/app/views/mezuro_configurations/_mezuro_configuration.html.erb
new file mode 100644
index 0000000..f191e5a
--- /dev/null
+++ b/app/views/mezuro_configurations/_mezuro_configuration.html.erb
@@ -0,0 +1,10 @@
+
+ <%= mezuro_configuration.name %> |
+ <%= mezuro_configuration.description %> |
+ <%= link_to 'Show', mezuro_configuration_path(mezuro_configuration.id), class: 'btn btn-info' %> |
+
+ <% if configuration_owner?(mezuro_configuration.id) %>
+ <%= link_to 'Edit', edit_mezuro_configuration_path(mezuro_configuration.id), class: 'btn btn-info' %>
+ <% end %>
+ |
+
\ No newline at end of file
diff --git a/app/views/mezuro_configurations/index.html.erb b/app/views/mezuro_configurations/index.html.erb
index f16d480..880a3d5 100644
--- a/app/views/mezuro_configurations/index.html.erb
+++ b/app/views/mezuro_configurations/index.html.erb
@@ -13,4 +13,4 @@
<% end %>
-<%= render partial: 'shared/mezuro_configuration_list', locals: {configurations: @configurations} %>
\ No newline at end of file
+<%= render partial: 'list', locals: {configurations: @configurations} %>
\ No newline at end of file
diff --git a/app/views/shared/_mezuro_configuration_list.html.erb b/app/views/shared/_mezuro_configuration_list.html.erb
deleted file mode 100644
index 1211987..0000000
--- a/app/views/shared/_mezuro_configuration_list.html.erb
+++ /dev/null
@@ -1,22 +0,0 @@
-
-
-
- Name |
- Description |
- |
-
-
-
-
- <% configurations.each do |configuration| %>
-
- <%= configuration.name %> |
- <%= configuration.description %> |
- <%= link_to 'Show', mezuro_configuration_path(configuration.id), class: 'btn btn-info' %> |
-
- <%= link_to 'Edit', edit_mezuro_configuration_path(configuration.id), class: 'btn btn-info' %>
- |
-
- <% end %>
-
-
\ No newline at end of file
diff --git a/spec/controllers/mezuro_configurations_controller_spec.rb b/spec/controllers/mezuro_configurations_controller_spec.rb
index cdca0e6..f3f4624 100644
--- a/spec/controllers/mezuro_configurations_controller_spec.rb
+++ b/spec/controllers/mezuro_configurations_controller_spec.rb
@@ -90,7 +90,7 @@ describe MezuroConfigurationsController do
@ownership.expects(:destroy)
@subject.expects(:destroy)
- #Those two mocks looks the same but they are necessary since params[:id] is a String and @mezuro_configuration.id is an Integer :(
+ #Those two mocks looks the same but they are necessary since params[:id] is a String and @configuration.id is an Integer :(
@ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership)
@ownerships.expects(:find_by_mezuro_configuration_id).with(@subject.id).returns(@ownership)
@@ -164,8 +164,8 @@ describe MezuroConfigurationsController do
it { should render_template(:edit) }
- it 'should assign to @mezuro_configuration the @subject' do
- assigns(:mezuro_configuration).should eq(@subject)
+ it 'should assign to @configuration the @subject' do
+ assigns(:configuration).should eq(@subject)
end
end
diff --git a/spec/helpers/mezuro_configurations_helper_spec.rb b/spec/helpers/mezuro_configurations_helper_spec.rb
new file mode 100644
index 0000000..1fa6dad
--- /dev/null
+++ b/spec/helpers/mezuro_configurations_helper_spec.rb
@@ -0,0 +1,44 @@
+require 'spec_helper'
+
+describe MezuroConfigurationsHelper do
+ describe 'configuration_owner?' do
+ before :each do
+ @subject = FactoryGirl.build(:mezuro_configuration)
+ end
+
+ context 'returns false if not logged in' do
+ before :each do
+ helper.expects(:user_signed_in?).returns(false)
+ end
+ it { helper.configuration_owner?(@subject.id).should be_false }
+ end
+
+ context 'returns false if is not the owner' do
+ before :each do
+ helper.expects(:user_signed_in?).returns(true)
+ helper.expects(:current_user).returns(FactoryGirl.build(:user))
+
+ @ownerships = []
+ @ownerships.expects(:find_by_mezuro_configuration_id).with(@subject.id).returns(nil)
+
+ User.any_instance.expects(:mezuro_configuration_ownerships).returns(@ownerships)
+ end
+
+ it { helper.configuration_owner?(@subject.id).should be_false }
+ end
+
+ context 'returns true if user is the mezuro_configuration owner' do
+ before :each do
+ helper.expects(:user_signed_in?).returns(true)
+ helper.expects(:current_user).returns(FactoryGirl.build(:user))
+
+ @ownership = FactoryGirl.build(:mezuro_configuration_ownership)
+ @ownerships = []
+ @ownerships.expects(:find_by_mezuro_configuration_id).with(@subject.id).returns(@ownership)
+ User.any_instance.expects(:mezuro_configuration_ownerships).returns(@ownerships)
+ end
+
+ it { helper.configuration_owner?(@subject.id).should be_true }
+ end
+ end
+end
\ No newline at end of file
--
libgit2 0.21.2