Optimize events listing

This commit is contained in:
Petko Bordjukov 2016-10-15 05:59:07 +03:00
parent 74505569c1
commit a9a538d08c
6 changed files with 49 additions and 10 deletions

View File

@ -3,7 +3,7 @@ module Management
def index
@conference = find_conference
@filters = params[:filters] || {}
@events = EventSearch.new(scope: Event.where(conference: @conference).eager_load(:proposition, :proposer, :track, :event_type), filters: params[:filters]).results
@events = EventSearch.new(scope: Event.where(conference: @conference).eager_load(:participants_with_personal_profiles, :proposition, :proposer, :track, :event_type).preload(:conference), filters: params[:filters]).results
# @events = @conference.events.order(:title).includes(:proposition, :proposer, :track, :event_type)
end

View File

@ -1,12 +1,11 @@
module EventsHelper
def links_to_event_participants_for(event)
event.participants.map do |participant|
if participant.personal_profile(event.conference).present?
profile = participant.personal_profile(event.conference)
link_to icon(:user, profile.name),
management_conference_personal_profile_path(profile, conference_id: event.conference.id)
event.participants_with_personal_profiles.map do |participant|
if participant.has_personal_profile?
link_to icon(:user, participant.name),
management_conference_personal_profile_path(participant.personal_profile_id, conference_id: event.conference.id)
else
link_to icon('user-plus', participant.email),
link_to icon('user-plus', participant.personal_email),
new_management_conference_personal_profile_path(conference_id: event.conference.id,
user_id: participant.id),
title: t('management.events.event.create_profile'), class: 'bg-danger'

View File

@ -9,6 +9,7 @@ class Event < ActiveRecord::Base
has_many :pending_participations, ->() { pending }, class_name: 'Participation'
has_many :approved_participations, ->() { approved }, class_name: 'Participation'
has_many :participants, through: :approved_participations
has_many :participants_with_personal_profiles, through: :approved_participations, source: :participant_with_personal_profile
has_many :conflict_counts, -> { order(number_of_conflicts: :desc) }, foreign_key: :left_id
belongs_to :event_type
@ -32,9 +33,7 @@ class Event < ActiveRecord::Base
accepts_nested_attributes_for :participations, allow_destroy: true
def all_participants_have_profiles?
participants.all? do |participant|
participant.personal_profile(conference).present?
end
participants_with_personal_profiles.all? { |participant| participant.has_personal_profile? }
end
def proposer_profile

16
app/models/participant.rb Normal file
View File

@ -0,0 +1,16 @@
class Participant < ActiveRecord::Base
self.table_name = :participants
self.primary_key = :participant_id
def twitter=(handle)
write_attribute :twitter, handle.gsub(/\A@/,'') if handle
end
def name
"#{first_name} #{last_name}"
end
def has_personal_profile?
user_id.present?
end
end

View File

@ -1,5 +1,6 @@
class Participation < ActiveRecord::Base
belongs_to :participant, class_name: User
has_one :participant_with_personal_profile, class_name: Participant
belongs_to :event
validates :participant_id, presence: true
scope :approved, ->() { where approved: true }

View File

@ -0,0 +1,24 @@
PARTICIPANTS_SQL = <<EOS
CREATE VIEW "participants" AS
SELECT "users"."id" AS "participant_id",
"users"."email" AS personal_email,
"users"."language" AS language,
"participations"."id" AS "participation_id",
"personal_profiles"."id" AS "personal_profile_id",
"personal_profiles".* FROM "users"
INNER JOIN "participations" ON "users"."id" = "participations"."participant_id"
INNER JOIN "events" ON "events"."id" = "participations"."event_id"
INNER JOIN "conferences" ON "events"."conference_id" = "conferences"."id"
LEFT JOIN "personal_profiles" ON "personal_profiles"."conference_id" = "events"."conference_id"
AND "personal_profiles"."user_id" = "users"."id";
EOS
class AddParticipantsView < ActiveRecord::Migration
def up
execute PARTICIPANTS_SQL
end
def down
execute 'DROP VIEW "participants"'
end
end