Rails: Making widgets - ruby-on-rails

I want to create a widget-like entity for my Rails app. For example, I'd like to be able to render a list of products marked as featured and have it work in any view file.
My first thought was to try to do this using a partial. Is this the best way? Will I be able to achieve my overall goal of having a widget I can inject anywhere? What's the best way to implement this?

Markup that is shared across many view files should be placed in a shared partial.
In this case, your partial will depend on having a collection of products available. You will need to query this collection in each controller action that corresponds to a page containing the widget. This sounds like a good way to do it to me.

Related

What is the use of partials in Rails?

In Rails why would I ever use partials? Explain it as if I'm 5 years old. I simply do not get why anyone would ever use them.
First thing, Please have a read here Official Rails guide for partials .
Now some benefits
It keeps your view clean and systematic, DRY Philosphy .
The most important where
partial comes into picture is when you want to reuse some of the
component amongst various view. Usually developer create a kind of
shared/common folder where partial sits and are used amongst
various view.
It’s also easy to conditionally load partials using Rails’ “if” or “unless” statements
It is beneficial where a template needs to iterate over a collection and render a sub template for each of the elements.
Your different partials can also have different layouts.
The Partial API here list all the various methods which will make you understand it's benefit.
Separating your view into partials can also help in in proper fragment caching (Fragment Caching) of some portion of you webpage. Better management.
If you are into Metaprogramming then you can add that flavor in your partials too, by creating dynamic helpers. As Stackoverflow answer here
They are a handy way to avoid repeating yourself.
For example, you may have several pages that display a menu. Instead of repeating the markup for the menu in every view, you just throw it in a partial and render on every page.
There are other cases where complex views are made more manageable by breaking them into several partials.
You normally use partials to reuse code: let's say you have a list of posts and each post has a small preview with image, title and excerpt. In your blog homepage you have a list of posts, when you group posts by year, you show a list of posts, when you search for a term you have to display a list of posts.
Instead of repeating the logic to display a post preview, you can move that logic to a partial and keep referring to that whenever you need it. You can keep your code DRY and write less code. Moreover, if you realize you want to add something new, you can just change the partial instead of going and hunting for the templates which display the post previews all over your application.
Well, for the exact same reason that you want to use methods, which is reusing code. Say you have a status area in your application, that you want to show in different places. You can just put the view code for that status area in a partial and then use that partial on the corresponding pages.
Since partials can also take parameters, they make it very easy to reuse view code. Also, you can make partials dedicated for certain models of your application. This way, you can just call render #model and the correct partial is picked by naming conventions.
A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts.
For example, you might have a login form that you want to display on 10 different pages on your site. Rather than writing the form code 10 times, you can write it once in a partial, and in each of the 10 pages, simply include that partial at the appropriate place in the layout. If necessary, you can pass local instance variables to the partial to make them available to it.
That way, if you need to change the form, you only need to change the code in the partial, one time, rather than changing it 10 times across all of your layouts.
Here is the guide on partials: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
And more discussion on getting data/variables into partials: Pass a variable into a partial, rails 3?

ASP.NET MVC: different functionality for authorized users - single view

Let's say that I have an MVC view that has one set of features available to non authorized users, and an extended set of features that is available to authorized users. Currently, I have implemented this by marking up my view with
#if(User.IsInRole(...)) {...}
in several places in the view file. This wasn't a big deal when it was just 2 or 3 things that authenticated users could do beyond the non-authenticated functionality, but now I have this all over the place in a "dashboard" type of page. It seems to be getting kind of messy.
Is there a cleaner or more elegant way to implement this? I am using viewmodels. I am wondering if I should use different viewmodels/views based on role, but then using different viewmodels seems like it might be more difficult to maintain. I am not sure what the best practice is for this, and I am open to ideas.
Edit
One example of what I am doing: I have several lists/tables that allows managers to edit the record, so the code adds an extra
<td>
for the manager-allowed actions. Another use case: when the manager is signed in, an employee name is now an actionlink instead of just text.
What you could try is encapsulating each portion of the view that will be interchanged based on roles into partial views. This has worked well for me in the past, and is much cleaner when trying to troubleshoot code as opposed to seeing a bunch of #if statements in a single view.
Hmmm. I have this idea. You need list of dashboards enumerations where you have a property like RolesAllowedToAccess (string[])
On the view you can foreach by dashboards enumerations where RolesAllowedToAccess contains current user role.
After you can render partials for each of dashboards. Make sence?

Bad idea to manually edit a scaffolded view?

I scaffolded a new MVC controller, along with a corresponding view and repository for an IEnumerable collection of a POCO class. In the resulting view is a table that will list every entry in the collection of that class. However, the table also contains a column for every property in that class, and I only want to display a subset of them in this view. Is it a bad idea if I now manually edit the view to remove what I don't want to display in the generated HTML?
I'm not clear on whether the scaffolding features that are part of ASP.NET MVC (version 3 and later) are designed to generate the basic functionality for your web application, giving you the freedom to do what you want with the results afterwards, or if it's considered a best practice to leave the generated results alone and find another way to do what I want to accomplish. I know that you can customize your own scaffolding templates, but that seems like a lot of work for such a simple change.
Thanks!
It's definitely OK to edit the scaffolded view. This is just a feature that's there to quickly build the basics, but that will never suffice.
So you can just edit it all you want. Keep in mind though that if you edit your model, you will have to manipulate your view manually, but that's normal I think.
Scaffold will HELP you to generate a View strongly typed to a Model. Of course, if you want, you can update the html in your views. Another good tip: Instead of using your Model, create a ViewModel based on this Model, and put only the properties you need to display in the View.
View this content:
https://stackoverflow.com/a/11064362/1384539

Structuring a Rails CMS type application

I've just finished going through my first book learning RoR and wanted to practice some more extending the project, but I'm at a bit of a loss for how to structure the additions I want to make and would appreciate some guidance.
Right now the application is separated by pages that have a model controlling the logic and a controller routing that logic to the view, but none of the pages have any cross-over in features. I want to create a page that has pre-templated features with logic from other models, sort of like widgets in WordPress, or plugins in Magento. If I had a Page model and wanted to inject a Bestsellers list in the view, or I had a Blog model and wanted to inject a list of products with a tag calling out to a template with all the markup already, what is the proper way to do this?
Would these have to be modules? Would I just create another view template for Catalog that I would call into the Page index view?
Nevermind, I found the answer to what I was looking for here

Rails: aggregate multiple models into single view (think dashboard)

I have a Rails 3.1 "blog" type app and my Post#index has become a sort of overview (or dashboard or sorts) into the whole system. The main content is pulled from the Post model but a sidebar contains info from an Event model, there are other snippets from a News model, etc…
Post#index is a busy action that has to populate many instance variable before /view/posts/index.html.haml gets a call to render and then the many layouts and partials go to work.
Being as some of these other areas are self-contained, so to speak, I'm wondering if there's a better "Rails way" to approach this? Perhaps I should be looking at encapsulating the Event sidebar (which is effectively Event#index) into some kind of entity that can be re-used perhaps in other views? And, if so, how?
What are the options?
You might checkout
http://cells.rubyforge.org/
I would look into widgetizing it. A framework that I looked into in the past was: http://apotomo.de/ This provides a nice clean way to widget"ize" as well as callback functionality (Ajax) for specific widgets.

Resources