Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to locate/edit a recurrence in a schedule #167

Open
bigbean17 opened this issue Feb 7, 2024 · 0 comments
Open

How to locate/edit a recurrence in a schedule #167

bigbean17 opened this issue Feb 7, 2024 · 0 comments

Comments

@bigbean17
Copy link

Hi,

I have a model "Exam", each exam will have a schedule (Montrose::Schedule) which has lots of recurrences. For exam, an exam has a schedule with 2 recurrences:

  • from '12-23-2024' to '12-23-2025' at 9:30am every Monday
  • from '01-01-2026' to '01-31-2026' at 4:00pm every Sunday

If a user wants to edit the first recurrence, what my code will do is to find which record matches the selected recurrence and remove that record, then create a new one based on the new fields. My code looks like below:

class ExamController
  
  def update
      exam = Exam.find(params[exam_id])
      target_recurrence_index = -1
      exam.schedule.rules.each_with_index do |r, index|
         # compare starts, until, at and on of "r" and "edit_recurrence_param", if all same, set target_recurrence = index
      end
      
      if target_recurrence_index >= 0
        exam.schedule.rules.delete_at(target_recurrence_index)
        edited_recurrence = Montrose::Recurrence.new(edit_recurrence_param)
        exam.schedule << edited_recurrence
      end

  end
  
  private
  
  def edit_recurrence_param
      params.permit(::every, :starts, :until, :at, :on)
  end
end

I think this is a very bad way to do the edition, what could be the best practice to edit a recurrence? Any help would be appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant