tests for data methods

This commit is contained in:
Maxwell Salzberg 2011-06-16 15:47:16 -07:00
parent 86f4f983f6
commit 41e8a32b28
8 changed files with 168 additions and 59 deletions

View file

@ -4,7 +4,6 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
has_mobile_fu has_mobile_fu
helper_method :all_aspects
protect_from_forgery :except => :receive protect_from_forgery :except => :receive
before_filter :ensure_http_referer_is_set before_filter :ensure_http_referer_is_set
before_filter :set_header_data, :except => [:create, :update] before_filter :set_header_data, :except => [:create, :update]
@ -17,6 +16,8 @@ class ApplicationController < ActionController::Base
inflection_method :grammatical_gender => :gender inflection_method :grammatical_gender => :gender
helper_method :all_aspects, :object_aspect_ids
def ensure_http_referer_is_set def ensure_http_referer_is_set
request.env['HTTP_REFERER'] ||= '/aspects' request.env['HTTP_REFERER'] ||= '/aspects'
end end
@ -28,8 +29,14 @@ class ApplicationController < ActionController::Base
@notification_count = Notification.for(current_user, :unread =>true).count @notification_count = Notification.for(current_user, :unread =>true).count
@unread_message_count = ConversationVisibility.sum(:unread, :conditions => "person_id = #{current_user.person.id}") @unread_message_count = ConversationVisibility.sum(:unread, :conditions => "person_id = #{current_user.person.id}")
end end
@object_aspect_ids = [] end
end
##helpers
def object_aspect_ids
if user_signed_in?
@object_aspect_ids ||= []
end end
end end

View file

@ -5,8 +5,11 @@
class PhotosController < ApplicationController class PhotosController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
helper_method :object_aspect_ids, :parent, :photo, :additional_photos, :next_photo, :previous_photo, :ownership
respond_to :html, :json respond_to :html, :json
def index def index
@post_type = :photos @post_type = :photos
@person = Person.find_by_id(params[:person_id]) @person = Person.find_by_id(params[:person_id])
@ -138,30 +141,8 @@ class PhotosController < ApplicationController
end end
def show def show
@photo = current_user.find_visible_post_by_id(params[:id], :type => 'Photo') if photo
if @photo respond_with photo
@parent = StatusMessage.where(:guid => @photo.status_message_guid).includes(:photos).first if @photo.status_message_guid
#if photo is not an attachment, fetch comments for self
if @parent
@additional_photos = @photo.status_message.photos
if @additional_photos
@next_photo = @additional_photos[@additional_photos.index(@photo)+1]
@prev_photo = @additional_photos[@additional_photos.index(@photo)-1]
@next_photo ||= @additional_photos.first
end
else
@parent = @photo
end
@object_aspect_ids = []
if @parent_aspects = @parent.aspects.where(:user_id => current_user.id)
@object_aspect_ids = @parent_aspects.map{|a| a.id}
end
@ownership = current_user.owns? @photo
respond_with @photo
else else
begin begin
redirect_to :back redirect_to :back
@ -169,7 +150,6 @@ class PhotosController < ApplicationController
redirect_to aspects_path redirect_to aspects_path
end end
end end
end end
def edit def edit
@ -200,6 +180,45 @@ class PhotosController < ApplicationController
end end
end end
# helpers
# used on the show page to show which aspects are selected
def object_aspect_ids
if params[:action] == 'show' && parent_aspects = parent.aspects.where(:user_id => current_user.id).all
@object_aspect_ids ||= parent_aspects.map!{|a| a.id}
else
super
end
end
def ownership
@ownership ||= current_user.owns? photo
end
def parent
@parent ||= StatusMessage.where(:guid => photo.status_message_guid).includes(:photos).first if photo.status_message_guid
@parent ||= photo
end
def photo
@photo ||= current_user.find_visible_post_by_id(params[:id], :type => 'Photo')
@photo
end
def additional_photos
if photo.status_message_guid?
@additional_photos ||= photo.status_message.photos
end
end
def next_photo
@next_photo ||= additional_photos[additional_photos.index(photo)+1]
@next_photo ||= additional_photos.first
end
def previous_photo
@previous_photo ||= additional_photos[additional_photos.index(photo)-1]
end
private private

View file

@ -0,0 +1,7 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module PhotosHelper
end

View file

