Changing admin route from 'admin/' to 'customadmin/' - ruby-on-rails

All. I've googled this quite a bit and I haven't found anything that'll work.
I want to change the admin route to something other than "admin". In this case, we'll just use "customadmin" as the desired name.
I've been toying with two solutions. One uses rewrites with Nginx, the other uses the routes in the spree application. Maybe, I should use both.
I'm doing this for the SSL layer, so in my 443 server section, (for the Nginx Solution) I tried the following:
locaton /customadmin/
{
rewrite ^/customadmin/(.*)$ /admin/$1 permanent;
}
Cool. That maps customadmin to all the admin routes. But What I don't want is for any random user to even get to the /admin section. Also, If I'm mapping 'customadmin' to 'admin' it'll probably create some sort of error.
Ideas:
*Should I use the 'internal' property? That is, in the admin location block, only go somewhere if the request is internal (or from a rewrite)
*Is there a way to namespace /admin in my spree application so that it uses 'customadmin' instead? I know it can be done (somewhat) by doing the following:
Spree::Core::Engine.routes.append do
scope :customadmin do
namespace :admin do
resources :my_model
resources :my_other_model
end
end
end
Environment Specs:
Spree: 2.1.2
Ruby: ruby-2.0.0-p353
rails: 4

Related

How do I explicitly set a rails engine routing proxy?

On my Rails 5.0.7 application I have Thredded (0.15.4) mounted as an engine in a namespace.
#routes.rb
namespace :home do
mount Thredded::Engine => "/communities", as: "thredded"
end
This allows me to access /home/communities to pull up the thredded interface.
Unfortunately, this also leads to unexpected behavior. The rails route proxy is not thredded as expected, but rather home_thredded. If I wanted to access a named route inside my engine, I need to do home_thredded.root_path instead of thredded.root_path
This is especially troublesome because thredded.root_path is actually referenced in the code of the thredded gem itself, which now throws an error.
Is there some way to explicitly set the routing proxy to avoid needing to include the namespace in the routing proxy?
A few things that I've considered and rejected:
I could directly edit the code in the offending file of the gem, but that would be annoying to maintain.
I could also write a decorator that overwrites the offending method so that it uses the correct rails routing proxy, but that would still mean that I would have to use the home_thredded.some_path anywhere rather than the expected thredded.some_path
I could not namespace the engine. This could work, but it would break the naming conventions of the rest of the application.
Have you tried:
scope :home do
mount Thredded::Engine => "/communities", as: "thredded"
end
That should add /home to your URI Pattern but leave your Controller#Action and Prefixs unchanged.

Rails ActiveAdmin Routing new Resources

I'm just starting out with Rails and I decided to try out ActiveAdmin last night. I was able to register a new resource name 'Pages' in my ActiveAdmin app, but there's one thing I can't figure out how to customize with it.
I create a new Page with ActiveAdmin, but it's published within the admin/.. path.
(e.g. mydomain/admin/page/1)
How do I change the routing so the page can be viewed at mydomain/page/1?
Are you able to change the routing of existing resources in ActiveAdmin?
I'm very new at Rails so I assume this is a pretty easy fix. I plan to run through some more tutorials/books so I can better understand routing.
You can change the default admin namespace.
To do so you have to go to config/initializers/active_admin.rb file and find the following configuration:
# Default:
# config.default_namespace = :admin
Uncomment the line and set the default_namespace to whatever you need.
However, if you need to turn off the namespace at all, you will have to set the default_namespace to false:
config.default_namespace = false
This will allow you to run the AA from the root.
By doing so be aware of changes in routes:
if changed the namespace to hello, the admin_games_path becomes hello_games_path;
if changed to no namespace, use normal routes: admin_games_path becomes games_path.

Routes in Engine mounted on subdomain do not inherit the constraints

Setup
Our current Rails app is made out of sub-apps that are mounted as engines. Typically these engines are mounted on a subdomain in the main routes.rb file as follows
mount MySubApp::Engine => '/', as: :sub_app, constraints: {subdomain: 'sub_app'}
The Problem
Routes within MySubApp's routes.rb file do not get the subdomain when using the named _url helpers. For example the following in apps/my_sub_app/config/routes.rb
MySubApp::Engine.routes.draw do
resources :foos
end
gives us sub_app.foo_url(5) but it results in
http://www.example.com/foos/5
when we want
http://sub_app.example.com/foos/5
tl;dr
How can I get the engine's mounting constraints passed to its named routes?
EDIT: A Workaround
While I'd still prefer a better solution, the following will work. You can wrap all the routes in each of the sub apps routes.rb files that could be mounted on a subdomain like so
MySubApp::Engine.routes.draw do
constraints Rails.application.routes.named_routes[:sub_app].constraints do
resources :foos
end
end
EDIT 2: A much less desirable workaround
A commenter (since deleted?) pointed out you can pass a subdomain option to the helpers but we'd like to avoid having to use sub_app.foo_url(5, {subdomain: 'sub_app'}) for every cross subdomain link. Even if we moved the subdomain name into an ENV var and made a wrapper, this is not DRY.
check out the guide it says you can do it by
namespace :Engine do
resources :controller, :methods
end
the Engine is just name spacing your code
#Aaron not sure if you ever got this fixed, but look into the
config.action_dispatch.tld_length
setting (on the engine's config). I'm not sure how it'll react with engines, but in our case it lets us handle the case of sub-subdomains for our staging server (so when we use the _url helpers with the staging server it correctly does subdomain.staging.domain.com, rather than subdomain.domain.com).

How to run copy of the app running on '/' on '/someurl' as well?

Let's say I have a site like Digg running on bookmarks.com and want to create bookmarks.com/girls which shares all the routes, but all the links point to /girls/<something> instead of just /<something>.
Without changing all the links in views, I would like to modify routes.rb to do something like this:
all_routes = lambda {
...
}
scope '/girls', &all_routes, constraint: { |r| r.session[:subsite] == girls }
scope '/', &all_routes
However I am stuck with trial and error and haven't been able to come up with a solution.
Bonus points: how would I route this, if I had dynamic multiple sub-sites?
I think you can achieve this by subclassing your Rails app, and then mounting it using Rack.mount in your routes
mount AnotherRailsApp, :at => "/something"
Alternatively, you could use a rackup file, so in config.ru:
map "/" do
run RailsApp::Application
end
map "/something" do
run AnotherRailsApp::Application
end
However, with either approach, I'm not sure Rails' helper methods for URLs will be able to generate valid URLs when mounted at "/something", and you're probably going to get some interesting issues with your data too, unless you have one db per app.

Prepend path prefix to all rails routes

I have a setup where nginx serves a rails application inside a specific subfolder
eg. http://myserver/railsapp/ and everything inside gets proxied to rails, if the first subfolder is different, it servers static files from another folder.
I haven't been able to find how to specify this behaviour in rails in an intelligent way. I mean, what I want is to specify an option like Rails.server_prefix = /railsapp so that all the routes get prepended automagically, both on the incoming requests and on the generated links.
You probably want to use the router's scope method with the :path argument:
Rails.application.routes do
scope(:path => '/railsapp') do
# the rest of your routes go here
end
end
See the docs for more info.

Resources