end def call(user:, new_role:) users_repository .for(user) .grant_role(new_role) .tap do |updated_user| yield updated_user if block_given? end end def default_users_repository Api::Users end end
to: :user_class alias_method :for, :new end delegate :find, :update, to: :class attr_reader :instance def initialize(instance) @instance = instance end def grant_role(role) if update(instance.id, role: role) return find(instance.id) end raise InvalidRoleGrant end end end
or Hanami::Model • Replace all ActiveRecord model references with Repository calls • Sigh contentedly when my test suite execution time drops • Rinse and repeat
user Api::Users.find(user_id) end end class Api::Users def self.with_lessons(users) user_lessons = UserLesson.for_users(users).group_by(&:user_id) users.map do |user| user.with(lesson: user_lessons[user.id].lesson) end end end