When to use a custom view helper class in Rails - ruby-on-rails-3

I tend to do a lot with partials. They almost feel like small helper classes that take some parameters and spit out view snippets.
My question is, when is it appropriate to make a separate class for a view? Would this be appropriate for something with some additional logic like a menu system that needs to be available for testing?
I have a feeling I may be overusing partials.

you can use a helper to build a single unit of display. and you can use partial to build complex unit of display.
see Helpers vs. Partials and What belongs in a helper method?

Related

What is Helper Class in dart?

I started learning the dart language. when I heard the term Helper Class. I didn't get any clear answer.
Please tell me about
What is the meaning of the Helper Class?
For which purpose they are used
Thank you so much to the kind community. :)
In simple words Helper class is like a warehouse where you can put commonly used operations for other classes. So whenever other classes will need them they can access them.
Imagine you have some code which is commonly used in your app. So there are two ways to use that code.
To write the same code again and again. (Which no one wants and prefers)
To put that code somewhere and call it whenever you need it. And this is a situation where helper class come in to play.
Below are some main objectives behind its creation:
A helper class is created to make code more readable and clearly organizable.
They help to eliminate boilerplate code as they contain commonly used functionalities.
The other goal behind its creation is to provide a common functionality to other classes. In helper class, you can move some methods, variables, and operations, which are commonly used in other classes. So it helps to make your code more organized, maintainable and readable to others.
I hope it helps you. :)

Dynamically defining methods called from Objective-C with RubyMotion

To separate action methods out of the app delegate, I moved them into modules in the relevant controllers and included the modules in AppDelegate.
However at first, I tried to define the actions for each controller in a hash and use define_method to dynamically create the methods. That gave me the error method '<action>:' created by attr_reader/writer or define_method cannot be called from Objective-C. Please manually define the method instead (using the 'def' keyword).
Compared to define_method, including modules not very efficient with RubyMotion. For this situation performance is not as important, but is there another way to create methods on-the-fly that can be called from Objective-C?
From what I can understand from your explanation of your problem, you're handling actions in your app delegate. It's important to handle this kind of stuff from your controllers or to put the actions in your views, then setup a delegate for your view.
When it comes to RM though, you will have to work around not being able to do things like this.
I can't think of a time where this would be the only way to do what you're trying. Using Napalm's suggestion is one way.
If you were creating lots of events for dynamic content or server provided content, you can just use a method that uses those values instead of defining a method's definition off them (as define_method is usually providing a very similar method for all the dynamic methods it defines). Just try using normal `
There will be lots of alternative solutions available to you.

ViewModels or ViewBag?

I'm fairly new to MVC4, EF5 and ASP.Net, and I don't seem to be able to find a good answer anywhere.
Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag?
Say I have a method which populates a drop down list, and I am using a viewmodel to represent the output for the view.
Am I ok to use Viewbag.DropDown = PopulateDropdown(); or would it be better to incorporate
this into the ViewModel, by creating a property to hold the List<SelectListItem> created by PopulateDropdown(); ?
I know how handy ViewBag is, but I'm yet to see any solid reason as to not use it? If anyone could also offer me some more insight, that would be fantastic.
Basically, Should everything be done through the viewmodel or is it Ok
to also incorporate viewbag?
Everything should be done inside a view model. That's what a view model is. A class that you specifically define to meet the requirements of your view. Don't mix ViewBags with ViewModels. It is no longer clear for the view where is the information coming from. Either use only a view model (approach that I recommend) or only use ViewBags. But don't mix the 2.
So in your particular example you would have a property on your view model which is of type IENumerable<SelectListItem> and inside your view you will use the strongly typed version of the Html.DropDownListFor helper to bind to the model:
#Html.DropDownListFor(x => x.ProductId, Model.Products)
Obviously those are only my 2 cents. Other people will say that mixing ViewModels and ViewBags is fine and I respect their opinion.
Prefer ViewModels over the ViewBag wherever you can. Create Strongly typed views. It makes your code cleaner, less fragile, less error-prone, and easy to maintain.
ViewBags are just dictionaries of dynamically typed objects so you lose:
Compile time checking
The ability to refactor with confidence (you lose the support of the tools)
IDE support - such as the ability to navigate to all usages
Intellisense
For bonus points making extensive use of the ViewBag also misses the point of using the MVC pattern
I get the impression ViewBags were created to solve an edge-case problem in asp.net and people use them instead of creating view models as was originally intended in the design of the platform, to the detriment of their work.
with thanks to Why not to use ViewBag heavily?

