Sha256: 841064b663f4c23c2fafb301dadfdcab3150d093c73e7f138add9836df265259

Contents?: true

Size: 1.42 KB

Versions: 38

Compression:

Stored size: 1.42 KB

Contents

module Rack
  module Formtastic
    class Samples
      def initialize(app)
        @app = app        
      end
      
      def call(env)
        @status, @headers, @body = @app.call(env)
        @env = env
  
        @body = '' if favicon?
        @body = static_file if !favicon? && static_file?
        @body = stylesheet if stylesheet?
        @headers ||= {}
        @headers['Content-Type'] = mime(extension)
        [@status, @headers, @body]
      end
      
      def static_file?
        !stylesheet?
      end
      
      def stylesheet?
        @env["PATH_INFO"] =~ /\.(css)/
      end
      
      def favicon?
        @env["PATH_INFO"] =~ /favicon.ico$/
      end
      
      def extension
        @env["PATH_INFO"].split(".").last
      end
      
      def mime(extension)
        mimes[extension] || mimes['html']
      end
      
      def mimes
        {
          'ico' => 'image/x-icon',
          'html' => 'text/html',
          'css' => 'text/css',
          'js' => 'text/javascript'
        }
      end
      
      def static_file
        ::File.open(file_path)
      end
      
      def stylesheet
        ::File.open(::File.join("../app/assets/stylesheets", file_path))
      end
      
      def file_path
        @env["PATH_INFO"].gsub(/^\//, '').gsub(/^$/, 'index.html')
      end
      
    end
  end
end

use Rack::ContentLength
use Rack::Formtastic::Samples

app = lambda { |env| [200, @headers, @body] }
run app

Version data entries

38 entries across 38 versions & 3 rubygems

Version Path
formtastic-5.0.0 sample/config.ru
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/formtastic-4.0.0/sample/config.ru
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/formtastic-4.0.0/sample/config.ru
formtastic-4.0.0 sample/config.ru
formtastic-4.0.0.rc1 sample/config.ru
formtastic-3.1.5 sample/config.ru
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/formtastic-3.1.4/sample/config.ru
formtastic-3.1.4 sample/config.ru
formtastic-3.1.3 sample/config.ru
formtastic-3.1.2 sample/config.ru
formtastic-3.1.1 sample/config.ru
formtastic-3.1.0 sample/config.ru
formtastic-3.1.0.rc2 sample/config.ru
formtastic-3.1.0.rc1 sample/config.ru
formtastic-3.0.0 sample/config.ru
formtastic-3.0.0.rc2 sample/config.ru
formtastic-2.3.1 sample/config.ru
formtastic-2.3.0 sample/config.ru
formtastic-3.0.0.rc sample/config.ru
formtastic-2.3.0.rc4 sample/config.ru