Skip to content

Instantly share code, notes, and snippets.

@parndt
Forked from Siron/gist:5282262
Last active December 15, 2015 15:29
Show Gist options
  • Save parndt/5282335 to your computer and use it in GitHub Desktop.
Save parndt/5282335 to your computer and use it in GitHub Desktop.
module Refinery
module PhotoGallery
module Extensions
module Pages
def has_one_page_album
has_many :album_page, :as => :page, :dependent=> :destroy
has_many :album, :through => :album_page
has_many :photos, :class_name => Refinery::PhotoGallery::Photo, :through => :album , :order => "created_at ASC"
accepts_nested_attributes_for :album_page
module_eval do
def photos_for_page(params_page)
Refinery::PhotoGallery::Photo.where(:album_id => self.album.id).
includes(:album).
paginate(:page => params_page).
order("created_at ASC")
end
def album_page=(album_page_params)
# new
self.build_album_page if self.album_page.nil?
# destroy
if album_page_params[:album_id].blank?
self.album_page.destroy
Refinery::Admin::AlbumPageSweeper.sweep
# create or update if changed
elsif self.album_page.album_id.to_s != album_page_params[:album_id]
ap = album_page.find(album_page_params[:album_id]) || album_page.build
ap.attributes = album_page_params
ap.save
Refinery::Admin::AlbumPageSweeper.sweep
end
end
end
attr_accessible :album_page
end
end
end
end
end
ActiveRecord::Base.send(:extend, Refinery::PhotoGallery::Extensions::Pages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment