rails overriding/scoping views for multi tenancy app - ruby-on-rails

I'm wondering how I can scope my views. I want to have custom themes depending on the organization, I can use render directly on the show/index actions... and that works, but I would have to override a lot of actions on my application. I would prefer to do it on the controller level and tried doing it with prepend_view_path but it didn't except the variable as undefined.
class EditionsController < ApplicationController
helper_method :current_organization
prepend_view_path "app/views/#{current_organization.slug}/editions" #doesn't work
def show
#edition = Edition.find(params[:edition_id])
#page = #edition.pages.first
render template: "#{current_organization.slug}/editions/show" #works
end
Any ideas?
Also tried: (with same error)
append_view_path(File.join(Rails.root, "app/views/#{current_organization.slug}"))

custom themes depending on the organization
Surely it would make more sense to define custom layouts & CSS rather than entirely different view sets for each company?
--
I would personally do this:
#app/layouts/application.html.erb
<%= stylesheet_link_tag "application", controller_name ... %>
This will give me the ability to style the different pages as per the theme. Obviously a constriction on what you want, but I hope it shows how you could modularize your controllers etc
--
If you wanted to create different "themes" (IE having a completely different view structure per tenant), you'll want to use the prepend_view_path helper, as described here:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
prepend_view_path("views/#{current_organization.slug}")
end

Try to remove editions in prepend_view_path
prepend_view_path "app/views/#{current_organization.slug}"
Make sure what the way was added. If it doesn't add before_filter

Related

Rails. Different application layouts for different parts of the site.

I'm building a web site with a part that users can browse and administrative panel. How can use different application layouts for different parts of the site? Is it possible?
You can use layout method to render different layout for different controllers.
class MyController < ApplicationController
layout :admin_layout
private
def admin_layout
# Check if logged in, because current_user could be nil.
if logged_in? and current_user.is_able_to('siteadmin')
"admin"
else
"application"
end
end
end
You can use seperate views, for example if your run rails g controller home index you get a home_controller.rb and in the views folder you get a home folder with an index view. In your controller you can put your logic there and present it in the view.
Check this link seems to be good for beginners

Accessing model data from numerous nested layouts in Rails

Let's say I have a nested layout in Rails, as described in Rails Guides, where my application.html.erb file has:
...
<%= content_for?(:content) ? yield(:content) : yield %>
...
somewhere in it.
In both the application layout, and in the sub-layout, I need to access data from models.
I've found a solution from this question. I could put something like this in my ApplicationController:
class ApplicationController < ActionController::Base
before_filter :get_main_layout_stuff, :get_sub_layout_stuff
private
def get_main_layout_stuff
#cart = find_cart
end
def get_sub_layout_stuff
#categories = find_categories
end
end
and then in any controllers not using the sub layout, I can just say:
skip_before_filter :get_sub_layout_stuff
This works fine. However, if I start to have more layouts, say, a dozen, with many layers of nesting, and where maybe a layout needs specific information based on the contents of the URL, then it becomes unwieldy. I have to either list a million skip_before_filters in every controller, or I have to remember exactly which set of functions to add as a before_filter in each controller. Neither solution is very DRY, when I'm already specifying which layout I'd like in each controller.
So my question is: how can I get the right information to each layout in the hierarchy of layouts without having a crazy number of before_filters? Is there way to automatically load the required data based on the layout requested and then recursively go back to load the required data for each of parent layouts? Or maybe is there a way to have a "controller" for each layout that gets called whenever the layout is required? Or am I thinking about this problem in completely the wrong way?

What is the most effecient way to get and view commonly used data inside a Rails application?

I have three models all associated with eachother via has_many :through method.
ProgramCategory
ProgramSubcategory
Program
Inside my application, I need to use ProgramCategory.title, ProgramSubcategory.title and Program.title very often. Lets say, there'll be a dynamic sidebar menu and it will look like this:
|- Shows (ProgramCategory)
|- Action (ProgramSubcategory)
|- Lost (Program)
|- Games of Thrones (Program)
|- Dexter (Program)
Since I'm aware of power of application_controller, application_helper and partials; I feel lost about combining all of these together to find the most appropriate way.
Where and how should I call my models? Where should I build my methods to have access it through all of my controllers? Should I simply create a partial and render it at application layout?
I need some expert enlightment, please...
Thanks.
This navigation bar is not a core part of the data you are showing, it will be more or less the same for all pages. Thus, it does not belong to the controller.
Make it a helper method, and cache its results in the view:
app/helpers/sidebar_helper.rb
module SidebarHelper
def sidebar_data
# blahblah, use any tag helper (include it here if necessary)
# just remember to
end
end
app/controllers/your_controller.rb
class YourController < ApplicationController
helper :sidebar
# ...
(or put the helper method in your application helper to have it available everywhere)
app/views/application/_sidebar.html.haml
- cache do
# well, put here whatever you need to output the sidebar
# use the sidebar_data, which should return precooked data on
# which you can build your nested <ul>s.
# just indent it two spaces, so it's inside the "cache do" block
or app/views/application/_sidebar.html.erb
<% cache do %>
same as above, don't worry about indentation
<% end -%>
and include the partial where appropriate
<%= render 'sidebar' %>
If this sidebar shows in every view throughout your application, you can it to your application layout. Then add a before filter in your application controller to get the data, this will get executed for each action in every controller that inherits from application controller. You can also restrict it to specific actions, only :index, and :show (for each controller) for example.
class ApplicationController < ActionController::Base
before_filter :get_side_bar, :only => [:index, :show]
def get_side_bar
##sidebar = some code
end
end
Then you can use helper method (if needed) and render #sidebar in your application layout.
If you need to skip this action for some controllers you can do that as well, this one will skip application controller before filter for anything but index inside of the OtherController:
class OtherController < ApplicationController
skip_before_filter :get_side_bar, :except => [:index]
end