@ -52,7 +52,7 @@
= link_to t('all_aspects'), aspects_path, :class => 'home_selector' = link_to t('all_aspects'), aspects_path, :class => 'home_selector'
- for aspect in all_aspects - for aspect in all_aspects
%li{:data=>{:guid=>aspect.id}, :class => ("selected" if @object_aspect_ids.include?(aspect.id))} %li{:data=>{:guid=>aspect.id}, :class => ("selected" if object_aspect_ids.include?(aspect.id))}
= link_for_aspect(aspect, :class => 'aspect_selector name', :title => t('contacts', :count => aspect.contacts.size)) = link_for_aspect(aspect, :class => 'aspect_selector name', :title => t('contacts', :count => aspect.contacts.size))
%li %li

View file

@ -7,59 +7,59 @@
.span-16.append-4.prepend-4.last .span-16.append-4.prepend-4.last
#photo_controls #photo_controls
-if @additional_photos && @additional_photos.length > 1 -if additional_photos && additional_photos.length > 1
.right .right
=link_to "← #{t('previous')}", @prev_photo, :rel => 'prefetch', :id => 'photo_show_left' =link_to "← #{t('previous')}", previous_photo, :rel => 'prefetch', :id => 'photo_show_left'
\/ \/
=link_to "#{t('next')} →", @next_photo, :rel => 'prefetch', :id => 'photo_show_right' =link_to "#{t('next')} →", next_photo, :rel => 'prefetch', :id => 'photo_show_right'
#original_post_info #original_post_info
= render 'shared/author_info', :person => @photo.author, :post => @photo = render 'shared/author_info', :person => photo.author, :post => photo
#photo_container #photo_container
#show_photo{:data=>{:guid=>@photo.id}} #show_photo{:data=>{:guid=>photo.id}}
-if @ownership -if ownership
= image_tag 'ajax-loader.gif', :id => "photo_spinner", :class => "hidden" = image_tag 'ajax-loader.gif', :id => "photo_spinner", :class => "hidden"
= image_tag @photo.url(:scaled_full) = image_tag photo.url(:scaled_full)
-else -else
= image_tag @photo.url(:scaled_full) = image_tag photo.url(:scaled_full)
#caption #caption
= @photo.text = photo.text
- if @ownership - if ownership
.photo_options{:data=>{:actor=>"#{@photo.author.owner.id}", :actor_person => "#{@photo.author.id}", :image_url => "#{@photo.url(:thumb_large)}"}} .photo_options{:data=>{:actor=>"#{photo.author.owner.id}", :actor_person => "#{photo.author.id}", :image_url => "#{photo.url(:thumb_large)}"}}
= link_to t('.make_profile_photo'), photo_make_profile_photo_path(@photo), :method => :put, :remote => true, :class => 'make_profile_photo' = link_to t('.make_profile_photo'), photo_make_profile_photo_path(photo), :method => :put, :remote => true, :class => 'make_profile_photo'
| |
= link_to t('.edit'), '#', :id => "edit_photo_toggle" = link_to t('.edit'), '#', :id => "edit_photo_toggle"
%br %br
%hr %hr
- if @photo.status_message - if photo.status_message
.span-8.last .span-8.last
%p %p
= markdownify(@photo.status_message.text) = markdownify(photo.status_message.text)
%span{:style=>'font-size:smaller'} %span{:style=>'font-size:smaller'}
=link_to t('.collection_permalink'), @photo.status_message =link_to t('.collection_permalink'), photo.status_message
%br %br
%br %br
.span-7.prepend-1 .span-7.prepend-1
.show_photo_attachments .show_photo_attachments
- for photo in @additional_photos - for photo in additional_photos
= link_to (image_tag photo.url(:thumb_small), :class => 'thumb_small'), object_path(photo) = link_to (image_tag photo.url(:thumb_small), :class => 'thumb_small'), object_path(photo)
#photo_edit_options #photo_edit_options
%h4= t('.edit_delete_photo') %h4= t('.edit_delete_photo')
%p %p
= form_for @photo, :remote => true do |p| = form_for photo, :remote => true do |p|
= p.text_field :text, :value => @photo.text = p.text_field :text, :value => photo.text
= p.submit t('.update_photo') = p.submit t('.update_photo')
%p %p
= button_to t('.delete_photo'), @photo, :confirm => t('are_you_sure'), :method => :delete = button_to t('.delete_photo'), photo, :confirm => t('are_you_sure'), :method => :delete
.span-16.prepend-4.append-4.last .span-16.prepend-4.append-4.last
#photo_stream.stream.show #photo_stream.stream.show
@ -70,7 +70,7 @@
/ | / |
/ = link_to t('shared.stream_element.dislike'), likes_path(:positive => 'false', :post_id => @parent.id), :method => :post, :class => "dislike_it", :remote => true / = link_to t('shared.stream_element.dislike'), likes_path(:positive => 'false', :post_id => @parent.id), :method => :post, :class => "dislike_it", :remote => true
%div{:data=>{:guid=>@parent.id}} %div{:data=>{:guid=>parent.id}}
.likes_container .likes_container
= render "likes/likes", :post_id => @parent.id, :likes => @parent.likes, :dislikes => @parent.dislikes = render "likes/likes", :post_id => parent.id, :likes => parent.likes, :dislikes => parent.dislikes
= render "comments/comments", :post => @parent, :comments => @parent.comments, :always_expanded => true = render "comments/comments", :post => parent, :comments => parent.comments, :always_expanded => true

