Sha256: d98b7c8b31c356f76c2be7f61cdb24d20f4194613f677c8a239c54827f8d61d0

Contents?: true

Size: 1.3 KB

Versions: 12

Compression:

Stored size: 1.3 KB

Contents

module SiteFramework
  # Rack middleware class of `site_framework` which is responsible for loading
  # [Domain] model instance and [Site] model instance and putting them on
  # `Rails.application`.
  class Middleware

    # Middleware initializer method which gets the `app` from previous
    # middleware
    def initialize(app)
      @app = app
    end

    def call(env)
      # Create a method called domain which will return the current domain
      # name
      Rails.application.send :define_singleton_method, 'domain_name' do
        env['SERVER_NAME']
      end

      # Create `fetch_domain` method on `Rails.application`
      # only if it didn't already define.
      unless Rails.application.respond_to? :fetch_domain
        Rails.application.send :define_singleton_method, 'fetch_domain' do
          if defined? ActiveRecord
            Domain.find_by(nam: Rails.application.domain_name)
          elsif defined? Mongoid
            Site.where('domains.name' => Rails.application.domain_name).domains.first
          end
        end
      end

      Rails.application.send :define_singleton_method, 'site' do
        site = nil
        unless Rails.application.domain.nil?
          site = Rails.application.domain.site
        end
        site
      end

      Rails.application
      @app.call(env)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
site_framework-4.3.3 lib/site_framework/middleware.rb
site_framework-4.3.2 lib/site_framework/middleware.rb
site_framework-4.3.1 lib/site_framework/middleware.rb
site_framework-4.3.0 lib/site_framework/middleware.rb
site_framework-4.2.0 lib/site_framework/middleware.rb
site_framework-4.1.2 lib/site_framework/middleware.rb
site_framework-4.1.0 lib/site_framework/middleware.rb
site_framework-4.0.1 lib/site_framework/middleware.rb
site_framework-4.0.0 lib/site_framework/middleware.rb
site_framework-3.2.0 lib/site_framework/middleware.rb
site_framework-3.1.0 lib/site_framework/middleware.rb
site_framework-2.0.0 lib/site_framework/middleware.rb