From e201340bb2129a4bf887aafbf1a0d98c4153c12d Mon Sep 17 00:00:00 2001 From: MrZYX Date: Fri, 27 May 2011 18:32:54 +0200 Subject: [PATCH] Photo association on GUID 2/2: replace status_message_id with status_message_guid --- app/controllers/photos_controller.rb | 4 ++-- app/controllers/status_messages_controller.rb | 2 +- app/models/photo.rb | 22 +++++-------------- app/models/status_message.rb | 2 +- app/views/photos/_index.html.haml | 2 +- app/views/posts/photo.html.haml | 2 +- 6 files changed, 11 insertions(+), 23 deletions(-) diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index d4dc55983..c8f9def0c 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -125,7 +125,7 @@ class PhotosController < ApplicationController format.json{ render :nothing => true, :status => 204 } format.html do flash[:notice] = I18n.t 'photos.destroy.notice' - if photo.status_message_id + if photo.status_message_guid respond_with photo, :location => photo.status_message else respond_with photo, :location => person_photos_path(current_user.person) @@ -140,7 +140,7 @@ class PhotosController < ApplicationController def show @photo = current_user.find_visible_post_by_id(params[:id], :type => 'Photo') if @photo - @parent = StatusMessage.where(:id => @photo.status_message_id).includes(:photos).first if @photo.status_message_id + @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 diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 37f71917b..1b96d0167 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -76,7 +76,7 @@ class StatusMessagesController < ApplicationController end else if !photos.empty? - photos.update_all(:status_message_id => nil) + photos.update_all(:status_message_guid => nil) end respond_to do |format| format.js { render :json =>{:errors => @status_message.errors.full_messages}, :status => 422 } diff --git a/app/models/photo.rb b/app/models/photo.rb index 4281cb91a..f39ced116 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -13,7 +13,7 @@ class Photo < Post xml_attr :text xml_attr :status_message_guid - belongs_to :status_message + belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid attr_accessible :text, :pending validate :ownership_of_status_message @@ -22,8 +22,8 @@ class Photo < Post after_create :queue_processing_job def ownership_of_status_message - message = StatusMessage.find_by_id(self.status_message_id) - if status_message_id && message + message = StatusMessage.find_by_guid(self.status_message_guid) + if self.status_message_guid && message self.diaspora_handle == message.diaspora_handle else true @@ -61,18 +61,6 @@ class Photo < Post self.remote_photo_name = remote_path.slice(name_start + 1, remote_path.length) end - def status_message_guid - if self.status_message - self.status_message.guid - else - nil - end - end - - def status_message_guid= new_sm_guid - self.status_message= StatusMessage.where(:guid => new_sm_guid).first - end - def url(name = nil) if remote_photo_path name = name.to_s + '_' if name @@ -121,7 +109,7 @@ class Photo < Post } end - scope :on_statuses, lambda { |post_ids| - where(:status_message_id => post_ids) + scope :on_statuses, lambda { |post_guids| + where(:status_message_guid => post_guids) } end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 3f5b3b77d..aae222173 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -17,7 +17,7 @@ class StatusMessage < Post xml_name :status_message xml_attr :raw_message - has_many :photos, :dependent => :destroy + has_many :photos, :dependent => :destroy, :foreign_key => :status_message_guid, :primary_key => :guid validate :message_or_photos_present? attr_accessible :text diff --git a/app/views/photos/_index.html.haml b/app/views/photos/_index.html.haml index 9c1e42e72..bc38eeca3 100644 --- a/app/views/photos/_index.html.haml +++ b/app/views/photos/_index.html.haml @@ -17,5 +17,5 @@ #thumbnails.span-15.last - for photo in photos - = link_to (image_tag photo.url(:thumb_medium), "data-message-id" => photo.status_message_id ), photo_path(photo) + = link_to (image_tag photo.url(:thumb_medium), "data-message-guid" => photo.status_message_guid ), photo_path(photo) diff --git a/app/views/posts/photo.html.haml b/app/views/posts/photo.html.haml index 0ddc2dd16..b520c09cf 100644 --- a/app/views/posts/photo.html.haml +++ b/app/views/posts/photo.html.haml @@ -20,7 +20,7 @@ %br .span-9.last - - if @post.status_message_id + - if @post.status_message_guid #original_post_info %h4{:style=>"position:relative;"} = t('photos.show.original_post')