project structure and share var under all module controllers

I have the following structure:
modules/group
modules/group/modules/forum
modules/group/modules/gallery
modules/group/modules/events
Is this a correct way of structuring groups? or is better
modules/group
modules/group/controllers/ForumController.php
modules/group/controllers/GalleryController.php
modules/group/controllers/EventsController.php
And another question. I need group object in all actions controllers under group module and i dont want to write the following code in all controllers
$group = Group::model()->findByPk($_GET['idgroup']);
The url's like:
/group/<idgroup>/forum/<idforum>
/group/<idgroup>/gallery/<idgallery>
What is the right way to do this?
Is this a correct way of structuring
groups?
The answer depends on a couple things:
Are you pulling in forum, gallery and events modules from a 3rd party?
Do forum, gallery and events contain their own models, views and controllers?
Do you plan on making forum, gallery and events stand-alone modules that others can use in their Yii applications?
If any of the above, then yes, it would make sense to put them into their own modules. Otherwise, adding them to the group module would be preferred.
According to Yii documentation:
A module is a self-contained software
unit that consists of models, views,
controllers and other supporting
components.
In regards to the adding the group object to all controller actions, generally I'll add a base Controller class for my module (something like /group/components/GroupController.php) and extend that controller rather than the base one in all of my controllers. GroupController should extend Yii's CController and contain the group object that you want to have accessible from your module's controllers.

When is it Appropriate to use Custom Classes in a Rails Application?

This is a complicated question with many possible answers, so I'll break down my situation into simple bullet points to help narrow down the solution:
My Rails App Has the Following 'Objects'
Author
Feed
Update
FeedTypes
The Objects are Related Like So:
Authors can have 1 or more Feeds
Feeds can have one or more Updates
A Feed has one feedType
Example Setup:
Author: Levi Hackwith
Feed: view-source:http://www.twitter.com/statuses/user_timeline/opnsrce.xml
FeedType: Twitter
Update: The tweets inside the Feed
My problem and My Questions:
Problem:
I need to parse the above-mentioned feed and store each tweet in the updates table. To parse the feed, I was thinking of writing a custom Feed class which would get inherited by TwitterFeed, FacebookFeed, TumblrFeed, etc.
However, I'm not sure if this is the 'Best Practice' for solving this kind of problem.
Questions:
When is it appropriate to develop a custom class to perform an action in RoR (as opposed to going through the Model or Controller)?
If this situation does not call for a custom class, which element should I apply the parsing logic to? The model or the controller?
If this is an appropriate situation for a custom class, where in my rails application should I store it (in other words, what's the right 'convention')?
You are probably going to have a background task invoked from time-to-time to check all the feeds, fetch new updates and store those in database. This task is completely separate from controllers and it should be possible to invoke it without any controller logic.
Your abstraction looks fine. You can further have something like XmlFeed < Feed if several feeds share a common XML structure.
1) Controllers should talk to database/models and pass relevant data to the view to render. Everything else should be either in a model, helper or library.
2) Are you asking where the parsing logic belongs to? In MVC, I think this would belong under the Model and/or a helper class, but definitely not the controller.. it's not its responsibility.
3) Classes holding data go into app/models. Classes that have nothing to do with holding data, go into the lib directory.
Don't shy away from using a custom class if it's appropriate. If you need another a class, then add one, the fact you are using rails is not relevant to that decision.