Controllers and Helpers - sorbet

I have a Padrino application with a controller and a corresponding helper (similar to Rails). Sorbet is reporting a method defined in the helper as being missing in the controller which is calling it. Is there anything to do about this in the current implementation of Sorbet?

You can define in an rbi file that the controller includes the helper. That will trick Sorbet to include the methods from the helper
# — foo_controller.rbi
class FooController
include BarHelper
end

Related

Pluralizing rails 3 controller manually

I have a Rails 3 controller which is not pluralized (IphoneUser) - it already has some controller methods, and a model generated.
However I'd like now rather than when it gets too late into the game, to pluralize it.
What's the best way to pluralize this controller without a nightmare of 1-by-1 guess and checks?
You should just need to rename the controller, it's class name, it's views folder, its helper and its functional tests. The only other option is to use the rails generator to destroy it rails destroy and then recreate it named properly. I'd just copy the controller methods and paste them into the new file. rails destroy won't affect your model.

helper for all views in a namespace

Is there a way to create a view helper file that will be available to all views in a namespace? Like application_helper.rb, but only working for a given namespace.
Specifically, I have a namespace called "office". I want to set up a view helper that is accessible to any view within the "office" namespace.
Thanks.
I would suggest that you have a BaseController for that specific namespace. For example,
class Office::BaseController < ApplicationController
helper :office
end
And inherit this controller in all the other controllers within that namespace.
class Office::UsersController < Office::BaseController
def index
..
end
end
Now all the methods within the helper office_helper.rb is present within this namespace.
Also, this is a good practice to separate the concerns / code for the controller namespaces.

How can I escape HTML in a Rails 3.1 controller method?

h / html_escape don’t work within a controller method.
I know, it’s for views. (Or was — haven’t done much Rails since v2.3). But I’m just building an eensy-weensy string and want to use this method in my controller.
So how can I escape HTML from within a controller method?
ERB::Util.html_escape
You can include ERB::Util in your controller to use these methods directly.

Load a module and then controller by default in Kohana 2.3.4

Working in Kohana 2.3.4, I need to load a module when I go to example.com.
In the routes.php file you can specify a default controller like:
$config['_default'] = 'welcome';
but that refers to the controllers within the main application.
Is there a way to load a module by default, and then specify the default controller to load within that module?
In 2.3.4 you need to specify which modules you are loading in your application/config/config.php. Once they're loaded you can use them in your routing just as you would your standard controllers.
Assuming within your module you had a controller named foo and a method named bar your default route would simply be:
$config['_default'] = 'foo/bar';
Example config from http://docs.kohanaphp.com/general/modules
// Paths are relative to the docroot, but absolute paths are also possible
// Use the MODPATH constant (?)
$config['modules'] = array
(
MODPATH.'acl',
MODPATH.'auth',
)
It's worth noting that the Kohana filesystem is cascading so duplicate controllers (and other files) in your application folder would override module controllers which in turn override the system controllers.
For more see: http://docs.kohanaphp.com/general/filesystem#cascading

Rails/Rspec Views: The right way to test partials

I have a Rails 3 partial that lists all categories as a navigation menu - it's on most, but not all of my template pages...let's say about 75%. I'm trying to test the partial (in RSpec) right now, and I've just realised a few things:
At the moment, I'm calling Categories.all in the actual view. The difficulty is that, because that touches the database, my mocks/stubs in the view spec are ignored, and consequently the test fails.
I'm guessing the alternative is to assign the variable in the application controller, and then pass it as a local variable to the partial. Still, about 25% of my pages won't use the variable and I'm wondering if there's a more graceful way of doing things.
In short, I want view specs to pass without touching the test DB, but I'm not sure a global variable passed to my partial is the best way to do it...and I'm not declaring the variable in every (& only) those controllers who require it.
Any suggestions appreciated...
Why not create a helper method for all categories?
# in categories helper
def all_categories
#all_categories ||= Category.all
end
Or...
# application controller
helper_method :all_categories
def all_categories
...
You can then stub out this method in your specs and you won't be touching the DB