refactor status_message_controller#create to suck less. it still sucks

This commit is contained in:
Maxwell Salzberg 2012-01-26 23:39:10 -08:00
parent 0da5f18ad5
commit 718c4fd38c
5 changed files with 47 additions and 32 deletions

View file

@ -43,32 +43,21 @@ class StatusMessagesController < ApplicationController
def create def create
params[:status_message][:aspect_ids] = [*params[:aspect_ids]] params[:status_message][:aspect_ids] = [*params[:aspect_ids]]
normalize_public_flag! normalize_public_flag!
services = [*params[:services]].compact
# ensure services is an array since .map doesn't work on a string for ruby 1.9
params[:services] = [params[:services]] if params[:services].is_a?(String)
@status_message = current_user.build_post(:status_message, params[:status_message]) @status_message = current_user.build_post(:status_message, params[:status_message])
@status_message.attach_photos_by_ids(params[:photos])
photos = Photo.where(:id => [*params[:photos]], :diaspora_handle => current_user.person.diaspora_handle)
unless photos.empty?
@status_message.photos << photos
end
if @status_message.save if @status_message.save
# always send to all aspects if public aspects = current_user.aspects_from_ids(destination_aspect_ids)
if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects"
aspect_ids = current_user.aspects.map{|a| a.id}
else
aspect_ids = params[:aspect_ids]
end
aspects = current_user.aspects_from_ids(aspect_ids)
current_user.add_to_streams(@status_message, aspects) current_user.add_to_streams(@status_message, aspects)
receiving_services = current_user.services.where(:type => params[:services].map{|s| "Services::"+s.titleize}) if params[:services] receiving_services = current_user.services.where(:type => Service.titles(services))
current_user.dispatch_post(@status_message, :url => short_post_url(@status_message.guid), :services => receiving_services) current_user.dispatch_post(@status_message, :url => short_post_url(@status_message.guid), :services => receiving_services)
if request.env['HTTP_REFERER'].include?("people") # if this is a post coming from a profile page
flash[:notice] = t('status_messages.create.success', :names => @status_message.mentions.includes(:person => :profile).map{ |mention| mention.person.name }.join(', ')) if coming_from_profile_page? # if this is a post coming from a profile page
flash[:notice] = successful_mention_message
end end
respond_to do |format| respond_to do |format|
@ -77,10 +66,6 @@ class StatusMessagesController < ApplicationController
format.json{ render :json => @status_message.as_api_response(:backbone), :status => 201 } format.json{ render :json => @status_message.as_api_response(:backbone), :status => 201 }
end end
else else
unless photos.empty?
photos.update_all(:status_message_guid => nil)
end
respond_to do |format| respond_to do |format|
format.json { render :nothing, :status => 403 } format.json { render :nothing, :status => 403 }
format.html { redirect_to :back } format.html { redirect_to :back }
@ -88,23 +73,32 @@ class StatusMessagesController < ApplicationController
end end
end end
def destination_aspect_ids
if params[:status_message][:public] || params[:status_message][:aspect_ids].first == "all_aspects"
current_user.aspect_ids
else
params[:aspect_ids]
end
end
def successful_mention_message
t('status_messages.create.success', :names => @status_message.mentioned_people_names)
end
def coming_from_profile_page?
request.env['HTTP_REFERER'].include?("people")
end
def normalize_public_flag! def normalize_public_flag!
# mobile || desktop conditions # mobile || desktop conditions
public_flag = (params[:status_message][:aspect_ids] && params[:status_message][:aspect_ids].first == 'public') || params[:status_message][:public] sm = params[:status_message]
public_flag = (sm[:aspect_ids] && sm[:aspect_ids].first == 'public') || sm[:public]
public_flag.to_s.match(/(true)|(on)/) ? public_flag = true : public_flag = false public_flag.to_s.match(/(true)|(on)/) ? public_flag = true : public_flag = false
params[:status_message][:public] = public_flag params[:status_message][:public] = public_flag
public_flag public_flag
end end
helper_method :comments_expanded
def comments_expanded
true
end
def remove_getting_started def remove_getting_started
if current_user.getting_started == true current_user.disable_getting_started
current_user.update_attributes(:getting_started => false)
end
true
end end
end end

View file

@ -8,6 +8,7 @@ class Photo < ActiveRecord::Base
include Diaspora::Commentable include Diaspora::Commentable
include Diaspora::Shareable include Diaspora::Shareable
# NOTE API V1 to be extracted # NOTE API V1 to be extracted
acts_as_api acts_as_api
api_accessible :backbone do |t| api_accessible :backbone do |t|
@ -32,6 +33,7 @@ class Photo < ActiveRecord::Base
xml_attr :status_message_guid xml_attr :status_message_guid
belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid
validates_associated :status_message
attr_accessible :text, :pending attr_accessible :text, :pending
validate :ownership_of_status_message validate :ownership_of_status_message

View file

@ -9,6 +9,10 @@ class Service < ActiveRecord::Base
validates_uniqueness_of :uid, :scope => :type validates_uniqueness_of :uid, :scope => :type
has_many :service_users, :dependent => :destroy has_many :service_users, :dependent => :destroy
def self.titles(service_strings)
service_strings.map{|s| "Services::#{s.titleize}"}
end
def public_message(post, length, url = "") def public_message(post, length, url = "")
url = "" if post.respond_to?(:photos) && post.photos.count == 0 url = "" if post.respond_to?(:photos) && post.photos.count == 0
space_for_url = url.blank? ? 0 : (url.length + 1) space_for_url = url.blank? ? 0 : (url.length + 1)
@ -17,6 +21,7 @@ class Service < ActiveRecord::Base
return truncated return truncated
end end
def profile_photo_url def profile_photo_url
nil nil
end end

View file

@ -66,6 +66,12 @@ class StatusMessage < Post
write_attribute(:text, text) write_attribute(:text, text)
end end
def attach_photos_by_ids(photo_ids)
return [] unless photo_ids.present?
self.photos << Photo.where(:id => photo_ids, :author_id => self.author_id).all
end
def nsfw? def nsfw?
self.raw_message.match(/#nsfw/i) self.raw_message.match(/#nsfw/i)
end end
@ -104,6 +110,10 @@ class StatusMessage < Post
end end
end end
def mentioned_people_names
self.mentioned_people.map(&:name).join(', ')
end
def create_mentions def create_mentions
mentioned_people_from_string.each do |person| mentioned_people_from_string.each do |person|
self.mentions.create(:person => person) self.mentions.create(:person => person)

View file

@ -191,6 +191,10 @@ class User < ActiveRecord::Base
end end
end end
def disable_getting_started
self.update_attribute(:getting_started, false) if self.getting_started?
end
def set_current_language def set_current_language
self.language = I18n.locale.to_s if self.language.blank? self.language = I18n.locale.to_s if self.language.blank?
end end