Sha256: 8131ff31bf7e8f681ea527759ddec426bcd3e26d5fd59a858161446fcffb7f69

Contents?: true

Size: 998 Bytes

Versions: 6

Compression:

Stored size: 998 Bytes

Contents

require "ledge/columns"

module Ledge
  module UsesLedge
    extend ActiveSupport::Concern

    included do
    end

    module ClassMethods
      def uses_ledge options = {}
        cattr_accessor :field
        self.field = (options[:field] || :content).to_s

        include Ledge::UsesLedge::LocalInstanceMethods
      end
    end

    module LocalInstanceMethods

      def method_missing method, *args, &block
        if method.to_s == "#{self.field}_ledge"
          content = JSON.parse read_attribute(self.class.field)
          return Ledge::Columns.new content
        else
          super
        end
      end

      def respond_to? method
        if method.to_s == "#{self.field}_ledge"
          true
        else
          super
        end
      end

      def ledge_init
        self.content ||= '{"columns": [{"rows":[]}]}'
      end

      def initialize hash = {}
        super
        ledge_init
      end
    end
  end
end

ActiveRecord::Base.send :include, Ledge::UsesLedge

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ledge-0.2.4 lib/ledge/uses_ledge.rb
ledge-0.2.3 lib/ledge/uses_ledge.rb
ledge-0.2.2 lib/ledge/uses_ledge.rb
ledge-0.2.1 lib/ledge/uses_ledge.rb
ledge-0.2.0 lib/ledge/uses_ledge.rb
ledge-0.1.0 lib/ledge/uses_ledge.rb