Sha256: f52d8283d09cbc30d037f11a424d981ec2e5a52fb46487591310824334a0c60a

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Layout
  module Base
    def self.included(base)
      base.class_eval do
        include InstanceMethods

        def self.layout_options
          @layout_options ||= []
        end

        def self.layout_options=(options)
          @layout_options = options
        end

        # Set current action's layout.
        # Works in the same old fashion, but you can have multiple calls to it.
        #
        #   set_layout "application"
        #   set_layout "site", :only => %w[show edit]
        #   set_layout "site", :except => %w[remove index]
        #
        def self.set_layout(name, options = {})
          self.layout_options << [name, options]
        end

        before_filter :choose_layout
      end
    end

    module InstanceMethods
      private
      def choose_layout
        self.class.layout_options.each do |name, options|
          if options[:only] && validates_action_for_layout(true, options[:only])
            self.class.layout(name)
          elsif options[:except] && validates_action_for_layout(false, options[:except])
            self.class.layout(name)
          end
        end
      end

      def validates_action_for_layout(compares_to, actions)
        [actions].flatten.collect(&:to_sym).include?(action_name.to_sym) == compares_to
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
layout-0.1.0 lib/layout/base.rb