So I'd like to set/get a session variable from inside a plugin model that I've included to my project. I tried making a method (one getter one setter) inside the application_controller.rb but this did not work. Where can I make getter/setter methods so that a plugin model has access to them?
Create a module in the lib folder in the directory.
Related
I'm creating a plugin for a website that uses a custom TagHelper, let's call it MainTagHelper.
I would like to override the MainTagHelper from my plugin, lets call it PluginTagHelper, which is in a different namespace. I can do this if I update the main website _ViewImports.cshtml file by adding a reference to my plugin via the #addTagHelper razor command. I would like to avoid that since my plugin won't have access to that file in a production setting.
I've tried using the MainTagHelper as a base class for my PluginTagHelper but it never gets called. I assuming this is because the main _ViewImports.cshtml is calling the MainTagHelper directly.
Any thoughts on how this can be accomplished?
Tag helpers cannot be overridden. All registered tag helpers are applied in the order in which they are registered (such as in _ViewImports.cshtml).
I've got a bunch of whitelist functions that do some complex validation on parameters. If you must know, I have several controllers that accept either:
A customer ID
e.g. customer=cus_123412341234
A customer Hash
e.g. customer[first_name]=Floating&customer[last_name]=Rock&...
Remember, there are several controllers that do this (e.g. a charge can be created at the charges endpoint using a customer ID or a Hash).
Now I want to know where I should place these methods (e.g. customer_hash_params(),customer_string_params(),..)?
Should I:
Place them in the lib/ folder as a Module
Place them in the helpers/ folder as a Helper
Something else..
Would like to know what the best practice is?
In short, a helper placed into app/helpers might provide some logic for both, controller and view. It is autoloaded for views and need inclusion within controller. A module or class placed within lib folder usually extend some controller or model logic. It is not autoloaded into environment, unless specified within application.rb
As you have some validation logic, that suits for lib directory, because it is not needed within views. I assume these validations are kind of pre-validations and your models do not have all these validated fields defined. Otherwise it would be a good idea to write some custom validators for your models.
Lib Folder in Rails are always used when we have long methods to support controller or model. So that we can call those methods from various controller.
If that is the requirement , Do go for lib Folder, Make a class. Call those class methods in different controllers and models.
I am trying to use Spring in my rails project but I have my own class called Spring that inherits from another class of mine called Feature.
In my code I call .superclass on a variable that is set to Spring sometimes. It fails because the variable is set to the other Spring class. How can I set it to class I defined?
i would suggest use modules.
create your class inside a module and call it MyModule::Spring to avoid conflicts
So this is a newbie rails design question. Lets say I want some of my common functionality to be sitting in a set of helper classes (either as class methods or instance methods).
And I want to use these helper objects inside controller (not view) or even may be a model. can I do that? how? Does it have to be a module or class or can be anything?
Is there rails specific pattern for this?
If their are not tied to one of the three tiers, you should place them in the /lib directory.
The convention under /lib is that you should name your folders as modules, and files and classes, and that you should always try to encapsulate your additional behavior in modules. Let's say, you have some class
module MyModule
class MyHelperClass
end
end
You should put it into /lib/my_module/my_helper_class.rb
I know that if I want to call a helper of another controller, I can do something like:
helper :other_controllers
But I was wondering why I can't do something like OtherControllersHelper.method inside the view?
Due to the way that Rails loads your modules, you cannot do this without modification.
Rails includes the associated helper models into the ActionView::Base instance used to render a template. ActionController::Helpers#helper (used in the example above) adds more helper modules to the list of those to be included. The helper methods that are used in views are written as instance methods. Modules in Ruby do not provide any good ways of getting at instance methods without using a constructor. Which is one of the big things that separates modules from classes.
To access your helpers from another controller with just OtherControllersHelper.method, you will need to redefine method as a class method. However, redefining those methods as class methods would make them inaccessible from your views.
You could duplicate all instance methods in your helpers as class methods, but that's definitely not a better solution that adding helper :other_controllers. There are ways to define wrappers pragmatically, but again, it's not the best way to handle the situation.
If you've got a lot of helpers that are likely to be used in multiple controllers/views maybe you're better off putting them somewhere else. Somewhere like app/helpers/application_helper.rb. Or another helper module that could be loaded only in the controllers that need it.