From ede1b68570ad9d190a8e167dae31a03c46c9a1d9 Mon Sep 17 00:00:00 2001 From: Heiko Bernloehr Date: Wed, 8 Apr 2015 18:18:31 +0200 Subject: Code reworking. Changed extended associations in community model to named scopes in participant model. --- app/models/community.rb | 21 +++++---------------- app/models/membership.rb | 18 +++++++++--------- app/models/participant.rb | 21 +++++++++++++-------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/app/models/community.rb b/app/models/community.rb index cf0d258..2cd3298 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -1,35 +1,24 @@ # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de). -# +# # This file is part of ECS. -# +# # ECS is free software: you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of # the License, or (at your option) any later version. -# +# # ECS is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Affero General Public License for more details. -# +# # You should have received a copy of the GNU Affero General Public # License along with ECS. If not, see . class Community < ActiveRecord::Base has_many :memberships, :order => :id - has_many :participants, :through => :memberships do - def with_reduced_attributes_and_without_anonymous - find :all, :select => "participants.id, name, description, email, dns, organization_id", - :conditions => ["participants.anonymous = ?", false], - :order => "participants.id ASC" - end - def with_reduced_attributes_and_only_itsyou_and_without_anonymous(pid) - find :all, :select => "participants.id, name, description, email, dns, organization_id", - :conditions => ["participants.anonymous = ? and participants.id = ?", false, pid], - :order => "participants.id ASC" - end - end + has_many :participants, :through => :memberships has_many :community_messages, :dependent => :destroy has_many :messages, :through => :community_messages validates_presence_of :name diff --git a/app/models/membership.rb b/app/models/membership.rb index 895d7e6..6eb4d74 100644 --- a/app/models/membership.rb +++ b/app/models/membership.rb @@ -1,17 +1,17 @@ # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de). -# +# # This file is part of ECS. -# +# # ECS is free software: you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of # the License, or (at your option) any later version. -# +# # ECS is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Affero General Public License for more details. -# +# # You should have received a copy of the GNU Affero General Public # License along with ECS. If not, see . @@ -31,11 +31,11 @@ class Membership < ActiveRecord::Base # returns memberships of the relation between a participant and a message # if no relationship then returns empty array. named_scope :receiver, lambda { |participant_id,message_id| { - :joins => [:participant, {:membership_messages => :message}], + :joins => [:participant, {:membership_messages => :message}], :conditions => { :participants => { :id => participant_id }, :messages => { :id => message_id } } } } named_scope :receivers, lambda { |message_id| { - :joins => [:membership_messages => :message], + :joins => [:membership_messages => :message], :select => :memberships.to_s+".id" + ", community_id, participant_id", :conditions => { :messages => { :id => message_id } } } } @@ -69,10 +69,10 @@ class Membership < ActiveRecord::Base }.call(membership) logger.debug "**** Membership::memberships: community: #{community.inspect}" if itsyou - participants_with_reduced_attribs= membership.community.participants.with_reduced_attributes_and_only_itsyou_and_without_anonymous(participant.id) + participants_with_reduced_attribs= membership.community.participants.itsyou(participant.id).without_anonymous.reduced_attributes logger.debug "**** Membership::memberships: participants_with_reduced_attribs: #{participants_with_reduced_attribs.inspect}" else - participants_with_reduced_attribs= membership.community.participants.with_reduced_attributes_and_without_anonymous + participants_with_reduced_attribs= membership.community.participants.without_anonymous.reduced_attributes end participants= participants_with_reduced_attribs.map do |p| attribs = p.attributes @@ -85,7 +85,7 @@ class Membership < ActiveRecord::Base attribs end logger.debug "**** Membership::memberships: participants: #{participants.inspect}" - memberships << + memberships << { :community => community, :participants => participants } diff --git a/app/models/participant.rb b/app/models/participant.rb index 561c1f7..296fd17 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -1,17 +1,17 @@ # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de). -# +# # This file is part of ECS. -# +# # ECS is free software: you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of # the License, or (at your option) any later version. -# +# # ECS is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Affero General Public License for more details. -# +# # You should have received a copy of the GNU Affero General Public # License along with ECS. If not, see . @@ -41,7 +41,7 @@ class Participant < ActiveRecord::Base accepts_nested_attributes_for :communities, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } accepts_nested_attributes_for :subparticipant, :allow_destroy => true - #named_scope :reduced_attributes, :select => "name, description, dns, email" + named_scope :order_id_asc, :order => "participants.id ASC" named_scope :without_anonymous, :conditions => { :participants => { :anonymous => false } } named_scope :anonymous, :conditions => { :participants => { :anonymous => true } } named_scope :for_message, lambda { |message| { @@ -51,6 +51,11 @@ class Participant < ActiveRecord::Base :joins => [:memberships => :community], :conditions => { :communities => { :id => community.id }}}} named_scope :for_subparticipants + named_scope :itsyou, lambda { |itsyoupid| { :conditions => { :participants => { :id => itsyoupid } } } } + + def self.reduced_attributes + find :all, :select => "participants.id, participants.name, participants.description, participants.email, participants.dns, participants.organization_id" + end # test if the participant is the initial sender of the message in question. def sender?(message) @@ -62,9 +67,9 @@ class Participant < ActiveRecord::Base end def receiver?(message) - not Membership.receiver(id, message.id).empty? + not Membership.receiver(id, message.id).empty? end - + def events? self.events_.blank? ? false : true end @@ -109,5 +114,5 @@ private def delete_messages Message.destroy_all(["sender = ?", self.id]) end - + end -- cgit v1.2.3