Commit 5e08e4ec9fc26b13464df0095df6f13596393fe1

Authored by Fellipe Souto Sampaio
Committed by Rafael Manzo
1 parent 71e31d89

Helpers reading group

Missing Acceptance Test

Signed-off By : Guilherme Rojas V. de Lima <guilhermehrojas@gmail.com>
app/controllers/readings_controller.rb
1 1 include OwnershipAuthentication
2 2  
3 3 class ReadingsController < ApplicationController
4   - before_action :authenticate_user!, except: [:show]
  4 + before_action :authenticate_user!, except: [:show, :index]
5 5 # before_action :reading_group_owner?, except: [:show]
6 6  
7 7 end
... ...
app/helpers/reading_groups_helper.rb 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +module ReadingGroupsHelper
  2 + def reading_groups_owner? reading_group_id
  3 + user_signed_in? && !current_user.reading_group_ownerships.find_by_reading_group_id(reading_group_id).nil?
  4 + end
  5 +end
0 6 \ No newline at end of file
... ...
app/views/reading_groups/index.html.erb
... ... @@ -0,0 +1,16 @@
  1 +<div class="page-header">
  2 + <h1>Reading Groups</h1>
  3 +</div>
  4 +
  5 +<% if user_signed_in? %>
  6 + <p>
  7 + <%= link_to 'New Reading Group', new_reading_group_path, class: 'btn btn-primary' %>
  8 + </p>
  9 +<%else%>
  10 + <p class="alert alert-warning alert-dismissable">
  11 + <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
  12 + To create new reading groups you must be logged in page.
  13 + </p>
  14 +<% end %>
  15 +
  16 +<%= render partial: 'shared/reading_groups_list', locals: {reading_groups: @reading_groups} %>
0 17 \ No newline at end of file
... ...
app/views/shared/_reading_groups_list.erb 0 → 100644
... ... @@ -0,0 +1,25 @@
  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 + <% reading_groups.each do |reading_group| %>
  12 + <% p reading_group %>
  13 + <tr>
  14 + <td><%= reading_group.name %></td>
  15 + <td><%= reading_group.description %></td>
  16 + <td><%= link_to 'Show', reading_group_path(reading_group.id), class: 'btn btn-info' %></td>
  17 + <td>
  18 + <% if reading_groups_owner? reading_group.id %>
  19 + <%= link_to 'Edit', edit_reading_group_path(reading_group.id), class: 'btn btn-info' %>
  20 + <% end %>
  21 + </td>
  22 + </tr>
  23 + <% end %>
  24 + </tbody>
  25 +</table>
0 26 \ No newline at end of file
... ...
spec/helpers/reading_groups_helper_spec.rb 0 → 100644
... ... @@ -0,0 +1,46 @@
  1 +require 'spec_helper'
  2 +
  3 +describe ReadingGroupsHelper do
  4 +
  5 + describe 'reading_group_owner?' do
  6 + before :each do
  7 + @subject = FactoryGirl.build(:reading_group)
  8 + end
  9 +
  10 + context 'returns false if not logged in' do
  11 + before :each do
  12 + helper.expects(:user_signed_in?).returns(false)
  13 + end
  14 + it { helper.reading_groups_owner?(@subject.id).should be_false }
  15 + end
  16 +
  17 + context 'returns false if is not the owner' do
  18 + before :each do
  19 + helper.expects(:user_signed_in?).returns(true)
  20 + helper.expects(:current_user).returns(FactoryGirl.build(:user))
  21 +
  22 + @ownerships = []
  23 + @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(nil)
  24 +
  25 + User.any_instance.expects(:reading_group_ownerships).returns(@ownerships)
  26 + end
  27 +
  28 + it { helper.reading_groups_owner?(@subject.id).should be_false }
  29 + end
  30 +
  31 + context 'returns true if user is the reading_group owner' do
  32 + before :each do
  33 + helper.expects(:user_signed_in?).returns(true)
  34 + helper.expects(:current_user).returns(FactoryGirl.build(:user))
  35 +
  36 + @ownership = FactoryGirl.build(:reading_group_ownership)
  37 + @ownerships = []
  38 + @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership)
  39 + User.any_instance.expects(:reading_group_ownerships).returns(@ownerships)
  40 + end
  41 +
  42 + it { helper.reading_groups_owner?(@subject.id).should be_true }
  43 + end
  44 + end
  45 +
  46 +end
0 47 \ No newline at end of file
... ...