Created
May 17, 2011 18:33
-
-
Save nurey/977064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
May 17, 2011
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
g = ScheduleGroup.new(:name=>'test')
TypeError: can't convert Symbol into Array
from /Volumes/Devel/manager/app/models/abstract_group.rb:11:in `concat'
from /Volumes/Devel/manager/app/models/abstract_group.rb:11:in`grouping'
from /Volumes/Devel/manager/app/models/schedule_group.rb:3:in `<class:ScheduleGroup>'
from /Volumes/Devel/manager/app/models/schedule_group.rb:1:in`<top (required)>'
…On 2011-05-17, at 3:00 PM, samuelkadolph wrote:
``` ruby
module AbstractGroup
extend ActiveSupport::Concern
included do
class_inheritable_accessor :groupings
self.groupings = []
before_destroy :removable?
end
module ClassMethods
def grouping(*attributes)
groupings.concat(*attributes)
end
end
def removable?
self.class.groupings.all? { |grouping| send(grouping).empty? }
end
end
```
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/977064
Sorry, make that groupings.concat(associations)
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment