Sha256: 2c78a93d4dc828350f7d0aa85241a4e883b8e9563c7a85dc8128696a750a537c

Contents?: true

Size: 1.28 KB

Versions: 67

Compression:

Stored size: 1.28 KB

Contents

module Users
  class DateJumperController < PgEngine.config.users_controller
    before_action only: :jump do
      if params[:quantity].blank?
        raise PgEngine::BadUserInput, 'Cantidad incorrecta'
      end
    end

    def jump
      start_date = Date.parse(params[:start_date])
      quantity = params[:quantity].to_i

      date =
        case params[:type]
        when 'calendar_days'
          start_date + (multiplier * quantity.days)
        when 'business_days', 'business_days_excluding_holidays'
          resolve_business_days(start_date, quantity)
        when 'weeks'
          start_date + (multiplier * quantity.weeks)
        when 'months'
          start_date + (multiplier * quantity.months)
        else
          # :nocov:
          raise PgEngine::Error, 'type no soportado'
          # :nocov:
        end

      render json: { date: }
    end

    private

    def resolve_business_days(start_date, quantity)
      exclude_holidays = params[:type].include?('excluding_holidays')
      options = { direction: params[:direction], exclude_holidays: }

      PgEngine::DateJumper.new(start_date)
                          .business_days(quantity, **options)
    end

    def multiplier
      if params[:direction] == 'forward'
        1
      else
        -1
      end
    end
  end
end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
pg_rails-7.6.5 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.6.4 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.6.3 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.6.2 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.6.1 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.6.0 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.7 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.6 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.5 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.4 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.3 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.2 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.1 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.5.0 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.4.3 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.4.2 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.4.1 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.4.0 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.3.5 pg_engine/app/controllers/users/date_jumper_controller.rb
pg_rails-7.3.4 pg_engine/app/controllers/users/date_jumper_controller.rb