diff --git a/Gemfile b/Gemfile index 209c342a..e287d5a8 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,9 @@ gem 'gravatar_image_tag', '~> 1.2' gem 'inline_svg', '~> 1.3', '>= 1.3.1' gem 'faker', '~> 1.8', '>= 1.8.7' +# Used for bulk inserting data into database using ActiveRecord. +gem 'activerecord-import', '~> 0.23.0' + # Used to round time to the nearest hour. gem 'rounding', '~> 1.0', '>= 1.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index 12ebe00b..1e033049 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -33,6 +33,8 @@ GEM activemodel (= 5.1.6) activesupport (= 5.1.6) arel (~> 8.0) + activerecord-import (0.23.0) + activerecord (>= 3.2) activesupport (5.1.6) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) @@ -374,6 +376,7 @@ PLATFORMS ruby DEPENDENCIES + activerecord-import (~> 0.23.0) bcrypt (~> 3.1.7) bootstrap (~> 4.0.0.beta2.1) bootstrap-will_paginate (= 1.0.0) diff --git a/app/models/sample_event.rb b/app/models/sample_event.rb index dff44530..3764f30b 100644 --- a/app/models/sample_event.rb +++ b/app/models/sample_event.rb @@ -8,6 +8,7 @@ def self.build_for_group(group) def initialize(group) @group = group @event = nil + @attendances = [] end def build_sample_event @@ -69,10 +70,15 @@ def event_address end def add_sample_attendees - random_members = group.members_with_role.shuffle[0..29] - random_members.each do |member| - @event.attendees << member + @attendances << Attendance.new( + attendee: member, attended_event: @event) end + + Attendance.import(@attendances) + end + + def random_members + group.members_with_role.shuffle[0..29] end end diff --git a/app/models/sample_group.rb b/app/models/sample_group.rb index 150cedf1..3e995fde 100644 --- a/app/models/sample_group.rb +++ b/app/models/sample_group.rb @@ -8,6 +8,7 @@ def self.create_for_user(user) def initialize(user) @user = user @group = nil + @memberships = [] end def create_sample_group @@ -47,9 +48,24 @@ def group_location end def add_sample_members + create_memberships + add_member_role_to_memberships + end + + # We are using 'activerecord-import' for bulk inserting the data. + # https://github.com/zdennis/activerecord-import/wiki/Examples + def create_memberships SampleUser.all.each do |user| - group.members << user - user.add_role :member, group + @memberships << GroupMembership.new(group: @group, user: user) + end + + GroupMembership.import(@memberships) + end + + def add_member_role_to_memberships + # https://github.com/zdennis/activerecord-import/wiki/Callbacks + @memberships.each do |membership| + membership.run_callbacks(:save) { false } end end diff --git a/test/models/sample_event_test.rb b/test/models/sample_event_test.rb index bdc00a57..020933d0 100644 --- a/test/models/sample_event_test.rb +++ b/test/models/sample_event_test.rb @@ -3,14 +3,24 @@ class SampleEventTest < ActiveSupport::TestCase def setup stub_geocoder + @group = Group.last end test "creates event with sample attendees" do - group = Group.last - SampleEvent.build_for_group(group) + add_members_with_role + + SampleEvent.build_for_group(@group) event = Event.last - assert event.organizer, group.owner - assert event.attendees, group.members + assert_equal event.organizer, @group.owner + assert_equal event.attendees, @group.members end + + private + + def add_members_with_role + @group.members.each do |user| + user.add_role :member, @group + end + end end diff --git a/test/models/sample_group_test.rb b/test/models/sample_group_test.rb index 67431c97..a5b899c2 100644 --- a/test/models/sample_group_test.rb +++ b/test/models/sample_group_test.rb @@ -1,18 +1,28 @@ require 'test_helper' class SampleGroupTest < ActiveSupport::TestCase + GROUP_OWNER_COUNT = 1 + def setup stub_geocoder end test "creates group with sample members, organizer and event" do user = users(:stranger) + SampleGroup.create_for_user(user) group = Group.last + members_count = SampleUser.all.count + organizers_count = group.organizers.count - GROUP_OWNER_COUNT + members_with_role_count = members_count - organizers_count + assert group.sample_group? assert_equal user, group.owner - assert SampleUser.all.count, group.members.count + + assert_equal SampleUser.all, group.members + assert_equal members_with_role_count, group.members_with_role.count + assert_equal 5, group.organizers.count assert_equal 1, group.events.count end