Skip to content

Commit

Permalink
Use has_many :through relation for galleries and icons
Browse files Browse the repository at this point in the history
(Fix recognizing galleries_icons relation with protected attributes)
  • Loading branch information
Throne3d committed May 24, 2017
1 parent 906de23 commit 18134a5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
5 changes: 5 additions & 0 deletions app/models/galleries_icon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ class GalleriesIcon < ActiveRecord::Base
belongs_to :gallery
accepts_nested_attributes_for :icon, allow_destroy: true

after_create :set_has_gallery
after_destroy :unset_has_gallery

def set_has_gallery
icon.update_attributes(has_gallery: true)
end

def unset_has_gallery
return if icon.galleries.present?
icon.update_attributes(has_gallery: false)
Expand Down
15 changes: 2 additions & 13 deletions app/models/gallery.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Gallery < ActiveRecord::Base
belongs_to :user
belongs_to :cover_icon, class_name: Icon

has_many :galleries_icons
has_and_belongs_to_many :icons, -> { order('LOWER(keyword)') }, after_add: :set_has_gallery, after_remove: :unset_has_gallery
has_many :icons, -> { order('LOWER(keyword)') }, through: :galleries_icons

has_many :characters_galleries
has_many :characters, through: :characters_galleries
Expand All @@ -20,16 +21,4 @@ def default_icon
def character_gallery_for(character)
characters_galleries.where(character_id: character).first
end

private

def set_has_gallery(icon)
return unless valid? # don't change icon status unless the gallery being saved is valid
icon.update_attributes(has_gallery: true)
end

def unset_has_gallery(icon)
return if icon.galleries.present?
icon.update_attributes(has_gallery: false)
end
end

0 comments on commit 18134a5

Please sign in to comment.