Skip to content

Commit

Permalink
Some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Paco Guzman committed Feb 14, 2010
1 parent 896cf21 commit 1cab4c1
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 69 deletions.
13 changes: 13 additions & 0 deletions app/controllers/walls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class WallsController < ApplicationController
include WallsHelper
helper :graffities

before_filter :to_id_required, :only => [:walltowall]

def show
@owner = Profile.active.find(params[:profile_id])
@wall = @owner.wall
Expand All @@ -10,4 +12,15 @@ def show
@last_graffity = @graffities.last
end

def walltowall
@from = Profile.active.find(params[:profile_id])
@graffities = Wall.get_walltowall_graffities_for([@from, @to])
@last_graffity = @graffities.last
end

protected
def to_id_required
@to = Profile.active.find(params[:to_id])
end

end
14 changes: 14 additions & 0 deletions app/helpers/graffities_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,18 @@ def show_more_button(graffities)
render '/member/graffities/show_more'
end
end

def graffity_link_to_comment(graffity)
link_to I18n.t('tog_wall.views.site.comment.to_comment'), "#", :id => "reply-to-#{graffity.id}"
end

def graffity_link_to_like(graffity)
link_to I18n.t('tog_wall.views.site.comment.to_like'), like_member_graffity_path(graffity), :id => "like-to-#{graffity.id}", :class => "like"
end

def graffity_link_to_walltowall(graffity)
from = graffity.profile
to = graffity.wall.profile
link_to I18n.t('tog_wall.views.site.comment.to_walltowall'), profile_wall_to_wall_path(from, to)
end
end
20 changes: 14 additions & 6 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ class Ability

def initialize(user)
can([:comment_at_wall, :reply_at_wall], Wall) do |wall|
wall && user && (user.profile == wall.profile || user.profile.is_friend_of?(wall.profile))
wall.present? && user.present? && (user.profile == wall.profile || user.profile.is_friend_of?(wall.profile))
end

can([:like_graffity], Graffity) do |graffity|
owner = graffity.wall.profile
graffity && user && (user.profile == owner || user.profile.is_friend_of?(owner)) && !graffity.liked_by?(user.profile)
unless graffity.present?
false
else
owner = graffity.wall.profile
user.present? && (user.profile == owner || user.profile.is_friend_of?(owner)) && !graffity.liked_by?(user.profile)
end
end

can([:see_walltowall], Graffity) do |graffity|
owner = graffity.wall.profile
writer = graffity.profile
graffity && user && user.profile.is_friend_of?(owner) && user.profile.is_friend_of?(writer)
unless graffity.present?
false
else
owner = graffity.wall.profile
writer = graffity.profile
user.present? && user.profile.is_friend_of?(owner) && user.profile.is_friend_of?(writer)
end
end
end
end
6 changes: 3 additions & 3 deletions app/models/graffity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Graffity < ActiveRecord::Base
{:conditions => { :profile_id => Array(*profiles).collect(&:id) }}
}

validates_presence_of :comment, :if => :is_common_graffity?
validates_presence_of :comment, :if => :common_graffity?
validates_presence_of :wall
validates_presence_of :profile

Expand Down Expand Up @@ -51,11 +51,11 @@ def self.build_like(wall, profile, params = {})
self.build_from(wall, profile, {:type_common => false}, params)
end

def is_common_graffity?
def common_graffity?
type_common
end

def is_like_graffity?
def like_graffity?
!type_common
end

Expand Down
5 changes: 3 additions & 2 deletions app/views/member/graffities/_graffity.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
</div>
<div class="more">
<span class="datetime"><%= graffity_created_at_in_words(graffity.created_at) %></span>
<%= link_to I18n.t('tog_wall.views.site.comment.to_comment'), "#", :id => "reply-to-#{graffity.id}" if can?(:reply_at_wall, graffity.wall) %>
<%= link_to I18n.t('tog_wall.views.site.comment.to_like'), like_member_graffity_path(graffity), :id => "like-to-#{graffity.id}", :class => "like" if can?(:like_graffity, graffity) %>
<%= graffity_link_to_comment(graffity) if can?(:reply_at_wall, graffity.wall) %>
<%= graffity_link_to_like(graffity) if can?(:like_graffity, graffity) %>
<%= graffity_link_to_walltowall(graffity) if can?(:see_walltowall, graffity) %>
</div>

