From 061c965330497235a8450103cd0865832b04b625 Mon Sep 17 00:00:00 2001 From: Heiko Bernloehr Date: Wed, 31 Dec 2014 08:58:19 +0100 Subject: Code rework. Moved code from MembershipsController to Membership model. --- app/controllers/memberships_controller.rb | 21 +-------------------- app/models/membership.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/app/controllers/memberships_controller.rb b/app/controllers/memberships_controller.rb index 5a5cf6d..6a7bd50 100644 --- a/app/controllers/memberships_controller.rb +++ b/app/controllers/memberships_controller.rb @@ -28,26 +28,7 @@ class MembershipsController < ApplicationController end def index - memberships = [] - Membership.for_participant_id(@participant.id).each do |membership| - memberships << - { :community => lambda { |memb| - attribs = memb.community_with_reduced_attributes.attributes - id = attribs["id"]; attribs.delete("id"); attribs["cid"] = id - attribs - }.call(membership), - :participants => membership.community.participants.with_reduced_attributes_and_without_anonymous.map {|p| - attribs = p.attributes - attribs["mid"] = Membership.for_participant_id_and_community_id(p.id, membership.community.id).first.id - attribs["org"] = {"name" => p.organization.name, "abbr" => p.organization.abrev} - attribs["itsyou"] = p.id == @participant.id - attribs["pid"] = p.id - attribs.delete("id") - attribs.delete("organization_id") - attribs - } - } - end + memberships = Membership.memberships(@participant) if memberships.empty? render :text => "", :content_type => "application/json", :layout => false else diff --git a/app/models/membership.rb b/app/models/membership.rb index d26f8f0..7e7aebf 100644 --- a/app/models/membership.rb +++ b/app/models/membership.rb @@ -59,6 +59,33 @@ class Membership < ActiveRecord::Base end end + def self.memberships(participant) + memberships = [] + Membership.for_participant_id(participant.id).each do |membership| + community= lambda { |memb| + attribs = memb.community_with_reduced_attributes.attributes + id = attribs["id"]; attribs.delete("id"); attribs["cid"] = id + attribs + }.call(membership) + logger.debug "**** Membership::memberships: community: #{community.inspect}" + participants= membership.community.participants.with_reduced_attributes_and_without_anonymous.map do |p| + attribs = p.attributes + attribs["mid"] = Membership.for_participant_id_and_community_id(p.id, membership.community.id).first.id + attribs["org"] = {"name" => p.organization.name, "abbr" => p.organization.abrev} + attribs["itsyou"] = p.id == participant.id + attribs["pid"] = p.id + attribs.delete("id") + attribs.delete("organization_id") + attribs + end + logger.debug "**** Membership::memberships: participants: #{participants.inspect}" + memberships << + { :community => community, + :participants => participants + } + end + memberships + end private -- cgit v1.2.3