GithubHelp home page GithubHelp logo

Comments (2)

demetrios avatar demetrios commented on August 15, 2024

Any luck with this?

from fullcalendar_rails.

foncho avatar foncho commented on August 15, 2024

Oh! Yes, sorry for not answering before...

Rails 3.1 allows you to insert only one item at once into your database, so you need to manually tell him that not passed the nested validation through self.events.new(:some_attributes).save(:validate => false). Four days to figure it out, because the nested insertion ban is a new feature in this version, due to security reasons (via Rails 3.1 specifications).

I also have got to integrate your solution with IceCube gem (great solution for recurrence calculations). Here I put the code I develop for that, I hope you find it useful:

# encoding: UTF-8

class EventSeries < ActiveRecord::Base
  include IceCube
  attr_accessor :title, :description, :event_series_id, :commit_button
  attr_accessible :id, :title, :description, :frequency, :period, :starts_at, :ends_at, :event_series_id, :commit_button

  validates :frequency, :presence => :true
  validates :period, :presence => true
  validates :starts_at, :presence => true
  validates :ends_at, :presence => true
  validates :title, :presence => true
  validates :description, :presence => true

  has_many :events, :dependent => :destroy
  accepts_nested_attributes_for :events

  before_save :generate_recurrence

  def generate_recurrence
    days = number_of_days
    if period.to_i == days.count
      create_events_until(END_TIME)
    else
      self.errors.add :period, "Repeats and number of days selected are not the same. Please, fix it and try it out again."
    end
  end

  def create_events_until(end_time)
    start = starts_at
    final = ends_at
    new_starts_at, new_ends_at = start, final
    schedule = IceCube::Schedule.new(start.to_time, :end_time => end_time)

    # Generate the schedule and the recurrence rules
    number_of_days.each do |day|
      if day.downcase.include?('mon') then schedule.add_recurrence_rule IceCube::Rule.weekly.day(1) end
      if day.downcase.include?('tue') then schedule.add_recurrence_rule IceCube::Rule.weekly.day(2) end
      if day.downcase.include?('wed') then schedule.add_recurrence_rule IceCube::Rule.weekly.day(3) end
      if day.downcase.include?('thu') then schedule.add_recurrence_rule IceCube::Rule.weekly.day(4) end
      if day.downcase.include?('fri') then schedule.add_recurrence_rule IceCube::Rule.weekly.day(5) end
      if day.downcase.include?('sat') then schedule.add_recurrence_rule IceCube::Rule.weekly.day(6) end
      if day.downcase.include?('sun') then schedule.add_recurrence_rule IceCube::Rule.weekly.day(7) end
    end

    # Insert all the occurrence into the database individually, passing over the Rails-3.1's nested insertions ban
    occurrences = schedule.all_occurrences
    occurrences.each do |occurrence|
      self.events.new(
        :title => title,
        :description => description,
        :event_series_id => id,
        :starts_at => occurrence,
        :ends_at => occurrence + 1.hour
      ).save(:validate => false)
    end
  end

  def number_of_days
    days = []
    frequency.each do |item|
      # Taking out only the not null elements
      unless item.nil? or item == '' 
        days << item
      end
    end
    return days
  end
end

Thx...

from fullcalendar_rails.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.