Skip to content

Commit

Permalink
review: remove create_ from methods
Browse files Browse the repository at this point in the history
I'll try to name methods for what they represent in this context
and not what they literally do
  • Loading branch information
vasconsaurus committed Feb 14, 2024
1 parent e2999a8 commit b711ee1
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def initialize(existing_user_email)
@user_emails = @user_names.map { |name| Faker::Internet.safe_email(name: name) }
@team_names = Array.new(4) { Faker::Company.name }

create_users
create_teams
create_team_users
users
teams
team_users
end

def get_users_emails_and_passwords
Expand All @@ -38,7 +38,7 @@ def get_users_emails_and_passwords
created.flatten
end

def create_users
def users
@users ||= begin

all_users = {
Expand All @@ -60,13 +60,13 @@ def create_users
}
}.transform_values { |params| User.create!(params) }

all_users[:main_user_a] = create_or_fetch_main_user_a
all_users[:main_user_a] = main_user_a
all_users.each_value { |user| user.confirm && user.save! }
all_users
end
end

def create_teams
def teams
@teams ||= begin

all_teams = {
Expand All @@ -90,14 +90,14 @@ def create_teams
}
}.transform_values { |t| Team.create!(t) }

all_teams[:main_team_a] = create_or_fetch_main_team_a
all_teams[:main_team_a] = main_team_a
all_teams
end
end

private

def create_or_fetch_main_user_a
def main_user_a
@main_user_a ||= if existing_user_email
User.find_by(email: existing_user_email)
else
Expand All @@ -111,7 +111,7 @@ def create_or_fetch_main_user_a
end
end

def create_or_fetch_main_team_a
def main_team_a
if main_user_a.teams.first
main_user_a.teams.first
else
Expand All @@ -123,7 +123,7 @@ def create_or_fetch_main_team_a
end
end

def create_team_users
def team_users
[
{
team: teams[:invited_team_b1],
Expand All @@ -145,10 +145,10 @@ def create_team_users
}
].each { |params| TeamUser.create!(params) }

create_or_fetch_main_team_a_team_user
main_team_a_team_user
end

def create_or_fetch_main_team_a_team_user
def main_team_a_team_user
return if @existing_user_email
TeamUser.create!({
team: teams[:main_team_a],
Expand All @@ -172,7 +172,7 @@ def initialize(setup)
@users = setup.users
end

def create_populated_projects
def populated_projects
projects_params = [
{
title: "#{teams[:main_team_a][:name]} / [a] Main User: Main Team",
Expand All @@ -183,10 +183,10 @@ def create_populated_projects
media_attributes: media_params,
team: teams[:main_team_a],
claim_description_attributes: {
description: create_claim_title(media_params),
description: claim_title(media_params),
context: Faker::Lorem.sentence,
user: users[:main_user_a],
fact_check_attributes: get_fact_check_params_for_half_the_claims(index, users[:main_user_a]),
fact_check_attributes: fact_check_params_for_half_the_claims(index, users[:main_user_a]),
}
}
}
Expand All @@ -200,10 +200,10 @@ def create_populated_projects
media_attributes: media_params,
team: teams[:invited_team_b1],
claim_description_attributes: {
description: create_claim_title(media_params),
description: claim_title(media_params),
context: Faker::Lorem.sentence,
user: users[:invited_user_b],
fact_check_attributes: get_fact_check_params_for_half_the_claims(index, users[:invited_user_b]),
fact_check_attributes: fact_check_params_for_half_the_claims(index, users[:invited_user_b]),
}
}
}
Expand All @@ -217,10 +217,10 @@ def create_populated_projects
media_attributes: media_params,
team: teams[:invited_team_b2],
claim_description_attributes: {
description: create_claim_title(media_params),
description: claim_title(media_params),
context: Faker::Lorem.sentence,
user: users[:invited_user_b],
fact_check_attributes: get_fact_check_params_for_half_the_claims(index, users[:invited_user_b]),
fact_check_attributes: fact_check_params_for_half_the_claims(index, users[:invited_user_b]),
}
}
}
Expand All @@ -234,10 +234,10 @@ def create_populated_projects
media_attributes: media_params,
team: teams[:invited_team_c],
claim_description_attributes: {
description: create_claim_title(media_params),
description: claim_title(media_params),
context: Faker::Lorem.sentence,
user: users[:invited_user_c],
fact_check_attributes: get_fact_check_params_for_half_the_claims(index, users[:invited_user_c]),
fact_check_attributes: fact_check_params_for_half_the_claims(index, users[:invited_user_c]),
}
}
}
Expand Down Expand Up @@ -300,19 +300,19 @@ def get_medias_params
]
end

def create_title_from_link(link)
def title_from_link(link)
path = URI.parse(link).path
path.remove('/post/').underscore.humanize
end

def create_claim_title(media_params)
media_params[:type] == "Link" ? create_title_from_link(media_params[:url]) : Faker::Company.catch_phrase
def claim_title(media_params)
media_params[:type] == "Link" ? title_from_link(media_params[:url]) : Faker::Company.catch_phrase
end

def get_fact_check_params_for_half_the_claims(index, user)
def fact_check_params_for_half_the_claims(index, user)
if index.even?
{
summary: Faker::Company.catch_phrase,
summary: Faker::Company.catch_phrase,
title: Faker::Company.name,
user: user,
language: 'en',
Expand All @@ -335,15 +335,16 @@ def get_url_for_some_fact_checks(index)
print ">> "
answer = STDIN.gets.chomp

puts "Stretch your legs, this might take a while"
puts "On a mac took about 10 minutes to create all populated workspaces"
puts "—————"
puts "Stretch your legs, this might take a while."
puts "On a mac took about 10 minutes to create all populated workspaces."
puts "—————"

begin
puts 'Creating users and teams...'
setup = Setup.new(answer.presence) # .presence : returns nil or the string
puts 'Creating projects for all users...'
PopulatedProjects.new(setup).create_populated_projects
PopulatedProjects.new(setup).populated_projects
rescue RuntimeError => e
if e.message.include?('We could not parse this link')
puts "—————"
Expand All @@ -354,16 +355,19 @@ def get_url_for_some_fact_checks(index)
end
end


# teams.each do |team|
# project_medias = team.project_medias

# create_confirmed_relationship(project_medias[0], project_medias[1..3])
# create_confirmed_relationship(project_medias[4], project_medias[5])
# create_confirmed_relationship(project_medias[6], project_medias[7])
# create_confirmed_relationship(project_medias[8], project_medias[1])
# confirmed_relationship(project_medias[0], project_medias[1..3])
# confirmed_relationship(project_medias[4], project_medias[5])
# confirmed_relationship(project_medias[6], project_medias[7])
# confirmed_relationship(project_medias[8], project_medias[1])
# end

unless e then setup.get_users_emails_and_passwords.each { |user_info| puts user_info } end
unless e
puts "—————"
puts "Created users:"
setup.get_users_emails_and_passwords.each { |user_info| puts user_info }
end

Rails.cache.clear

0 comments on commit b711ee1

Please sign in to comment.