I need to use one of the resourceful controllers plugins - resources_controller/ resource_controller/make_resourceful as I have several polymorphic models and the models have to be either initialized/build depending on the route.
For example:
www.example.com/groups/1/pages
www.example.com/projects/1/pages
where page acts as polymorphic object as both Group and Project have many pages. So I am thinking of using one of the aforementioned plugins to make pages_controller adapt to both routes. All three plugins works fine and differences are just their implementation of recognizing the routes and loading the models based on that.
Now I want to add sub-domain support using Subdomain_fu plugin so the above example would be:
Site1.example.com/groups/1/pages
Site1.example.com/projects/1/pages
Site2.example.com/groups/2/pages
Site2.example.com/projects/2/pages
On looking at all the three plugins, I don't see any way for them to start loading the resources from my subdomain object, as the subdomain is not part of the route. Any advise on what I am trying to accomplish in a dry/restful way?
I don't know how to do that with resources_controller but i was able to pull off the same thing with the inherited_resources plugin.
Here is how i accomplished it:
In my application controller I set up a before_filter to find the subdomain:
def set_subdomain
#subdomain = Subdomain.find_by_url( request.host )
end
Then in my controllers Using inherited resources I set the #subdomain association using the very cool method "begin_of_association_chain"
protected
def begin_of_association_chain
#subdomain
end
Agile web development has great documentation.
Related
We are in the process of taking a few pages out of our rails app to be served separately (they are a few static pages with some content being managed through a cms). The urls will stay the same. Our own routing system in front of the servers will decide which request should go to the rails app and which to the static part.
My question is about path helpers that we use quite a bit throughout the rails app, such as link_to about_path that generate mahwebsite.com/about. As I understand I can just leave them be, they will still generate correct urls. My only concern is that for them to work I'll have to keep the routings in the routes file, which will have to be connected to the dummy controller methods. Seems like a lot of redundant code just to fool rails into creating path helpers.
Alternatively, I can hard-code links to the static pages. But before I start replacing a whole lot of code, I'd like to know if there is a clean Railsy way to keep the path helpers without having to route to the redundant controllers.
Thanks.
Why not just create your own helper method? E.G.
# application_controller.rb
def about_path
"mahwebsite.com/about"
end
helper_method :about_path
alias_method :about_url, :about_path
This will overwrite any Rails helper method and do exactly what you're after :)
Hope this helps - give me a shout if you've any questions or comments.
How about
resources :custom_pages, only: [:your_options] do
get :view/:page_id_or_whatever_for_identify
end
and do the following content with the controller?
I'm writing a configurable Rails engine. I have an authentication_helper configuration option to define which helper should be called in a before_action in all controllers needing authentication.
The problem is that I don't have access to the parent app's helpers from the engine's controllers. My understanding is that this happens because the engine is isolated.
I have considered using a block instead of a method name, but I'm not sure if that would work, or if I would be able to cleanly access the authorization logic from outside my controllers.
Active Admin, which I have used in the past, has a similar configuration option. I have noticed that their engine is not isolated, so perhaps I'm overrating the importance of engine isolation?
Is there an elegant way to have the benefits of engine isolation while also allowing this kind of customization? Or should I just forego isolation altogether?
EDIT #1
Brad Werth pointed my in the right direction, as this works with a regular controller inheriting from ApplicationController::Base:
module MyBigFancyEngine
class Engine < Rails::Engine
isolate_namespace MyBigFancyEngine
config.to_prepare do
# Make the implementing application's helpers available to the engine.
# This is required for the overriding of engine views and helpers to work correctly.
MyBigFancyEngine::ApplicationController.helper Rails.application.helpers
end
end
end
However, my engine's ApplicationController inherits from RocketPant::Base, which does not provide a helper method. I've tried to use a simple include (which works fine for regular controllers), but that doesn't work either (the controller can't find the helper).
Any ideas?
You can expose the implementing application's helpers available to the engine by including the following code in your engine.rb file:
engine.rb
module MyBigFancyEngine
class Engine < Rails::Engine
isolate_namespace MyBigFancyEngine
config.to_prepare do
# Make the implementing application's helpers available to the engine.
# This is required for the overriding of engine views and helpers to work correctly.
MyBigFancyEngine::ApplicationController.helper Rails.application.helpers
end
end
end
The RailsAdmin Engine is also isolated, but they have the same configuration options as you would like to implement. They have configurable before_filters for both authentication and authorization. Have a look at this.
As far as I can tell, they just subclass the parent controller like this::ApplicationController or instead you can configure one (ref).
For your controller you could just create your own EngineController, that inherits from RocketPant::Base and maybe just create a method there that calls the configured authentication method directly via send on the parent controller.
As the RocketPant::Base Class does not inherit from ApplicationController::Base I guess you have to find some custom way around this and can't go the normal ways for Rails Engines. Maybe you could also try to file an issue to the rocket_pant repo, to add the helper method. As far as I read they soon want to inherit from ApplicationController::Base anyway in 2.0 (ref).
I think you impulse on keeping the isolation is good - because a solution that relies on a method 'just being there' is a pain to debug if s.th. goes wrong in the app using your gem (i.e. typo in method name).
Further if your gem evolves, an some day it won't need the method, in an explicit way it's very convient to give a meainingful error:
You could provide a config method to be called in an initializer:
YourGem.configure do |config|
config.add_callback { MyApp.doIt() }
end
I found this discussion particularly insightful. There are also some interesting ideas in the Rails Engine API under Isolated engine helpers.
The Rails Engine API docs helped me figure out a good solution for url_helpers
You probably moved on by now, but if anyone else needs access to the "parent app" application helpers. You could always just include it explicitly in the application controller in your engine. like so:
include Rails.application.helpers
I am really confused about rails namespaces. I tried to create my own admin namespace so added namespace to routes, this works good. Then i added folder admin into controllers.
Admin::Controller
this is how my controllers in that folder looks.
but here comes the problem. How can i separate Helpers? rails automatically loads all helpers. I disabled that in config but now it wont load it manually like module Admin::ApplicationHelper.
How about next things what needs to be separated? Like i18N, sessions, flashes? Is there a tutorial for this problem? Im using Rails 4. Thanks for advices
As you've noticed rails defaults to including all helpers into all views. You can turn this off by adding
config.application_controller.include_all_helpers = false
This will result in only ApplicationHelper and the controller's helper being included. Adding
helper :foo
To a controller would result in FooHelper being included in addition to the defaults. If there are helpers that should be loaded for all of the admin controllers then add this to their base class. If you need anything more than this then consider using a rails engine (with the isolate_namespaces option turned on)
You only namespace controllers, views, models, and helpers, not everything else you mentioned. If you disabled autoloading helpers you'll have to manually require each one that you need:
require 'admin/admin_helper'
class Admin::Controller < ActionController::Base
... code ...
Same goes for any other helper such as application_helper, etc. Everything else, sessions, flashes, i18n and so on are merely methods from ActionController::Base that all controller's inherit. There's no namespacing these.
Going back to the helpers question: you namespace them the same way you namespace the controllers:
# app/helpers/admin/admin_helper.rb
module Admin::AdminHelper
... code ...
end
And then just require it in your admin controllers if you need to. I'd keep autoloading helpers enabled in order to forego having to require them everywhere.
Im using a Rails engine as a cms. It all works fine. Im adding devise to this.
My generated devise controllers inherit from Devise::SessionsController. But there are some filters that are run from another controller in the engine that wont run in this case. A lot of the site relies on these filters being run. Of course I could just duplicate them, but thats bad juju.
So my Question is: How can I make one controller run the filters from another? I would prefer not to edit either of the gems.
Multiple inheritance is not supported in Ruby. I think extracting the filters into a module and mixing them in would be the cleanest solution.
See for example:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html
There is another option available for Devise: config.parent_controller. It defaults to ApplicationController but can be changed to something else. In my case (a Rails API) I use ApiController. In config/initializers/devise.rb:
Devise.setup do |config|
# ... other configuration options
config.parent_controller = 'ApiController'
end
So we use the same controllers to serve both mobile and desktop views of our site. We also use action caching heavily to cache the html for a page in memcache. I've been trying to figure out a way to globally change the caching prefix for all mobile requests to "views-mobile/" instead of the standard "views/". That way the mobile and and desktop pages will be saved under a different namespace so there are no conflicts in memcache.
We could do this per caches_action method by creating a custom cache_path using the controller variable for is_mobile?, but we'd prefer to do it globally somehow. Any suggestions? I imagine this would require monkey-patching ActionController::Caching but I can't figure out where it generates the "views/" prefix.
I'm sorry, I was Rails nubie, so I don't really understand about your question, but if it right, is this what you mean?
This is on my routes.rb:
scope "/administrator" do
resources :users
end
I changed my users_path 'prefix' to administrator. Sorry if wrong :D
I actually ended up figuring this out myself. Basically ActionController::Base uses a function called fragment_cache_key to generate the cache key for a specific fragment (which is what ActionCaching uses deep down). So you basically override that method and include your own logic for how to generate the prefix. This is how my method override looks:
# Monkey patch fragment_cache_key
def fragment_cache_key(key)
ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, mobile_device? ? "views-mobile" : "views")
end
Where mobile_device? is my own function that figures out whether the user is requesting the mobile or desktop version of the site.