Sha256: b89c3886c6673cf35121a9c350de96c1c901c99ac6da63f8e94a1d0f8e6dc0f9

Contents?: true

Size: 1.49 KB

Versions: 25

Compression:

Stored size: 1.49 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]

        cattr_accessor "#{name}_settings".to_sym
        send("#{name}_settings=".to_sym, settings)

        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

25 entries across 25 versions & 1 rubygems

Version Path
polyblock-0.9.4 lib/polyblock/has_polyblock.rb
polyblock-0.9.3 lib/polyblock/has_polyblock.rb
polyblock-0.9.2 lib/polyblock/has_polyblock.rb
polyblock-0.8.8 lib/polyblock/has_polyblock.rb
polyblock-0.8.7 lib/polyblock/has_polyblock.rb