Sha256: 3423a72b096f570df7a7054ede45bfca45b00aa97ba7ec6f4cdb59f0dc55707f

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module Polyblock
  module HasPolyblock
    extend ActiveSupport::Concern

    included do
    end

    module ClassMethods
      def has_polyblock(name, options={})
        defaults = Polyblock::Block::DEFAULTS
        settings = defaults.deep_merge(options)
        has_one name, -> { where(:name => name) }, settings[:association]
        accepts_nested_attributes_for name, settings[:nested_attributes]
        after_initialize do
          send("build_#{name}".to_sym) if send(name.to_sym).nil?
          send(name.to_sym).settings = settings
        end
        include Polyblock::HasPolyblock::LocalInstanceMethods
      end

      def polyblocks
        Polyblock::Block.select('polyblock_blocks.name')
          .where(:contentable_type => self.name)
          .group('polyblock_blocks.name')
          .map(&:name)
      end
      def has_polyblock?(name)
        self.polyblocks.include?(name)
      end
    end

    module LocalInstanceMethods
      def has_polyblock_content_for?(name)
        !self.send(name).nil? and !self.send(name).content.blank?
      end

      def polyblocks
        Polyblock::Block.select('polyblock_blocks.name')
          .where(:contentable => self)
          .group('polyblock_blocks.name')
          .map(&:name)
      end
      def has_polyblock?(name)
        polyblocks.include?(name)
      end
    end
  end
end

ActiveRecord::Base.send :include, Polyblock::HasPolyblock

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
polyblock-0.8.5 lib/polyblock/has_polyblock.rb
polyblock-0.8.4 lib/polyblock/has_polyblock.rb
polyblock-0.8.3 lib/polyblock/has_polyblock.rb
polyblock-0.8.2 lib/polyblock/has_polyblock.rb
polyblock-0.8.1 lib/polyblock/has_polyblock.rb
polyblock-0.8.0 lib/polyblock/has_polyblock.rb