Skip to content

Commit

Permalink
some refactoring regarding the law of demeter
Browse files Browse the repository at this point in the history
  • Loading branch information
jhass committed Sep 23, 2012
1 parent f0619db commit e70e48d
Show file tree
Hide file tree
Showing 67 changed files with 150 additions and 115 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def redirect_unless_admin

def set_grammatical_gender
if (user_signed_in? && I18n.inflector.inflected_locale?)
gender = current_user.profile.gender.to_s.tr('!()[]"\'`*=|/\#.,-:', '').downcase
gender = current_user.gender.to_s.tr('!()[]"\'`*=|/\#.,-:', '').downcase
unless gender.empty?
i_langs = I18n.inflector.inflected_locales(:gender)
i_langs.delete I18n.locale
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class ConversationsController < ApplicationController

def index
@conversations = Conversation.joins(:conversation_visibilities).where(
:conversation_visibilities => {:person_id => current_user.person.id}).paginate(
:conversation_visibilities => {:person_id => current_user.person_id}).paginate(
:page => params[:page], :per_page => 15, :order => 'updated_at DESC')

@visibilities = ConversationVisibility.where(:person_id => current_user.person.id).paginate(
@visibilities = ConversationVisibility.where(:person_id => current_user.person_id).paginate(
:page => params[:page], :per_page => 15, :order => 'updated_at DESC')

@unread_counts = {}
Expand All @@ -18,7 +18,7 @@ def index
@conversations.each { |c| @authors[c.id] = c.last_author }

@conversation = Conversation.joins(:conversation_visibilities).where(
:conversation_visibilities => {:person_id => current_user.person.id, :conversation_id => params[:conversation_id]}).first
:conversation_visibilities => {:person_id => current_user.person_id, :conversation_id => params[:conversation_id]}).first

respond_with do |format|
format.html
Expand All @@ -31,7 +31,7 @@ def create
contact.person_id
end

params[:conversation][:participant_ids] = person_ids | [current_user.person.id]
params[:conversation][:participant_ids] = person_ids | [current_user.person_id]
params[:conversation][:author] = current_user.person
message_text = params[:conversation].delete(:text)
params[:conversation][:messages_attributes] = [ {:author => current_user.person, :text => message_text }]
Expand All @@ -52,7 +52,7 @@ def create

def show
if @conversation = Conversation.joins(:conversation_visibilities).where(:id => params[:id],
:conversation_visibilities => {:person_id => current_user.person.id}).first
:conversation_visibilities => {:person_id => current_user.person_id}).first
if @visibility = ConversationVisibility.where(:conversation_id => params[:id], :person_id => current_user.person.id).first
@visibility.unread = 0
@visibility.save
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MessagesController < ApplicationController

def create
cnv = Conversation.joins(:conversation_visibilities).where(:id => params[:conversation_id],
:conversation_visibilities => {:person_id => current_user.person.id}).first
:conversation_visibilities => {:person_id => current_user.person_id}).first

if cnv
message = Message.new(:conversation_id => cnv.id, :text => params[:message][:text], :author => current_user.person)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def create
end

def make_profile_photo
author_id = current_user.person.id
author_id = current_user.person_id
@photo = Photo.where(:id => params[:photo_id], :author_id => author_id).first

if @photo
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def update
@profile_attrs[:nsfw] ||= false

if params[:photo_id]
@profile_attrs[:photo] = Photo.where(:author_id => current_user.person.id, :id => params[:photo_id]).first
@profile_attrs[:photo] = Photo.where(:author_id => current_user.person_id, :id => params[:photo_id]).first
end

if current_user.update_profile(@profile_attrs)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/reshares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def create
@reshare = current_user.build_post(:reshare, :root_guid => params[:root_guid])
if @reshare.save
current_user.add_to_streams(@reshare, current_user.aspects)
current_user.dispatch_post(@reshare, :url => post_url(@reshare), :additional_subscribers => @reshare.root.author)
current_user.dispatch_post(@reshare, :url => post_url(@reshare), :additional_subscribers => @reshare.root_author)
end

render :json => ExtremePostPresenter.new(@reshare, current_user), :status => 201
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def create
current_user.services << service

