Sha256: e5027ec4fe2213d2b52e6b5f2c33e2e9a1cd0b05464665537b67e1c9706a547a

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'rubygems'
require 'active_support'
require 'active_record'
require 'cockpit'
require 'acts-as-joinable'

def require_local(path, from = __FILE__)
  files(path, from) {|file| require file if File.extname(file) == ".rb"}
end

def files(path, from = __FILE__, &block)
  Dir.glob(File.expand_path(File.join(File.dirname(from), path))) {|file| yield file}
end

module ActsAsLayoutable
  def self.included(base)
    base.extend ClassMethods
    base.send :include, InstanceMethods
  end
  
  module ClassMethods

    def acts_as_layoutable(*args)
      acts_as_joinable_on :layouts, :class_name => "ActsAsLayoutable::Layout"
      # before_save :ensure_layout_exists
      
      options = args.extract_options!
      
      args.each do |action|
        define_method "#{action.to_s}_layout" do
          join_for(:parent, :layout, action)
        end
        define_method "#{action.to_s}_layout=" do |value|
          set_joined(:parent, :layout, action, value)
        end
        define_method "#{action.to_s}_layout_id" do
          result = join_for(:parent, :layout, action)
          result ? result.id : nil
        end
        define_method "#{action.to_s}_layout_id=" do |value|
          set_joined(:parent, :layout, action, value)
        end
      end
    end
    
    def template(layoutable)
      layoutable ? layoutable.template : nil
    end
  end
  
  module InstanceMethods
    
    def ensure_layout_exists
      if self.layouts.blank?
        self.layouts << Layout.first
      end
    end
    
    def layout
      self.layouts.first
    end
    
    def template
      self.layout.template
    end
  end
  
end

require File.dirname(__FILE__) + '/acts-as-layoutable/builder'
require_local "acts-as-layoutable/*"

ActiveRecord::Base.send(:include, ActsAsLayoutable)

def Layoutable(*args, &block)
  ActsAsLayoutable::Builder.define!(*args, &block)
end

def Layouts(*args, &block)
  Layoutable(*args, &block)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts-as-layoutable-0.0.1.7 lib/acts-as-layoutable.rb