Move shortcode setting to before_save on Party model

This commit is contained in:
Justin Edmund 2023-01-08 20:13:09 -08:00
parent c2576973bb
commit 2a465aca3b
2 changed files with 12 additions and 12 deletions

View file

@ -8,12 +8,7 @@ module Api
before_action :set, only: %w[update destroy] before_action :set, only: %w[update destroy]
def create def create
party = Party.new(shortcode: random_string) party = Party.new(party_params.merge(user: current_user))
party.user = current_user if current_user
if party_params
party.attributes = party_params
end
# unless party_params.empty? # unless party_params.empty?
# party.attributes = party_params # party.attributes = party_params
@ -125,12 +120,6 @@ module Api
end end
end end
def random_string
num_chars = 6
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
(0...num_chars).map { o[rand(o.length)] }.join
end
def set_from_slug def set_from_slug
@party = Party.where('shortcode = ?', params[:id]).first @party = Party.where('shortcode = ?', params[:id]).first
@party.favorited = current_user && @party ? @party.is_favorited(current_user) : false @party.favorited = current_user && @party ? @party.is_favorited(current_user) : false

View file

@ -43,6 +43,7 @@ class Party < ApplicationRecord
has_many :favorites has_many :favorites
before_save :set_shortcode
##### ActiveRecord Validations ##### ActiveRecord Validations
validate :skills_are_unique validate :skills_are_unique
@ -58,6 +59,16 @@ class Party < ApplicationRecord
private private
def set_shortcode
self.shortcode = random_string
end
def random_string
num_chars = 6
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
(0...num_chars).map { o[rand(o.length)] }.join
end
def skills_are_unique def skills_are_unique
skills = [skill0, skill1, skill2, skill3].compact skills = [skill0, skill1, skill2, skill3].compact