Sha256: 8fccaec0d2cd1b6dd60c115500d8809f12a02ae0570f7063a2257800e1c94ddd

Contents?: true

Size: 1.13 KB

Versions: 26

Compression:

Stored size: 1.13 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, interaction)
      case value
      when ActiveSupport::Duration
        (value == 0 && strip?) ? super(nil, interaction) : value
      when Hash
        if value[:value].present? && (value[:value].to_i != 0 || !strip?)
          value[:value].to_i.send(value[:unit])
        else
          super(nil, interaction)
        end
      else
        super
      end
    end

    private

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

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
roqua-support-0.4.6 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.4.5 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.4.4 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.4.3 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.4.2 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.4.1 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.4.0 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.3.5 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.3.4 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.3.3 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.3.2 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.3.1 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.3.0 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.34 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.33 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.32 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.31 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.30 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.29 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb
roqua-support-0.1.28 lib/roqua/core_ext/active_interaction/filters/duration_filter.rb