From 41e8a32b28506700ab1b5f67a8c7979c7fd93ff0 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Thu, 16 Jun 2011 15:47:16 -0700 Subject: [PATCH] tests for data methods --- app/controllers/application_controller.rb | 13 +++- app/controllers/photos_controller.rb | 69 +++++++++++++------- app/helpers/photos_helper.rb | 7 ++ app/views/layouts/_header.html.haml | 2 +- app/views/photos/show.html.haml | 44 ++++++------- app/views/photos/show.mobile.haml | 8 +-- spec/controllers/photos_controller_spec.rb | 76 ++++++++++++++++++++-- spec/helpers/photos_helper_spec.rb | 8 +++ 8 files changed, 168 insertions(+), 59 deletions(-) create mode 100644 app/helpers/photos_helper.rb create mode 100644 spec/helpers/photos_helper_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 47172314e..f7506e28a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,6 @@ class ApplicationController < ActionController::Base has_mobile_fu - helper_method :all_aspects protect_from_forgery :except => :receive before_filter :ensure_http_referer_is_set before_filter :set_header_data, :except => [:create, :update] @@ -17,6 +16,8 @@ class ApplicationController < ActionController::Base inflection_method :grammatical_gender => :gender + helper_method :all_aspects, :object_aspect_ids + def ensure_http_referer_is_set request.env['HTTP_REFERER'] ||= '/aspects' end @@ -28,8 +29,14 @@ class ApplicationController < ActionController::Base @notification_count = Notification.for(current_user, :unread =>true).count @unread_message_count = ConversationVisibility.sum(:unread, :conditions => "person_id = #{current_user.person.id}") end - @object_aspect_ids = [] - + end + end + + + ##helpers + def object_aspect_ids + if user_signed_in? + @object_aspect_ids ||= [] end end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index c8f9def0c..2807b75aa 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -5,8 +5,11 @@ class PhotosController < ApplicationController before_filter :authenticate_user! + helper_method :object_aspect_ids, :parent, :photo, :additional_photos, :next_photo, :previous_photo, :ownership + respond_to :html, :json + def index @post_type = :photos @person = Person.find_by_id(params[:person_id]) @@ -138,30 +141,8 @@ class PhotosController < ApplicationController end def show - @photo = current_user.find_visible_post_by_id(params[:id], :type => 'Photo') - if @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 + if photo + respond_with photo else begin redirect_to :back @@ -169,7 +150,6 @@ class PhotosController < ApplicationController redirect_to aspects_path end end - end def edit @@ -200,6 +180,45 @@ class PhotosController < ApplicationController 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 diff --git a/app/helpers/photos_helper.rb b/app/helpers/photos_helper.rb new file mode 100644 index 000000000..563e4b49d --- /dev/null +++ b/app/helpers/photos_helper.rb @@ -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 diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index 5dddbce54..7ea3d7bbd 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -52,7 +52,7 @@ = link_to t('all_aspects'), aspects_path, :class => 'home_selector' - 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)) %li diff --git a/app/views/photos/show.html.haml b/app/views/photos/show.html.haml index be0119e37..846463d63 100644 --- a/app/views/photos/show.html.haml +++ b/app/views/photos/show.html.haml @@ -7,59 +7,59 @@ .span-16.append-4.prepend-4.last #photo_controls - -if @additional_photos && @additional_photos.length > 1 + -if additional_photos && additional_photos.length > 1 .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 - = render 'shared/author_info', :person => @photo.author, :post => @photo + = render 'shared/author_info', :person => photo.author, :post => photo #photo_container - #show_photo{:data=>{:guid=>@photo.id}} - -if @ownership + #show_photo{:data=>{:guid=>photo.id}} + -if ownership = image_tag 'ajax-loader.gif', :id => "photo_spinner", :class => "hidden" - = image_tag @photo.url(:scaled_full) + = image_tag photo.url(:scaled_full) -else - = image_tag @photo.url(:scaled_full) + = image_tag photo.url(:scaled_full) #caption - = @photo.text + = photo.text - - if @ownership - .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' + - if ownership + .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('.edit'), '#', :id => "edit_photo_toggle" %br %hr - - if @photo.status_message + - if photo.status_message .span-8.last %p - = markdownify(@photo.status_message.text) + = markdownify(photo.status_message.text) %span{:style=>'font-size:smaller'} - =link_to t('.collection_permalink'), @photo.status_message + =link_to t('.collection_permalink'), photo.status_message %br %br .span-7.prepend-1 .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) #photo_edit_options %h4= t('.edit_delete_photo') %p - = form_for @photo, :remote => true do |p| - = p.text_field :text, :value => @photo.text + = form_for photo, :remote => true do |p| + = p.text_field :text, :value => photo.text = p.submit t('.update_photo') %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 #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 - %div{:data=>{:guid=>@parent.id}} + %div{:data=>{:guid=>parent.id}} .likes_container - = 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 "likes/likes", :post_id => parent.id, :likes => parent.likes, :dislikes => parent.dislikes + = render "comments/comments", :post => parent, :comments => parent.comments, :always_expanded => true diff --git a/app/views/photos/show.mobile.haml b/app/views/photos/show.mobile.haml index 4aebfea69..d145ac487 100755 --- a/app/views/photos/show.mobile.haml +++ b/app/views/photos/show.mobile.haml @@ -4,14 +4,14 @@ #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 %table %tr %td - =link_to "←", @prev_photo, :rel => 'prefetch', :class => 'arrow' + =link_to "←", previous_photo, :rel => 'prefetch', :class => 'arrow' %td{:width => '100%'} %td - =link_to "→", @next_photo, :rel => 'prefetch', :class => 'arrow' + =link_to "→", next_photo, :rel => 'prefetch', :class => 'arrow' diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index e4069da85..e77c0c2e7 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -60,7 +60,7 @@ describe PhotosController do it "assigns the photo" do assigns[:photo].should == @alices_photo - assigns[:ownership].should be_true + @controller.ownership.should be_true end end @@ -75,7 +75,7 @@ describe PhotosController do it "assigns the photo" do assigns[:photo].should == @bobs_photo - assigns[:ownership].should be_false + @controller.ownership.should be_false end end @@ -111,7 +111,7 @@ describe PhotosController do it "assigns the photo" do assigns[:photo].should == @photo - assigns[:ownership].should be_false + @controller.ownership.should be_false end end end @@ -183,4 +183,72 @@ describe PhotosController do response.code.should == "422" end end -end \ No newline at end of file + + + 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 diff --git a/spec/helpers/photos_helper_spec.rb b/spec/helpers/photos_helper_spec.rb new file mode 100644 index 000000000..72d640811 --- /dev/null +++ b/spec/helpers/photos_helper_spec.rb @@ -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