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
  • Loading branch information
Throne3d committed May 24, 2017
1 parent d8fef9e commit 25c6bd0
Show file tree
Hide file tree
Showing 2 changed files with 6 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
14 changes: 1 addition & 13 deletions app/models/gallery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Gallery < ActiveRecord::Base
has_many :galleries_icons
accepts_nested_attributes_for :galleries_icons, allow_destroy: true

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 @@ -21,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 25c6bd0

Please sign in to comment.