forgeplus/app/services/reward_experience_service.rb

24 lines
519 B
Ruby
Raw Normal View History

2020-03-09 00:40:16 +08:00
class RewardExperienceService
attr_reader :user, :attrs
def initialize(user, **attrs)
@user = user
@attrs = attrs.slice(*%i[container_id container_type score])
end
def call
return if user.experiences.exists?(attrs.except(:score))
ActiveRecord::Base.transaction do
experience = user.experiences.create!(attrs)
user.increment!(:experience, experience.score)
experience
end
end
def self.call(user, **attrs)
new(user, attrs).call
end
end