<%= render :partial => "member/graffities/like_resume", :locals => {:likes => graffity.likes, :graffity => graffity} unless graffity.likes.empty? %>
Expand Down
2 changes: 1 addition & 1 deletion config/desert_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
end
end

resource :wall, :path_prefix => 'profiles/:profile_id', :name_prefix => 'profile_', :only => [:show]
profile_wall 'profiles/:profile_id/wall', :controller => "walls", :action => "show", :method => :get
2 changes: 1 addition & 1 deletion locale/tog_wall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ en:
site:
comment:
to_comment: Comment
to_like : Like
to_like: Like
member:
graffities:
form:
Expand Down
2 changes: 1 addition & 1 deletion locale/tog_wall_es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ es:
site:
comment:
to_comment: Comentar
to_like : Me gusta
to_like: Me gusta
member:
graffities:
form:
Expand Down
2 changes: 1 addition & 1 deletion locale/tog_wall_pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pt-BR:
site:
comment:
to_comment: Comment
to_like : Like
to_like: Like
member:
graffities:
form:
Expand Down
20 changes: 10 additions & 10 deletions spec/controllers/member/graffities_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

context "A anonymous user" do
it "denies access to create action" do
@controller.expects(:create).never
@controller.should_receive(:create).never
post :create, {:wall_id => 1}
end

it "denies access to reply action" do
@controller.expects(:reply).never
@controller.should_receive(:reply).never
post :reply, {:wall_id => 1}
end

it "denies access to like action" do
@controller.expects(:like).never
@controller.should_receive(:like).never
post :like, {:wall_id => 1}
end

it "denies access to update action" do
@controller.expects(:update).never
@controller.should_receive(:update).never
put :reply, {:wall_id => 1}
end

it "denies access to destroy action" do
@controller.expects(:destroy).never
@controller.should_receive(:destroy).never
delete :reply, {:wall_id => 1}
end
end
Expand All @@ -33,8 +33,8 @@
before(:each) do
@member = Factory(:member).profile
@owner = Factory(:member, :login => "Berlusconi").profile
@request.session[:user_id] = @member.id
@request.stubs(:referer => "/")
@request.session[:user_id] = @member.user.id
@request.stub(:referer => "/")
end

context "on POST to :create" do
Expand All @@ -57,7 +57,7 @@
@suplanted_owner = Factory(:member, :login => "Merkel").profile
end

context "trying to write a graffity in other wall" do
context "trying to write a reply in other wall" do
before(:each) do
post :reply, {:id => @graffity.id, :graffity => Factory.attributes_for(:graffity)}
end
Expand Down Expand Up @@ -114,8 +114,8 @@
before(:each) do
@member = Factory(:member).profile
@owner = Factory(:member, :login => "Berlusconi").profile
@request.session[:user_id] = @member.id
@request.stubs(:referer => "/")
@request.session[:user_id] = @member.user.id
@request.stub(:referer => "/")

@owner.add_friend(@member)
end
Expand Down
13 changes: 5 additions & 8 deletions spec/controllers/member/walls_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

#should_route :get, "/member/wall", :controller => "member/walls", :action => "show"

before(:each) do
@member = Factory(:member)
@owner = @member.profile
@wall = @owner.wall
@graffities = @wall.graffities
@request.session[:user_id] = @member.id
end

describe "show action" do
before(:each) do
@member = Factory(:member)
@owner = @member.profile
@wall = @owner.wall
@graffities = @wall.graffities
@request.session[:user_id] = @member.id
get :show
end

Expand Down
45 changes: 24 additions & 21 deletions spec/models/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe "Wall abilities" do

describe "a user not logged in" do
context "a user not logged in" do
before(:each) do
@owner = Factory(:member, :login => "ownerious")
@current_user = nil
Expand All @@ -30,7 +30,7 @@
end
end

describe "a owner" do
context "a owner" do
before(:each) do
@owner = Factory(:member, :login => "ownerious")
@current_user = @owner
Expand Down Expand Up @@ -63,26 +63,28 @@
end
end

describe "a friend of the owner" do
context "a friend of the owner" do
before(:each) do
@owner = Factory(:user, :login => "ownerious")
@owner_wall = @owner.profile.wall
@current_user = @friend = Factory(:user, :login => "friendous")
@ability = Ability.new(@current_user)

@owner.profile.add_friend(@current_user.profile)
end