if service.persisted?
fetch_photo = current_user.person.profile[:image_url].blank?
fetch_photo = current_user.profile[:image_url].blank?

current_user.update_profile(current_user.person.profile.from_omniauth_hash(user))
current_user.update_profile(current_user.profile.from_omniauth_hash(user))
Resque.enqueue(Jobs::FetchProfilePhoto, current_user.id, service.id, user["image"]) if fetch_photo

flash[:notice] = I18n.t 'services.create.success'
Expand All @@ -44,7 +44,7 @@ def create

if existing_service = Service.where(:type => service.type.to_s, :uid => service.uid).first
flash[:error] << I18n.t('services.create.already_authorized',
:diaspora_id => existing_service.user.person.profile.diaspora_handle,
:diaspora_id => existing_service.user.profile.diaspora_handle,
:service_name => provider.camelize )
end
end
Expand All @@ -65,4 +65,4 @@ def destroy
redirect_to services_url
end

end
end
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def public
if @user = User.find_by_username(params[:username])
respond_to do |format|
format.atom do
@posts = StatusMessage.where(:author_id => @user.person.id, :public => true).order('created_at DESC').limit(25)
@posts = StatusMessage.where(:author_id => @user.person_id, :public => true).order('created_at DESC').limit(25)
end

format.any { redirect_to person_path(@user.person) }
Expand Down Expand Up @@ -140,7 +140,7 @@ def user_photo
username = params[:username].split('@')[0]
user = User.find_by_username(username)
if user.present?
redirect_to user.profile.image_url
redirect_to user.image_url
else
render :nothing => true, :status => 404
end
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/contacts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ def contact_aspect_dropdown(contact)
:action => 'destroy',
:id => 42,
:aspect_id => @aspect.id,
:person_id => contact.person.id
:person_id => contact.person_id
},
:title => t('.remove_person_from_aspect', :person_name => contact.person.first_name, :aspect_name => @aspect.name),
:title => t('.remove_person_from_aspect', :person_name => contact.person_first_name, :aspect_name => @aspect.name),
:method => 'delete')

else
Expand All @@ -17,4 +17,4 @@ def contact_aspect_dropdown(contact)
:current_user => current_user }
end
end
end
end
2 changes: 1 addition & 1 deletion app/helpers/invitation_codes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def invite_welcome_message
content_tag(:div, :class => 'media well') do
person_image_link(invite.user.person, :class => 'img') +
content_tag(:div, :class => 'bd') do
I18n.translate('invitation_codes.excited', :name => invite.user.name)
I18n.translate('invitation_codes.excited', :name => invite.user_name)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/mobile_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def mobile_reshare_icon(post)
if (post.public? || reshare?(post)) && (user_signed_in? && post.author != current_user.person)
root = reshare?(post) ? post.root : post

