Skip to content

Commit

Permalink
Bulk insert data in sample group and sample event
Browse files Browse the repository at this point in the history
  • Loading branch information
lujanfernaud committed May 15, 2018
1 parent 3756402 commit dcae891
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions app/models/sample_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.build_for_group(group)
def initialize(group)
@group = group
@event = nil
@attendances = []
end

def build_sample_event
Expand Down Expand Up @@ -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
20 changes: 18 additions & 2 deletions app/models/sample_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.create_for_user(user)
def initialize(user)
@user = user
@group = nil
@memberships = []
end

def create_sample_group
Expand Down Expand Up @@ -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

Expand Down
18 changes: 14 additions & 4 deletions test/models/sample_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion test/models/sample_group_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit dcae891

Please sign in to comment.