Sha256: a6edfb4f23790d80201f9e38c94d1835f420211c0604a3d52740d20e135a2a15

Contents?: true

Size: 1.1 KB

Versions: 10

Compression:

Stored size: 1.1 KB

Contents

I18n.backend.store_translations :en, active_interaction: {
  types: { duration: 'Duration' }
}

module ActiveInteraction
  class Base
    # @!method self.duration(*attributes, options = {})
    #   Creates accessors for the attributes and ensures that the value passed to
    #     the attributes is a  ActiveSupport::Duration.
    #   Value can be a hash with a value and unit key
    #
    #   @!macro filter_method_params
    #   @option options [Boolean] :strip (false) Make nil if value is 0.
    #
    #   @example
    #     duration :first_name
    #   @example
    #     duration :first_name, strip: true
  end

  # @private
  class DurationFilter < Filter
    register :duration

    def cast(value)
      case value
      when ActiveSupport::Duration
        (value == 0 && strip?) ? super(nil) : value
      when Hash
        if value[:value].present? && (value[:value].to_i != 0 || !strip?)
          value[:value].to_i.send(value[:unit])
        else
          super(nil)
        end
      else
        super
      end
    end

    private

    def strip?
      options.fetch(:strip, false)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
roqua-support-0.1.21 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.20 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.19 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.18 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.17 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.16 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.15 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.14 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.13 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.12 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb