Ask A Question

Notifications

You’re not receiving notifications from this thread.

Redmine Rails

Ondřej Láznička asked in Rails

Hi, i have trouble with ruby ( i dont know any ruby basics but im edititing redmine plugin ).

I prepared method
def getDateForRedmine( startDay, endDay, limit, date )

        datetime = Date.strptime(date.to_s,'%Y-%m-%d %H:%M:%s').to_datetime
        hour = datetime.hour.to_i
        minute = datetime.minute.to_i

        if hour >= startDay
            tempTime = hour + limit
            if tempTime < endDay
                return date.change(hour: tempTime).to_s(:db)
            elseif tempTime == endDay
                if minute > 0  then
                  return date.change(hour: startDay, day: 1).to_s(:db)
                else
                  return date.change(hour: tempTime).to_s(:db)
                end
            elseif tempTime > endDay
                if  hour > endDay
                    return getDateForRedmine(startDay,endDay, limit, date.change(hour: 8, day: 1) )
                else
                    remains = tempTime - endDay
                    return getDateForRedmine(startDay,endDay, remains, date.change(hour: 8, day: 1) )
                end
            end
        else

        end
    end

Which based on given values return datetime shifted.

Example of input
startDay = 8
endDay = 16
limit = 4
date = is current date

And it should return date shifted based on workday. But it keep returning nulll..

Reply

hour is never greater than startDay because a Date object converted to a DateTime always has hours / mins / seconds of 0, and startDay is 8. Although I suspect you wanted that to be start_hour ? Anyway that means the code path is always through the last else which returns nil. BTW you have a few syntax errors in here (shown in the highlighting above, such as 'elseif' which should be 'elsif').

Reply

Here is a version you can play with in the rails console until you get it to do what you want (which I cant quite figure out...).

require 'date'

def get_date_for_redmine( start_hour, end_hour, limit, now )

  puts "Running for #{start_hour}, #{end_hour}, #{limit}, #{now.to_fs(:db)}."

  hour = now.hour
  minute = now.minute

  if hour >= start_hour
    temp_hour = hour + limit
    if temp_hour < end_hour
      puts "Return 1"
      return now.change(hour: temp_hour).to_fs(:db)
    elsif temp_hour == end_hour
      if minute > 0  then
        puts "Return 2"
        return now.change(hour: start_hour, day: 1).to_fs(:db)
      else
        puts "Return 3"
        return now.change(hour: temp_hour).to_fs(:db)
      end
    elsif temp_hour > end_hour
      if  hour > end_hour
        puts "Return 4"
        return get_date_for_redmine(start_hour,end_hour, limit, now.change(hour: 8, day: 1) )
      else
        remains = temp_hour - end_hour
        puts "Return 5"
        return get_date_for_redmine(start_hour,end_hour, remains, now.change(hour: 8, day: 1) )
      end
    end
  else
    puts "Nothing Happened"
  end
end

puts get_date_for_redmine(8, 16, 4, DateTime.now)
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.