I'm a beginner with rails working on a rails 4 project to use with my students. Users enroll in lessons; on enrollment, four activities are created for each user (expositions, scrambled, dictations, and spellings). Each of these activities has a number of words which are set to false
in the db upon creation, and change to true
as the user rewrites them correctly. At the bottom of each lesson's view I'm displaying a list of enrolled users. If the user completes all four activities, I'm displaying a font-awesome checkmark icon.
I'm noticing as more students enroll in each lesson that loading times for the lesson show view are increasing--most probably because my setup to find whether each user completed the lessons is inefficient.
In my lessonss controller I wrote helper methods to list the lesson's enrolled users, and to generate arrays with users who completed each of the lessons activities. In the view page, I'm testing whether each of the listed users completed all four activities in order to add the "completed" checkmark to his or her name.
As things are right now the code does what it's supposed to, but I'd love to learn and improve the student-user's experience. Any feedback regarding how I can improve this process will be well received.
In lessons controller:
helper_method :enrolled_users
def enrolled_users
lesson = Lesson.find(params[:id])
enrollments = lesson.enrollments
enrolled_users = enrollments.map { |enrollment| enrollment.user }
end
helper_method :completed_scram
def completed_scram
lesson = Lesson.find(params[:id])
completed_scram = enrolled_users.map do |user|
current_enrollment = lesson.enrollment_for(user)
all_unscramble = current_enrollment.scrambled_words
user if all_unscramble.all? { |scrambled| scrambled.completed == true }
end
end
helper_method :completed_expos
def completed_expos
lesson = Lesson.find(params[:id])
completed_expos = enrolled_users.map do |user|
current_enrollment = lesson.enrollment_for(user)
all_expos = current_enrollment.word_expositions
user if all_expos.all? { |expos| expos.completed == true }
end
end
helper_method :completed_dicts
def completed_dicts
lesson = Lesson.find(params[:id])
completed_dic = enrolled_users.map do |user|
current_enrollment = lesson.enrollment_for(user)
all_dicts = current_enrollment.word_dictations
user if all_dicts.all? { |dict| dict.completed == true }
end
end
helper_method :completed_spel
def completed_spel
lesson = Lesson.find(params[:id])
completed_sp = enrolled_users.map do |user|
current_enrollment = lesson.enrollment_for(user)
all_sp = current_enrollment.image_spellings
user if all_sp.all? { |spell| spell.completed == true }
end
end
In the view:
<% unless enrolled_users.count == 0 %>
<div class="col-xs-8 col-xs-offset-2 enroll-box">
<h3>Enrolled users:</h3>
<% enrolled_users.uniq.each do |user| %>
<ul class="list-square">
<li>
<%= user.username %>
<!-- Check for all activities completed -->
<% if completed_scram.include?(user) && completed_expos.include?(user) && completed_dicts.include?(user) && completed_spel.include?(user) %>
<i class="fa fa-check-circle" id="check-completed-lesson"></i>
<% end %>
</li>
</ul>
<% end %>
</div>
<% end %>