-
-
Save kenrick/630a84922fb56f70e7e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Team < ActiveRecord::Base | |
# Friendly ID | |
extend FriendlyId | |
friendly_id :name | |
# Rolify | |
resourcify | |
# Associations | |
belongs_to :user | |
has_and_belongs_to_many :players | |
has_many :ranked_statistics | |
# Validations | |
validates :name, presence: true | |
validates :tag, presence: true | |
# Callbacks | |
after_save :send_to_mixpanel | |
after_create :import_ranked_statistic | |
# Fetch Initial Information | |
def fetch_and_save(team_identifier) | |
# Get User | |
owner = Player.find(player_id) | |
## @IF Team already exists | |
if Team.where(name: team_identifier, server: owner.server).present? | |
errors.add :base, 'This Team exists already!' | |
return false | |
end | |
# 1.) Connect to Perseus | |
client = Perseus::Client.new Settings.rito.api_key, {region: owner.server} | |
# 2.) Get Teams | |
teams = client.team.by_summoner(owner.summoner_id).values | |
# 3.) Find Team by Name | |
team = teams.first.detect{ |t| t.name == team_identifier } | |
if team.nil? | |
errors.add :base, "We couldn't find a Team with that Name" | |
return false | |
end | |
if team.roster.owner_id != owner.summoner_id | |
errors.add :base, "You are not the owner of #{team_identifier}!" | |
return false | |
end | |
assign_attributes( | |
full_id: team.full_id, | |
status: team.status.capitalize, | |
name: team.name, | |
tag: team.tag, | |
create_date: team.create_date, | |
modify_date: team.modify_date, | |
last_join_date: team.last_join_date, | |
second_last_join_date: team.second_last_join_date, | |
third_last_join_date: team.third_last_join_date, | |
last_joined_ranked_date: team.last_joined_ranked_team_queue_date, | |
owner_id: owner.id, | |
server: owner.server | |
) | |
## Do something with the Team Roster [Send invites etc.] | |
# Add and Save | |
players << owner | |
save | |
rescue StandardError | |
errors.add :base, "We couldn't verify that you are the owner of this team, Please contact Support" | |
return false | |
end | |
def send_to_mixpanel | |
TeamMxpJob.perform_later(self, user) | |
end | |
def import_ranked_statistic | |
TeamRankedStatJob.perform_later(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment