Sha256: 0938948260fcd0525be1061913a02261a6d6e4e1191daf4ca4db03fc569c07bb

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

Contents

module Assetable
  module Core
    extend ActiveSupport::Concern
    
    included do
    end

    module ClassMethods
      def assetable *args
        if args.present?
          args.each do |arg|
            has_one :"#{arg}_association", -> { where(name: arg) }, class_name: "AssetAttachment", as: :assetable
            has_one arg, through: :"#{arg}_association", source: :asset
            accepts_nested_attributes_for :"#{arg}_association", allow_destroy: true
          end
        end
      end

      # Galleries
      def galleryable *args
        # By default, let's include a gallery. 
        unless args.include? :gallery
          has_one :gallery, as: :galleryable, dependent: :destroy
          accepts_nested_attributes_for :gallery
        end
        
        if args.present?
          args.each do |arg|
            has_one arg, -> { where(name: arg) }, class_name: "Gallery", as: :galleryable
            accepts_nested_attributes_for arg
          end
        end
      end
    end

    module InstanceMethods    
    end
  end
end

ActiveRecord::Base.send :include, Assetable::Core

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
assetable-0.1.10 lib/assetable/core.rb
assetable-0.1.9 lib/assetable/core.rb
assetable-0.1.8 lib/assetable/core.rb
assetable-0.1.7 lib/assetable/core.rb
assetable-0.1.6 lib/assetable/core.rb
assetable-0.1.3 lib/assetable/core.rb
assetable-0.1.2 lib/assetable/core.rb