From a9a538d08cf24eb75e034301f5509e47f1fc25ec Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Sat, 15 Oct 2016 05:59:07 +0300 Subject: [PATCH] Optimize events listing --- .../management/events_controller.rb | 2 +- app/helpers/events_helper.rb | 11 ++++----- app/models/event.rb | 5 ++-- app/models/participant.rb | 16 +++++++++++++ app/models/participation.rb | 1 + .../20161015023007_add_participants_view.rb | 24 +++++++++++++++++++ 6 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 app/models/participant.rb create mode 100644 db/migrate/20161015023007_add_participants_view.rb diff --git a/app/controllers/management/events_controller.rb b/app/controllers/management/events_controller.rb index af4e277..22c479a 100644 --- a/app/controllers/management/events_controller.rb +++ b/app/controllers/management/events_controller.rb @@ -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 diff --git a/app/helpers/events_helper.rb b/app/helpers/events_helper.rb index a67cab8..dcedbc0 100644 --- a/app/helpers/events_helper.rb +++ b/app/helpers/events_helper.rb @@ -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' diff --git a/app/models/event.rb b/app/models/event.rb index 23306f7..9d3d854 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 diff --git a/app/models/participant.rb b/app/models/participant.rb new file mode 100644 index 0000000..8284739 --- /dev/null +++ b/app/models/participant.rb @@ -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 diff --git a/app/models/participation.rb b/app/models/participation.rb index d6506e8..42531d3 100644 --- a/app/models/participation.rb +++ b/app/models/participation.rb @@ -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 } diff --git a/db/migrate/20161015023007_add_participants_view.rb b/db/migrate/20161015023007_add_participants_view.rb new file mode 100644 index 0000000..b6ac360 --- /dev/null +++ b/db/migrate/20161015023007_add_participants_view.rb @@ -0,0 +1,24 @@ +PARTICIPANTS_SQL = <