How to access params hash in lib directory rails 6 - ruby-on-rails

In my rails app, I am using Kramdown to parse Markdown. I want to extend the functionality of the convert_a method in the HTML converter. Part of this involves accessing the database, but it is dependent on a parameter in the URL. Because I am not directly calling the method that I am overriding I cannot simply pass the method the params hash. Is there a way to access this hash, or even just get the current URL in a module in the lib directory?
to give a bit more context, the method call is in a helper method here:
# in app/helpers/myhelper.rb
def to_html(text)
Kramdown::Document.new(text, parse_block_html: true).to_custom_html
end
and here is the file in which I override the convert_a:
# in lib/custom_html.rb
class CustomHtml < Kramdown::Converter::Html
def convert_a(el, indent)
# use params[:foo] to make query
format_as_span_html(el.type, el.attr, inner(el, indent))
end
end
Edit:
To give a bit more context on where the overrided method is called. I am not extremely familiar with the Kramdown codebase, however it seems that when to_custom_html is called the following bit of code is run inside of Kramdown.rb:
output, warnings = Converter.const_get(name).convert(#root, #options)
which subsequently calls convert_#{el.type} on the internal kramdown elements.

You can pass additional options in Kramdown::Document#new, so just do something like Kramdown::Document.new(text, my_params: params). Then you can use the #options method of the converter to access your params.

Related

Rails: What is `sanitize` in Rails?

What does sanitize mean in Rails?
I'm reading through the documentation for CanCanCan. It says:
When using strong_parameters or Rails 4+, you have to sanitize inputs
before saving the record, in actions such as :create and :update.
Then per documentation, it requires adding the below:
load_and_authorize_resource param_method: :my_sanitizer
def my_sanitizer
params.require(:article).permit(:name)
end
Source: https://github.com/CanCanCommunity/cancancan
I've also seen sanitize in the area of SQL queries.
What does sanitize mean actually. Does it just mean to allow something?
The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements. These helper methods extend Action View making them callable within your template files.
data = data.html_safe will just mark string data as 'html_safe' and treat it as such afterwards (Marks a string as trusted safe. It will be inserted into HTML with no additional escaping performed. It is your responsibility to ensure that the string contains no malicious content. This method is equivalent to the raw helper in views. It is recommended that you use sanitize instead of this method. It should never be called on user input.).
Have a look at official api doc
action view sanitize helper

Rails 4 - controller strong params - generated with scaffold

I am trying to make an app in Rails 4.
I use scaffolding generators to make my resources starting points.
I'm noticing, when I ask questions on this board, that people comment on the form of my strong params definitions in the controllers.
The scaffold generator creates them this format:
def industry_params
params[:industry].permit(:sector, :icon)
end
Most resources that show how to whitelist strong params, show this format.
def industry_params
params.require(:industry).permit(:sector, :icon)
end
http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html
Is there anything wrong with the way the rails scaffold generator creates this method?
The require method ensures that a specific parameter is present, and if it's not provided, the require method throws an error. It returns an instance of ActionController::Parameters for the key passed into require.
The permit method returns a copy of the parameters object, returning only the permitted keys and values.
As you can see while using scafffold's default system we need to check for is there is any value in the params, whereas require throw an error if its missing.

How do I get environment variables in Rails from inside a gem?

I'm making a Ruby on Rails gem and need access to variables like cookie or the query parameters. How do I get this from inside a gem?
module MyModule
def self.my_method
# need access to cookies, query params, etc
end
end
So that from a controller or view, I can call
MyModule.my_method
and it'll have the access it needs.
You need to pass in what you want. Whether you want to pass in each individually or pass in the request object and pull out what you want depends on how/where you'll use your gem. I would lean towards passing in each individually in a well defined format so there aren't any surprises b/n rails servers and what the request object looks like.

What is the best way to give a model access to params hash in Rails?

This appears to be a violation of MVC, so I'll explain what I am trying to do:
My model makes a call to an API, and the URI of the API depends on the params hash. (The params hash stores the URL of the Rails app).
I created a module to mix into the model (because the model has nothing to do with the API call), but haven't figured out how to get the params into the module.
If possible, I would like to initialize the module with the params hash, but don't know where to do it. before_create on the model would work, but that is in the model.rb file which doesn't know about the params.
Couple ideas, depending on your needs:
Pass the parameters to the model with each call, if they change that much. You could do a class method or instance method -
Model.api_call params[:field]
#model.api_call params[:field]
Save the parameters as class variables in the model:
Model.set_parameters(params)
def self.set_parameters(params)
##params = params
end
# access it in methods with ##params

url_for in controller

in a rails 2 controller i get some data from a model
#company = Company.first
and generate the url in the view
<%= url_for #company %>
Of course this works fine. But when i try to use this in a script
include ActionController::UrlWriter
default_url_options[:host] = 'www.example.com'
#company = Company.first
puts url_for(#company)
it fails with
/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:127:in `merge': can't convert Company into Hash (TypeError)
Any ideas?
I think the issue might be that the url_for method that you're used to calling in your views (and defined on ActionView as a helper) is not the same url_for method that gets called when you're in a controller.
ActionController::Base has its own, similar (but not the same) method called url_for method. In the scope of your controller, the method defined on ActionController::Base is the one being called. http://apidock.com/rails/ActionController
The link to ActionController docs above technically points to the Rails3 version of the API, but it hasn't really changed. If you absolutely need or want the Rails 2.3 docs, you can download them here.
Those are not the same methods.
In your view, you're calling ActionView::Helpers::UrlHelper#url_for. That method has several checks in it to decide what to do based on the type of data that you passed in. If you pass in a model object you end up in the method ActionController::PolymorphicRoutes#polymorphic_path which figures out which named route it's supposed to be using.
The url_for that you're calling in your script doesn't know how to do any of that. However, it can still do quite a bit and I would suggest that you read the comments in that file for ideas on how to use it. The error message that you got will point you right to it.
/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb
(Note: actionpack 2.3.14 is available. You might want to upgrade while you're at it.)

Resources