forgeplus/app/services/reward_grade_service.rb

19 lines
489 B
Ruby
Raw Permalink Normal View History

2020-03-09 00:40:16 +08:00
class RewardGradeService < ApplicationService
attr_reader :user, :attrs, :not_unique
def initialize(user, **attrs)
@user = user
@not_unique = attrs.delete(:not_unique) || false
@attrs = attrs.slice(*%i[container_id container_type score])
end
def call
return if user.grades.exists?(attrs) && !not_unique
ActiveRecord::Base.transaction do
grade = user.grades.create!(attrs)
user.increment!(:grade, grade.score)
end
end
end