View file

@ -4,14 +4,14 @@
#show_content.photo #show_content.photo
= image_tag @photo.url(:scaled_full) = image_tag photo.url(:scaled_full)
-if @additional_photos && @additional_photos.length > 1 -if additional_photos && additional_photos.length > 1
#photo_controls #photo_controls
%table %table
%tr %tr
%td %td
=link_to "←", @prev_photo, :rel => 'prefetch', :class => 'arrow' =link_to "←", previous_photo, :rel => 'prefetch', :class => 'arrow'
%td{:width => '100%'} %td{:width => '100%'}
%td %td
=link_to "→", @next_photo, :rel => 'prefetch', :class => 'arrow' =link_to "→", next_photo, :rel => 'prefetch', :class => 'arrow'

View file

@ -60,7 +60,7 @@ describe PhotosController do
it "assigns the photo" do it "assigns the photo" do
assigns[:photo].should == @alices_photo assigns[:photo].should == @alices_photo
assigns[:ownership].should be_true @controller.ownership.should be_true
end end
end end
@ -75,7 +75,7 @@ describe PhotosController do
it "assigns the photo" do it "assigns the photo" do
assigns[:photo].should == @bobs_photo assigns[:photo].should == @bobs_photo
assigns[:ownership].should be_false @controller.ownership.should be_false
end end
end end
@ -111,7 +111,7 @@ describe PhotosController do
it "assigns the photo" do it "assigns the photo" do
assigns[:photo].should == @photo assigns[:photo].should == @photo
assigns[:ownership].should be_false @controller.ownership.should be_false
end end
end end
end end
@ -183,4 +183,72 @@ describe PhotosController do
response.code.should == "422" response.code.should == "422"
end end
end end
describe 'data helpers' do
describe '.object_aspect_ids' do
it 'on show, assigns object aspect ids' do
get :show, :id => @alices_photo.id
@controller.object_aspect_ids.should == [alice.aspects.first.id]
end
it 'on index, it is empty' do
get :index, :person_id => alice.person.id
@controller.object_aspect_ids.should == []
end
end
describe '.ownership' do
it 'is true if current user owns the photo' do
get :show, :id => @alices_photo.id
@controller.ownership.should be_true
end
it 'is true if current user owns the photo' do
get :show, :id => @bobs_photo.id
@controller.ownership.should be_false
end
end
describe 'parent' do
it 'grabs the status message of the photo if a parent exsists' do
sm = alice.post(:status_message, :text => 'yes', :to => alice.aspects.first)
@alices_photo.status_message = sm
@alices_photo.save
get :show, :id => @alices_photo.id
@controller.parent.id.should == sm.id
end
it 'uses the photo if no status_message exsists' do
get :show, :id => @alices_photo.id
@controller.parent.id.should == @alices_photo.id
end
end
describe '.photo' do
it 'returns a visible photo, based on the :id param' do
get :show, :id => @alices_photo.id
@controller.photo.id.should == @alices_photo.id
end
end
describe '.additonal_photos' do
it 'finds all of a parent status messages photos' do
sm = alice.post(:status_message, :text => 'yes', :to => alice.aspects.first)
@alices_photo.status_message = sm
@alices_photo.save
get :show, :id => @alices_photo.id
@controller.additional_photos.should include(@alices_photo)
end
end
describe '.next_photo' do
end
describe '.previous_photo' do
end
end
end end

View file

@ -0,0 +1,8 @@
# Copyright (c) 2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
#require 'spec_helper'
describe PhotosHelper do
end