it "should can comment at wall owned by his friend" do
@ability.can?(:comment_at_wall, @owner.profile.wall).should be_true
@ability.can?(:comment_at_wall, @owner_wall).should be_true
end

it "should can reply at wall owned by his friend" do
@ability.can?(:reply_at_wall, @owner.profile.wall).should be_true
debugger
@ability.can?(:reply_at_wall, @owner_wall).should be_true
end

describe "can like graffity" do
before(:each) do
@graffity = Factory(:graffity, :wall => @owner.profile.wall, :profile => @owner.profile)
@graffity = Factory(:graffity, :wall => @owner_wall, :profile => @owner.reload.profile)
end

it "because is a friend" do
Expand All @@ -91,24 +93,25 @@
end
end

describe "no related member" do
context "no related member" do
before(:each) do
@owner = Factory(:user, :login => "ownerious")
@owner = Factory(:user, :login => "ownerious").profile
@owner_wall = @owner.wall
@current_user = Factory(:user, :login => "norelatious")
@ability = Ability.new(@current_user)
end

it "should cannot comment at wall" do
@ability.cannot?(:comment_at_wall, @owner.profile.wall).should be_true
@ability.cannot?(:comment_at_wall, @owner_wall).should be_true
end

it "should cannot reply at wall" do
@ability.cannot?(:reply_at_wall, @owner.profile.wall).should be_true
@ability.cannot?(:reply_at_wall, @owner_wall).should be_true
end

describe "cannot like graffity" do
before(:each) do
@graffity = Factory(:graffity, :wall => @owner.profile.wall, :profile => @owner.profile)
@graffity = Factory(:graffity, :wall => @owner_wall, :profile => @owner)
end

it "because is not related" do
Expand All @@ -134,30 +137,30 @@

it "should cannot if the writer isn't your friend" do
@current_user = Factory(:member)
@writer = Factory(:member, :login => "writer")
@graffity = Factory(:graffity, :wall => @writer.profile.wall, :profile => @writer.profile)
@writer = Factory(:member, :login => "writer").profile
@graffity = Factory(:graffity, :wall => @writer.wall, :profile => @writer)
@ability = Ability.new(@current_user)

@ability.cannot?(:see_walltowall, @graffity).should be_true
end

it "should cannot if the owner isn't your friend" do
@current_user = Factory(:member)
@writer = Factory(:member, :login => "writer")
@owner = Factory(:member, :login => "owner")
@graffity = Factory(:graffity, :wall => @owner.profile.wall, :profile => @writer.profile)
@writer = Factory(:member, :login => "writer").profile
@owner = Factory(:member, :login => "owner").profile
@graffity = Factory(:graffity, :wall => @owner.wall, :profile => @writer)
@ability = Ability.new(@current_user)

@ability.cannot?(:see_walltowall, @graffity).should be_true
end

it "should can if logged in pass a graffity and the writer and the owner are friends of the current user" do
@current_user = Factory(:member)
@writer = Factory(:member, :login => "writer")
@owner = Factory(:member, :login => "owner")
@current_user.profile.add_friend(@writer.profile)
@current_user.profile.add_friend(@owner.profile)
@graffity = Factory(:graffity, :wall => @owner.profile.wall, :profile => @writer.profile)
@writer = Factory(:member, :login => "writer").profile
@owner = Factory(:member, :login => "owner").profile
@current_user.profile.add_friend(@writer)
@current_user.profile.add_friend(@owner)
@graffity = Factory(:graffity, :wall => @owner.wall, :profile => @writer)
@ability = Ability.new(@current_user)

@ability.can?(:see_walltowall, @graffity).should be_true
Expand Down
14 changes: 13 additions & 1 deletion spec/models/graffity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@wall.stub(:profile)
end

it "should return a graffity instance" do
it "should return a graffity" do
Graffity.build_from(@wall, nil, {}).should be_instance_of(Graffity)
end

Expand Down Expand Up @@ -55,7 +55,19 @@
graffity.comment.should == "Bazzinga!"
end

end

# Subject-ivity
describe "subject-ivity" do
subject { Graffity.new(:type_common => true) }
specify { subject.should be_common_graffity }
specify { subject.should_not be_like_graffity }
end

describe "implicit subject-ivity" do
subject { Graffity.new(:type_common => true) }
specify { should be_common_graffity }
specify { should_not be_like_graffity }
end

end
Loading

0 comments on commit 1cab4c1

Please sign in to comment.