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-1.2.3 lib/polyblock/has_polyblock.rb
polyblock-1.2.2 lib/polyblock/has_polyblock.rb
polyblock-1.2.1 lib/polyblock/has_polyblock.rb
polyblock-1.2.0 lib/polyblock/has_polyblock.rb
polyblock-1.1.1 lib/polyblock/has_polyblock.rb
polyblock-1.1.0 lib/polyblock/has_polyblock.rb
polyblock-1.0.8 lib/polyblock/has_polyblock.rb
polyblock-1.0.7 lib/polyblock/has_polyblock.rb
polyblock-1.0.6 lib/polyblock/has_polyblock.rb
polyblock-1.0.5 lib/polyblock/has_polyblock.rb
polyblock-1.0.4 lib/polyblock/has_polyblock.rb
polyblock-1.0.3 lib/polyblock/has_polyblock.rb
polyblock-1.0.2 lib/polyblock/has_polyblock.rb
polyblock-1.0.1 lib/polyblock/has_polyblock.rb
polyblock-1.0.0 lib/polyblock/has_polyblock.rb
polyblock-0.9.9 lib/polyblock/has_polyblock.rb
polyblock-0.9.8 lib/polyblock/has_polyblock.rb
polyblock-0.9.7 lib/polyblock/has_polyblock.rb
polyblock-0.9.6 lib/polyblock/has_polyblock.rb
polyblock-0.9.5 lib/polyblock/has_polyblock.rb