Sha256: f6e95c9f2ade4f444eda50d9b4af311fe4b09f0f01b2ec2bda11d1b9a6c3a529

Contents?: true

Size: 549 Bytes

Versions: 2

Compression:

Stored size: 549 Bytes

Contents

module Troo
  class SetDefault
    class << self
      def for(entity)
        new(entity).set_default!
      end
    end

    def initialize(entity)
      @entity = entity
    end

    def set_default!
      return false if already_default?
      unset_default
      set_new_default
      entity
    end

    private
    attr_reader :entity

    def set_new_default
      entity.update(default: true)
    end

    def unset_default
      entity.class.update(default: false)
    end

    def already_default?
      entity.default?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
troo-0.0.3 lib/troo/actions/set_default.rb
troo-0.0.2 lib/troo/actions/set_default.rb