Sha256: 4fafc5d31b8a92358841c85ad65419e81fb8ea872cbda10eabf8c5940b7ecf7b

Contents?: true

Size: 1.56 KB

Versions: 12

Compression:

Stored size: 1.56 KB

Contents

module Rad::Controller::Http
  inherit Rad::Controller::Abstract

  def request; rad.workspace? ? workspace.request : nil end
  def response; rad.workspace? ? workspace.response : nil end

  def render *args
    if args.size == 1 and (status = Rad::Http::Response.decode_status_message(args.first))          
      response.set! status: status
      throw :halt, ""
    else
      super
    end
  end

  protected
    def render_content options
      if response
        response.set!(
          status: options[:status] || :ok, 
          content_type: options[:content_type] || Mime[params.format]
        )
      end
    
      if options[:location]
        redirect_to options[:location]
      else          
        super
      end
    end

  module ClassMethods      
    inheritable_accessor :actions_allowed_for_get_request, []
  
    def allow_get_for *methods
      methods = methods.first if methods.first.is_a? Array
      
      enable_protection_from_get_requests!        
      actions_allowed_for_get_request.push *(methods.collect(&:to_sym))
    end
  
    private
      def enable_protection_from_get_requests!
        unless respond_to? :protect_from_get_request
          define_method :protect_from_get_request do
            get = !(request.post? or request.put? or request.delete?)                
            if get and !self.class.actions_allowed_for_get_request.include?(action_name)
              raise "GET request not allowed for :#{action_name} action!"
            end            
          end
          before :protect_from_get_request
        end
      end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rad_core-0.2.6 lib/rad/controller/_http.rb
rad_core-0.2.5 lib/rad/controller/_http.rb
rad_core-0.2.4 lib/rad/controller/_http.rb
rad_core-0.2.3 lib/rad/controller/_http.rb
rad_core-0.2.2 lib/rad/controller/_http.rb
rad_core-0.2.1 lib/rad/controller/_http.rb
rad_core-0.2.0 lib/rad/controller/_http.rb
rad_core-0.0.30 lib/rad/controller/_http.rb
rad_core-0.0.29 lib/rad/controller/_http.rb
rad_core-0.0.28 lib/rad/controller/_http.rb
rad_core-0.0.27 lib/rad/controller/_http.rb
rad_core-0.0.26 lib/rad/controller/_http.rb