Giter Club home page Giter Club logo

Comments (11)

danielgatis avatar danielgatis commented on May 27, 2024

Thank you guys,
I fixed it with the following code

module Apartment
  module Elevators
    class MySubdomain < Apartment::Elevators::Subdomain
      def call(env)
        request = Rack::Request.new(env)

        database = @processor.call(request)

        begin
          Apartment::Database.switch database if database
        rescue Apartment::DatabaseNotFound, Apartment::SchemaNotFound
          return [404, {"Content-Type" => "text/html"}, ["Not Found"]]
        end

        @app.call(env)
      end
    end
  end
end

from apartment.

bradrobertson avatar bradrobertson commented on May 27, 2024

hey sorry for the late response... that's basically what we do in our app also.

from apartment.

kizashi1122 avatar kizashi1122 commented on May 27, 2024

Please give me advice.

What file should I add the above code to?

from apartment.

bradrobertson avatar bradrobertson commented on May 27, 2024

@kizashi1122 this can live wherever you want. maybe app/middleware ?? It doesn't matter. Just as long as your module / classname corresponds to Rails autoloading. You could literally do this:

# lib/my_elevator.rb
class MyElevator < Apartment::Elevators::Subdomain
  # ...
end

from apartment.

kizashi1122 avatar kizashi1122 commented on May 27, 2024

Thank you. It works fine.

But one more step needed to accomplish it in my case.

  1. Put the following file to lib/my_elevator.rb as @bradrobertson mentioned
require 'apartment/elevators/subdomain' # <- this line is necessary
class MyElevator < Apartment::Elevators::Subdomain
  # ...
end
  1. Put some code to config/initializers/apartment.rb
require 'my_elevator'
# ...
Rails.application.config.middleware.use 'MyElevator' # instead of 'Apartment::Elevators::Subdomain'

Does it really work without the 2nd step?

Thanks,

from apartment.

bradrobertson avatar bradrobertson commented on May 27, 2024

no you need both. I was just explaining how to write the middleware.

For future reference this is nothing to do with Apartment itself. It's really just a rails question that could be asked on StackOverflow or something.

from apartment.

kizashi1122 avatar kizashi1122 commented on May 27, 2024

Thanks again.
Sorry about that.

But doesn't work with only 1st step. I have to learn Rails more.

I hope this feature will be implemented soon.

from apartment.

SvHu avatar SvHu commented on May 27, 2024

http://stackoverflow.com/questions/27188411/apartment-ruby-gem-want-to-catch-an-exception/28233828#28233828 this might help you.

from apartment.

kizashi1122 avatar kizashi1122 commented on May 27, 2024

Great help!

from apartment.

 avatar commented on May 27, 2024

// apartment.rb
require 'rescued_apartment_middleware'
...
...
...
Rails.application.config.middleware.insert_before ActiveRecord::Migration::CheckPending, Apartment::Elevators::RescuedApartmentMiddleware

// lib/rescued_apartment_middleware.rb

require 'apartment/elevators/subdomain'
 require 'apartment/tenant'

 module Apartment
  module Elevators
    class RescuedApartmentMiddleware < Apartment::Elevators::Subdomain
      def call(env)
        request = Rack::Request.new(env)
        # @processor is the current middleware instance and the call method will invoke the parse_tenant_name method.
        # if you need to change the subdomain database name you can change it by defining the parse_tenant_name method below
        database = @processor.call(request)
        begin
          Apartment::Tenant.switch!(database) if database
        rescue Apartment::TenantNotFound, ActiveRecord::NoDatabaseError
          Rails.logger.error "Error: Apartment says tenant was not found for #{database.inspect}"
          raise ActionController::RoutingError.new('Not Found')
          #return [404, {"Content-Type" => "text/html"}, ["Not Found"]]
        end
        @app.call(env)
      end
    end
  end
end

// application_controller.rb

 rescue_from Apartment::TenantNotFound do |exception|
    ActionController::RoutingError.new('Not Found')
 end

the above code resolved my issue in Rails 4.2.5

from apartment.

Xosmond avatar Xosmond commented on May 27, 2024

I don't know why rescue is not working, anybody knows?

I had to changed it to:

module Apartment
  module Elevators
    class RescuedApartmentMiddleware < Apartment::Elevators::Subdomain
      def call(env)
        request = Rack::Request.new(env)
        database = @processor.call(request)
        if database
          result = Apartment::Tenant.switch!(database) rescue "notfound"
          if result == "notfound"
            Rails.logger.error "Error: Apartment says tenant was not found for #{database.inspect}"
            raise ActionController::RoutingError.new('Not Found')
          else
            @app.call(env)
          end
        else
          @app.call(env)
        end
      end
    end
  end
end

from apartment.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.