From 1cab4c1f96959dcfdbc5a4e727973eb66eed2e33 Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Sun, 14 Feb 2010 15:15:36 +0100 Subject: [PATCH] Some updates --- app/controllers/walls_controller.rb | 13 ++++++ app/helpers/graffities_helper.rb | 14 ++++++ app/models/ability.rb | 20 ++++++--- app/models/graffity.rb | 6 +-- .../member/graffities/_graffity.html.erb | 5 ++- config/desert_routes.rb | 2 +- locale/tog_wall.yml | 2 +- locale/tog_wall_es.yml | 2 +- locale/tog_wall_pt-BR.yml | 2 +- .../member/graffities_controller_spec.rb | 20 ++++----- .../member/walls_controller_spec.rb | 13 +++--- spec/models/ability_spec.rb | 45 ++++++++++--------- spec/models/graffity_spec.rb | 14 +++++- spec/models/wall_spec.rb | 40 +++++++++++------ 14 files changed, 129 insertions(+), 69 deletions(-) diff --git a/app/controllers/walls_controller.rb b/app/controllers/walls_controller.rb index 43b8686..6be6210 100644 --- a/app/controllers/walls_controller.rb +++ b/app/controllers/walls_controller.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/app/helpers/graffities_helper.rb b/app/helpers/graffities_helper.rb index e92c109..edb6367 100644 --- a/app/helpers/graffities_helper.rb +++ b/app/helpers/graffities_helper.rb @@ -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 \ No newline at end of file diff --git a/app/models/ability.rb b/app/models/ability.rb index ac09587..3dbb8ee 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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 diff --git a/app/models/graffity.rb b/app/models/graffity.rb index 9da937a..78cc89f 100644 --- a/app/models/graffity.rb +++ b/app/models/graffity.rb @@ -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 @@ -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 diff --git a/app/views/member/graffities/_graffity.html.erb b/app/views/member/graffities/_graffity.html.erb index aaba712..76b8099 100644 --- a/app/views/member/graffities/_graffity.html.erb +++ b/app/views/member/graffities/_graffity.html.erb @@ -10,8 +10,9 @@
<%= graffity_created_at_in_words(graffity.created_at) %> - <%= 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) %>
<%= render :partial => "member/graffities/like_resume", :locals => {:likes => graffity.likes, :graffity => graffity} unless graffity.likes.empty? %> diff --git a/config/desert_routes.rb b/config/desert_routes.rb index 35e67d1..2084d36 100644 --- a/config/desert_routes.rb +++ b/config/desert_routes.rb @@ -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 diff --git a/locale/tog_wall.yml b/locale/tog_wall.yml index 386c090..038d406 100644 --- a/locale/tog_wall.yml +++ b/locale/tog_wall.yml @@ -27,7 +27,7 @@ en: site: comment: to_comment: Comment - to_like : Like + to_like: Like member: graffities: form: diff --git a/locale/tog_wall_es.yml b/locale/tog_wall_es.yml index 30ee915..110d8e7 100644 --- a/locale/tog_wall_es.yml +++ b/locale/tog_wall_es.yml @@ -27,7 +27,7 @@ es: site: comment: to_comment: Comentar - to_like : Me gusta + to_like: Me gusta member: graffities: form: diff --git a/locale/tog_wall_pt-BR.yml b/locale/tog_wall_pt-BR.yml index 2932d19..d083e98 100644 --- a/locale/tog_wall_pt-BR.yml +++ b/locale/tog_wall_pt-BR.yml @@ -27,7 +27,7 @@ pt-BR: site: comment: to_comment: Comment - to_like : Like + to_like: Like member: graffities: form: diff --git a/spec/controllers/member/graffities_controller_spec.rb b/spec/controllers/member/graffities_controller_spec.rb index 4658d10..b3a11fb 100644 --- a/spec/controllers/member/graffities_controller_spec.rb +++ b/spec/controllers/member/graffities_controller_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/controllers/member/walls_controller_spec.rb b/spec/controllers/member/walls_controller_spec.rb index 86f670e..ceaa281 100644 --- a/spec/controllers/member/walls_controller_spec.rb +++ b/spec/controllers/member/walls_controller_spec.rb @@ -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 diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 2a9497b..5604db8 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -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 @@ -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 @@ -63,9 +63,10 @@ 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) @@ -73,16 +74,17 @@ 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 @@ -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 @@ -134,8 +137,8 @@ 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 @@ -143,9 +146,9 @@ 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 @@ -153,11 +156,11 @@ 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 diff --git a/spec/models/graffity_spec.rb b/spec/models/graffity_spec.rb index ae32963..d4e98b4 100644 --- a/spec/models/graffity_spec.rb +++ b/spec/models/graffity_spec.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/spec/models/wall_spec.rb b/spec/models/wall_spec.rb index af80c59..e230981 100644 --- a/spec/models/wall_spec.rb +++ b/spec/models/wall_spec.rb @@ -4,7 +4,7 @@ # it { should have_many(:graffities) } # it { should belong_to(:profile) } - + it "should be able to create two graffities as root in the same wall" do @owner = Factory(:member).profile @wall = @owner.wall @@ -12,7 +12,8 @@ @graffity1 = Factory(:graffity, :profile => @owner, :wall => @wall) @graffity2 = Factory(:graffity, :profile => @owner, :wall => @wall) - @owner.wall.graffities.should have(2).items + # @owner.wall.graffities.should have(2).items + @owner.wall.should have(2).graffities @owner.wall.graffities.each {|g| g.should be_root} end @@ -28,31 +29,31 @@ @graffity4 = Factory(:graffity, :profile => @friend, :wall => @friend.wall) end - it "passing one wall should find all graffities in that wall" do + it "passing one wall find all graffities in that wall" do graffities = Wall.get_graffities_for(@wall) @wall.graffities.each do |graffity| graffities.should include(graffity) end end - it "passing an array of walls should find all graffities in these walls" do + it "passing an array of walls find all graffities in these walls" do graffities = Wall.get_graffities_for([@wall, @friend.wall]) - [@wall.graffities, @friend.wall.graffities].flatten.map(&:id).each do |graffity_id| - graffities.map(&:id).should include(graffity_id) + [@wall.graffities, @friend.wall.graffities].flatten.each do |graffity| + graffities.should include(graffity) end end - it "passing one wall and after option should find graffities with id higher than the specified in the after option" do + it "passing one wall and the option after find graffities with id higher than the specified in the option after" do graffities = Wall.get_graffities_for(@wall, :after => @graffity1.id) graffities.each do |graffity| - graffity.id.should >= @graffity1.id + graffity.id.should be > (@graffity1.id) # > @graffity1.id end end - it "passing one wall and after option should find graffities with id lower than the specified in the after option" do + it "passing one wall and the option before find graffities with id lower than the specified in the option before" do graffities = Wall.get_graffities_for(@wall, :before => @graffity4) graffities.each do |graffity| - graffity.id.should <= @graffity4.id + graffity.id.should be < (@graffity4.id) # < @graffity4.id end end @@ -67,24 +68,35 @@ Wall.get_graffities_for(@wall) end - it "passing a limit option should override the default limit" do + it "passing a limit option override the default limit" do Graffity.should_receive(:find).with(:all, hash_including(:limit => 1)) Wall.get_graffities_for(@wall, :limit => 1) end end describe "#scoped_graffities_for" do - it "without passing an option should not set an id boundary" do + it "without passing an option not set an id boundary" do Wall.scoped_graffities_for([]).proxy_options.should == {:conditions => []} end - it "passing after option should set a lower limiit" do + it "passing after option set a lower limiit" do Wall.scoped_graffities_for([], :after => 1).proxy_options.should == {:conditions => ["id > ?", 1]} end - it "passing before option should set a higher limit" do + it "passing before option set a higher limit" do Wall.scoped_graffities_for([], :before => 2).proxy_options.should == {:conditions => ["id < ?", 2]} end end + # Generated descriptions! + describe "after add two graffities" do + before(:each) do + @owner = Factory(:member).profile + @wall = @owner.wall + @graffity1 = Factory(:graffity, :profile => @owner, :wall => @wall) + @graffity2 = Factory(:graffity, :profile => @owner, :wall => @wall) + end + specify { @wall.should have(2).graffities } # autogenerated descriptions + end + end \ No newline at end of file