Skip to content

Instantly share code, notes, and snippets.

@nurey
Created May 17, 2011 18:33
Show Gist options
  • Save nurey/977064 to your computer and use it in GitHub Desktop.
Save nurey/977064 to your computer and use it in GitHub Desktop.
class ScheduleGroup < ActiveRecord::Base
include AbstractGroup
has_and_belongs_to_many :schedules
end
class Schedule < ActiveRecord::Base
has_and_belongs_to_many :schedule_groups
end
module AbstractGroup
before_destroy :removable?
def removable?
self.schedules.empty? # self.collection.empty?
end
end
@samuelkadolph
Copy link

module AbstractGroup
  extend ActiveSupport::Concern

  included do
    before_destroy :removable?
  end

  def removable?
  end
end

@samuelkadolph
Copy link

module AbstractGroup
  extend ActiveSupport::Concern

  included do
    class_inheritable_accessor :groupings
    self.groupings = []
    before_destroy :removable?
  end

  module ClassMethods
    def grouping(*associations)
      groupings.concat(*associations)
    end
  end

  def removable?
    self.class.groupings.all? { |grouping| send(grouping).empty? }
  end
end

@nurey
Copy link
Author

nurey commented May 17, 2011 via email

@samuelkadolph
Copy link

Sorry, make that groupings.concat(associations).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment