Skip to content

Commit

Permalink
Merge pull request #79 from notmarkmiranda/78-update-rake-task-to-cre…
Browse files Browse the repository at this point in the history
…ate-memberships

78 Fix rake task to create memberships
  • Loading branch information
notmarkmiranda committed Feb 11, 2019
2 parents 80b356a + 26075b0 commit 869a247
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions app/decorators/player_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def additional_expense_text(previous_text=false)
def delete_and_score_buttons
return unless h.policy(game_object).user_is_admin?
button_elements = []
button_elements.push(score_button(object)) unless object.finished_at
button_elements.push(delete_button(object)) if game_object.not_completed?
button_elements.push(score_button) if (object.finished_at.nil? && game_object.not_completed?)
button_elements.push(delete_button) if game_object.not_completed?

h.content_tag(:div, class: 'game-player-actions') do
button_elements.join.html_safe
Expand Down Expand Up @@ -44,11 +44,11 @@ def game_object
object.game
end

def score_button(object)
def score_button
h.button_to 'Score Player', h.player_path(object, commit: 'Score Player'), method: :patch, class: 'btn btn-outline-info btn-sm mr-2'
end

def delete_button(object)
def delete_button
h.button_to 'Delete Player', h.player_path(object), method: :delete, class: 'btn btn-danger btn-sm'
end
end
20 changes: 16 additions & 4 deletions lib/tasks/import_mike_cassano_league.rake
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ task import_mike_cassano: [:environment] do

users = URI.parse('https://docs.google.com/spreadsheets/d/e/2PACX-1vQsxani6LWbWj1o1LzK8U--98RnBOP5QhIfhSxdkls-8vzUhkPyGlT36prttjrm9dykS2U1680k_QOA/pub?gid=607317098&single=true&output=csv')

create_users(Net::HTTP.get(users))
users = create_users(Net::HTTP.get(users))

@csvs = all_seasons_csvs.map do |season|
Net::HTTP.get(season)
end

user = User.find_or_create_by(email: 'markmiranda51@gmail.com')
user.update(password: 'password')
admin_user = User.find_or_create_by(email: 'markmiranda51@gmail.com')
admin_user.update(password: 'password')

league = League.find_or_create_by(name: "Mike Cassano's Super Fun League")
league.update(user_id: user.id, privated: false)
league.update(user_id: admin_user.id, privated: false)
puts "Created #{league.name}!"

create_memberships(users.reject { |user| user == admin_user }, league)

seasons_to_create = difference_in_seasons(league.seasons_count)

unless seasons_to_create.zero?
Expand All @@ -51,14 +53,24 @@ task import_mike_cassano: [:environment] do
end

def create_users(users)
all_users = []
CSV.parse(users, { row_sep: :auto, headers: :first_row, encoding: 'bom|utf-8', header_converters: :symbol }) do |row|
first_name, last_name = row[:person].split

user = User.find_or_initialize_by(email: row[:email], first_name: first_name, last_name: last_name)
user.password = SecureRandom.hex
user.save!
all_users << user
puts "#{user.full_name} created!"
end
all_users
end

def create_memberships(users, league)
users.each do |user|
user.memberships.create!(league: league, role: 0)
puts "Created membership for #{user.full_name} as non-admin!"
end
end

def difference_in_seasons(seasons_count)
Expand Down

0 comments on commit 869a247

Please sign in to comment.