if root.author != current_user.person.id
reshare = Reshare.where(:author_id => current_user.person.id,
if root.author != current_user.person_id
reshare = Reshare.where(:author_id => current_user.person_id,
:root_guid => root.guid).first
klass = reshare.present? ? "active" : "inactive"
link_to '', reshares_path(:root_guid => root.guid), :title => t('reshares.reshare.reshare_confirmation', :author => root.author.name), :class => "image_link reshare_action #{klass}"
link_to '', reshares_path(:root_guid => root.guid), :title => t('reshares.reshare.reshare_confirmation', :author => root.author_name), :class => "image_link reshare_action #{klass}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/notifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def object_link(note, actors)
end
elsif note.instance_of?(Notifications::CommentOnPost) || note.instance_of?(Notifications::AlsoCommented) || note.instance_of?(Notifications::Reshared) || note.instance_of?(Notifications::Liked)
if post = note.linked_object
translation(target_type, :actors => actors, :count => actors_count, :post_author => h(post.author.name), :post_link => link_to(t('notifications.post'), post_path(post), 'data-ref' => post.id, :class => 'hard_object_link').html_safe)
translation(target_type, :actors => actors, :count => actors_count, :post_author => h(post.author_name), :post_link => link_to(t('notifications.post'), post_path(post), 'data-ref' => post.id, :class => 'hard_object_link').html_safe)
else
t(note.deleted_translation_key, :actors => actors, :count => actors_count).html_safe
end
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
module PostsHelper
def post_page_title(post, opts={})
if post.is_a?(Photo)
I18n.t "posts.show.photos_by", :count => 1, :author => post.status_message.author.name
I18n.t "posts.show.photos_by", :count => 1, :author => post.status_message_author_name
elsif post.is_a?(Reshare)
I18n.t "posts.show.reshare_by", :author => post.author.name
I18n.t "posts.show.reshare_by", :author => post.author_name
else
if post.text.present?
truncate(post.text(:plain_text => true), :length => opts.fetch(:length, 20))
elsif post.respond_to?(:photos) && post.photos.present?
I18n.t "posts.show.photos_by", :count => post.photos.size, :author => post.author.name
I18n.t "posts.show.photos_by", :count => post.photos.size, :author => post.author_name
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/mailers/notification_mailers/also_commented.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ class AlsoCommented < NotificationMailers::Base
include ActionView::Helpers::TextHelper

attr_accessor :comment
delegate :post, to: :comment, prefix: true

def set_headers(comment_id)
@comment = Comment.find_by_id(comment_id)

if mail?
@headers[:from] = "\"#{@comment.author.name} (Diaspora*)\" <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
@headers[:from] = "\"#{@comment.author_name} (Diaspora*)\" <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = truncate(@comment.comment_email_subject, :length => TRUNCATION_LEN)
@headers[:subject] = "Re: #{@headers[:subject]}"
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/mailers/notification_mailers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module NotificationMailers

class Base
attr_accessor :recipient, :sender

delegate :unconfirmed_email, :confirm_email_token,
:first_name, to: :recipient, prefix: true
delegate :first_name, :name, :sender, to: :sender, prefix: true

def initialize(recipient_id, sender_id=nil, *args)
@headers = {}
Expand Down
4 changes: 2 additions & 2 deletions app/mailers/notification_mailers/comment_on_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class CommentOnPost < NotificationMailers::Base
def set_headers(comment_id)
@comment = Comment.find(comment_id)

@headers[:from] = "\"#{@comment.author.name} (Diaspora*)\" <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
@headers[:from] = "\"#{@comment.author_name} (Diaspora*)\" <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = truncate(@comment.comment_email_subject, :length => TRUNCATION_LEN)
@headers[:subject] = "Re: #{@headers[:subject]}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/notification_mailers/confirm_email.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module NotificationMailers
class ConfirmEmail < NotificationMailers::Base
def set_headers
@headers[:to] = name_and_address(@recipient.profile.first_name, @recipient.unconfirmed_email)
@headers[:to] = name_and_address(@recipient.first_name, @recipient.unconfirmed_email)
@headers[:subject] = I18n.t('notifier.confirm_email.subject', :unconfirmed_email => @recipient.unconfirmed_email)
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/mailers/notification_mailers/liked.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module NotificationMailers
class Liked < NotificationMailers::Base
attr_accessor :like
delegate :target, to: :like, prefix: true

def set_headers(like_id)
@like = Like.find(like_id)

@headers[:subject] = I18n.t('notifier.liked.liked', :name => @sender.name)
end
end
end
end
3 changes: 2 additions & 1 deletion app/mailers/notification_mailers/mentioned.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module NotificationMailers
class Mentioned < NotificationMailers::Base
attr_accessor :post
delegate :author_name, to: :post, prefix: true

def set_headers(target_id)
@post = Mention.find_by_id(target_id).post

@headers[:subject] = I18n.t('notifier.mentioned.subject', :name => @sender.name)
end
end
end
end
2 changes: 1 addition & 1 deletion app/mailers/notification_mailers/private_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def set_headers(message_id)
@conversation = @message.conversation
@participants = @conversation.participants

@headers[:from] = "\"#{@message.author.name} (Diaspora*)\" <#{AppConfig[:smtp_sender_address]}>"
@headers[:from] = "\"#{@message.author_name} (Diaspora*)\" <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = @conversation.subject.strip
@headers[:subject] = "Re: #{@headers[:subject]}" if @conversation.messages.size > 1
end
Expand Down
4 changes: 3 additions & 1 deletion app/mailers/notification_mailers/reshared.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module NotificationMailers
class Reshared < NotificationMailers::Base
attr_accessor :reshare

delegate :root, to: :reshare, prefix: true

def set_headers(reshare_id)
@reshare = Reshare.find(reshare_id)

@headers[:subject] = I18n.t('notifier.reshared.reshared', :name => @sender.name)
end
end
end
end
2 changes: 1 addition & 1 deletion app/mailers/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def invite(email, message, inviter, invitation_code, locale)
@invitation_code = invitation_code

mail_opts = {:to => email, :from => AppConfig[:smtp_sender_address],
:subject => I18n.t('notifier.invited_you', :name => @inviter.person.name),
:subject => I18n.t('notifier.invited_you', :name => @inviter.name),
:host => AppConfig[:pod_uri].host}

I18n.with_locale(locale) do
Expand Down
4 changes: 3 additions & 1 deletion app/models/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Block < ActiveRecord::Base
belongs_to :person
belongs_to :user

delegate :name, to: :person, prefix: true

validates :user_id, :presence => true
validates :person_id, :presence => true, :uniqueness => { :scope => :user_id }

Expand All @@ -12,4 +14,4 @@ def not_blocking_yourself
errors[:person_id] << "stop blocking yourself!"
end
end
end
end
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Comment < ActiveRecord::Base
belongs_to :commentable, :touch => true, :polymorphic => true
alias_attribute :post, :commentable
belongs_to :author, :class_name => 'Person'

delegate :name, to: :author, prefix: true
delegate :comment_email_subject, to: :parent
delegate :author_name, to: :parent, prefix: true

validates :text, :presence => true, :length => {:maximum => 65535}
validates :parent, :presence => true #should be in relayable (pending on fixing Message)
Expand Down
5 changes: 4 additions & 1 deletion app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Contact < ActiveRecord::Base

belongs_to :person
validates :person, :presence => true

delegate :name, :diaspora_handle, :guid, :first_name,
to: :person, prefix: true

has_many :aspect_memberships
has_many :aspects, :through => :aspect_memberships
Expand Down Expand Up @@ -72,7 +75,7 @@ def contacts
incoming_aspects = Aspect.where(
:user_id => self.person.owner_id,
:contacts_visible => true).joins(:contacts).where(
:contacts => {:person_id => self.user.person.id}).select('aspects.id')
:contacts => {:person_id => self.user.person_id}).select('aspects.id')
incoming_aspect_ids = incoming_aspects.map{|a| a.id}
similar_contacts = Person.joins(:contacts => :aspect_memberships).where(
:aspect_memberships => {:aspect_id => incoming_aspect_ids}).where(people[:id].not_eq(self.user.person.id)).select('DISTINCT people.*')
Expand Down
2 changes: 2 additions & 0 deletions app/models/invitation_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class InvitationCode < ActiveRecord::Base

before_create :generate_token, :set_default_invite_count

delegate :name, to: :user, prefix: true

def to_param
token
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Message < ActiveRecord::Base

belongs_to :author, :class_name => 'Person'
belongs_to :conversation, :touch => true

delegate :name, to: :author, prefix: true

validates :text, :presence => true
validate :participant_of_parent_conversation
Expand Down
4 changes: 3 additions & 1 deletion app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class Person < ActiveRecord::Base
xml_attr :exported_key

has_one :profile, :dependent => :destroy
delegate :last_name, :image_url, :to => :profile
delegate :last_name, :image_url, :tag_string, :bio, :location,
:gender, :birthday, :formatted_birthday, :tags, :searchable,
to: :profile
accepts_nested_attributes_for :profile

before_validation :downcase_diaspora_handle
Expand Down
1 change: 1 addition & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Photo < ActiveRecord::Base

belongs_to :status_message, :foreign_key => :status_message_guid, :primary_key => :guid
validates_associated :status_message
delegate :author_name, to: :status_message, prefix: true

attr_accessible :text, :pending
validate :ownership_of_status_message
Expand Down
Loading

0 comments on commit e70e48d

Please sign in to comment.