Sha256: 50f1163d9ea5027b87ba5f237b16a4f7c928aac3866fd57537a3e07703343edf

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module Polygallery
  module HasPolygallery
    extend ActiveSupport::Concern

    included do

    end

    module ClassMethods
      def has_polygallery(title='gallery', options={})
        defaults = Polygallery::Gallery::DEFAULTS
        settings = defaults.deep_merge(options)
        has_one title.to_sym, -> { where(:title => title.to_s) }, settings[:association]
        accepts_nested_attributes_for title.to_sym, settings[:nested_attributes]
        after_initialize do
          send("build_#{title.to_s}".to_sym) if send(title.to_sym).nil?
          send(title.to_sym).settings = settings
        end
        include Polygallery::HasPolygallery::LocalInstanceMethods
      end

      def polygalleries
        self.reflect_on_all_associations(:has_one).select{|a| a.foreign_key == 'galleryable_id'}.map(&:name)
      end

      def has_polygallery?(title)
        self.polygalleries.include?(title)
      end
    end

    module LocalInstanceMethods
      def polygalleries
        Polygallery::Gallery.select('polygallery_galleries.title')
          .where(:galleryable => self)
          .group('polygallery_galleries.title')
          .map(&:title)
      end

      def has_polygallery?(title)
        polygalleries.include?(title)
      end
    end
  end
end

ActiveRecord::Base.send :include, Polygallery::HasPolygallery

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
polygallery-0.0.2 lib/polygallery/has_polygallery.rb
polygallery-0.0.1 lib/polygallery/has_polygallery.rb