Asp.net MasterPage equivalent in Ruby on Rails, Trying to define a site wide layout

Asp.net WebForms and MVC has a concept of Masterpages which make it easy to define a one time layout for all the page of your site. In Rails I'm struggling to find an equivalent usage pattern or feature.
From what I've read it's really easy to define a layout in every action with:
layout: 'viewname'
Now that seemed pretty ceremonial to include in every controller so I added:
layout: 'application'
in the base ApplicationController.
So far this is working ok unless I have a more specific layout in the view pages. Is this common technique for getting a consistent style in your Rails application?
Imagine a simplified blog where we have a controller called PostsController which has two actions: index and show
The index action is called when the user hits http://yourwebsite.com/posts - this action displays all of the available blog posts.
The show action is called when a user gets a specific blog article - i.e. http://yourwebsite.com/posts/article-about-something-interesting
Let's say that we want the index page to have a two column layout and we want the show page for each blog-article to have a three column layout. To achieve this, we would simply define two separate layouts (in app/views/layouts folder) - we'll call the two column layout "application" and we'll call the three-column layout "alternate".
In order to get the index page to use the two-column layout and the show page to use the three-column layout, we could just do the following in our controller:
class PostsController < ApplicationController
def index
#posts = Post.all
render :layout => "application"
end
def show
#post = Post.find(params[:id])
render :layout => "alternate"
end
end
If we want all actions to use the same layout, we can just do this:
class PostsController < ApplicationController
layout "application"
def index
#posts = Post.all
end
def show
#post = Post.find(params[:id])
end
end
Finally, if we do not specify which layout we want to use, then Rails will by default, render any layout which has the same name as the resource we are displaying. So in our example, where our resources are called "Posts", if we define a third layout called posts.html.erb (in app/views/layouts) then Rails will automatically use that layout when the user renders any of the actions in the PostsController - providing of course that we have not explicitly asked Rails to render another layout....
Hope it helps,
This PDF book excerpt from Rails for .Net Developers has a pretty good explanation of Rails layouts, along with a comparison to ASP.Net MasterPages. Since it seems to work pretty well, it's probably used fairly often, at least by developers familiar with the master page concept.

Rails Sub-controllers?

I'm pretty new to Rails and have an issue which I can't quite get my
head around as to the architecturally 'correct' way of doing it.
Problem relates to what I kinda call sub-controllers. The scenario is
this:
I have a series of pages, on which is a panel of some form containing
some information (think the user panel on gitHub top right).
So, in my app, I have controllers that generate the data for the pages
and render out the responses which is fine, but when it comes to this
panel, it seems to me that you would want some sort of controller action
dedicated to generating this panel and it's view.
Question is, how do you go about doing this? How do I render a 'sub
controller' from within a view?
I would put the logic in a helper or a module. (http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html)
Then render partials where you want these things displayed. (http://api.rubyonrails.org/classes/ActionView/Partials.html)
Like Herman said, if it's logic that you need generated after the controller hands off to the view (ie, the Pages controller generates a page view, but you want a customized panel) then put it in a helper. Or, call a separate method in your Pages controller before handing off to the view. Or, if it's a lot of logic, create a Module and stick it in your /lib folder. So you could have a whole Panel module with methods that generate different parts of your Panel and which are called by your controller. But if you want to call these methods from within the view, then you should use a helper instead.
I dont think a module is what is required here, modules are required for shared behaviour across a small subset of your classes.
What I think is required here is the understanding of the inheritance of ApplicationController and also layouts
so, for example, my layout might look like:
<html>
<head><title>Foo</title></head>
<body>
<%= render :partial => (current_user ? "/shared/user_widget_bar" : "/shared/login_bar") %>
<%= yield %>
</body>
</html>
Any code that i want to use for it would go in my ApplicationController since it would be shared across the majority of my app:
before_filter :generate_user_widget
def generate_user_widget
if current_user
#avatar = ...
#unread_messages = ...
end
end
I understand that it might be cleaner for it to belong in a separate controller BUT honestly, unless the code is huge, it doesn't matter and can even still be put inside a module which is then included by ActionController. However it does need to be inside ApplicationController if you consider the scope of it.
If there are more related pages, say for example, you have a Rails app that manages multiple sites and you want shared behaviour across a particular site, try creating a parent controller which has no actions and only private methods, any controllers that need to have access to those methods can inherit off it. That way you can apply before filters to all controllers which inherit off it, saving you the pain of forgetting to add one in your non-parent controllers.
e.g:
class SiteA::SiteAParentController < ApplicationController
before_filter :generate_user_widget
...
end
class SiteA::ProductController < SiteA::SiteAParentController
def index
...
end
end
well, if you really need to call a controller action from the view, you can use components. They were part of the framework, now they only exist as plugins. One such plugin that seems to be well maintained is here: http://github.com/cainlevy/components/tree/master
from its docs:
== Usage
Note that these examples are very simplistic and would be better implemented using Rails partials.
=== Generator
Running script/generator users details will create a UsersComponent with a "details" view. You might then flesh out
the templates like this:
class UsersComponent < Components::Base
def details(user_or_id)
#user = user_or_id.is_a?(User) ? user_or_id : User.find(user_or_id)
render
end
end
=== From ActionController
class UsersController < ApplicationController
def show
return :text => component("users/detail", params[:id])
end
end
=== From ActionView
<%= component "users/detail", #user %>

Resources