Whats the template option in cache_digests gem for? - ruby-on-rails

I am using the cache_digests gem. I am trying to understand whats the purpose of TEMPLATE option in rake cache_digests:nested_dependencies and cache_digests:dependencies rake tasks.
Could you also tell what the output means? Does the output shows the list of partials which needs will be rendered using cache, so that if there is a mismatch I can change the render partial method accordingly?

whats the purpose of TEMPLATE option
Both cache_digests:nested_dependencies and cache_digests:dependencies tasks are provided to help you resolve which partials are rendered inside of your template. To get the list of dependencies you should first decide which page's dependencies you are interested in. So that page (template) is passed to rake task as a TEMPLATE option.
Does the output shows the list of partials which needs will be
rendered using cache, so that if there is a mismatch I can change the
render partial method accordingly?
The output lists files which are observed by cache-digests for changes. In case the code which that files contain changes, the parent template's cache is going to be expired and new cache is generated.
Sometimes cache-digests can't derive partials' paths correctly, so it's also a reason to watch rake tasks output. Changing problematic render calls or using special comments for explicit dependencies is the way to solve these kind of problems, as described here.

Related

Kristin PDF to HTML result into a variable

By using Kristin Gem, Is there any possible way to store the result of the conversion on a variable instead of outputting it as a file?
Assuming that the link below goes to the gem you are talking about, no. The gem is a very thin layer on top of pdf2htmlEX and simply spawns the process with the arguments passed. Further, pdf2htmlEX doesn't seem to support redirecting its output and adding this feature doesn't seem to be on their todo list, so adding this functionality would require wrapping a different converter.
I think your best bet would be to save load the HTML to a variable after creation.
Kristin:https://github.com/ricn/kristin
Thread about adding output redirection to pdf2htmlEX: https://github.com/coolwanglu/pdf2htmlEX/issues/638

Storing the output of a compiled template in a variable rather than rendering it

By running a method I would like to fetch the output of a compiled erb-template and store it in a variable instead of rendering it.
I would like to be able to use rails' helper functions (e.g. distance_of_time_in_words_to_now or including partials) in the template.
How would I go about that?
Thank you!

Ruby operations on template variables - Chef

I have written following template in my chef cookbook recipe
template '/etc/app.conf' do
variables({
my_id: Chef::HTTP.new(https://example.com).get('/',{header})
})
end
And my erb file is
Output is : <%= #my_id %>
I actually want to perform some ruby operations(mainly filter out and count the components of my_id) and then pass those values(count of each component) back to the template and use it further. What should be erb configuration or anything thats need to be added in template block?
(Here, my_id actually has the subnets and I want to get those the count of those subnets and its values so that I can use it further to perform another http request and get the nodes in each of the subnet).
Don't know much about Chef Cookbooks but you can write some ruby within an ERB template. It is not the cleanest solution I believe though.
See here on how to embed code in your ERB
The thing you pasted from (hopefully you were summarizing since you missed a bunch of quotes in there) was only a quick tip. To get JSON data you want to use Chef::HTTP::SimpleJSON, the will do the parsing for you and whatnot.
variables data: Chef::HTTP::SimpleJSON.new('https://whatever.com/').get('/foo')

I can't find where a string is getting defined -- any tricks to find its source?

I'm using:
Rails 3.2x
Spree 1.2
Ruby 1.9.3x
I'm trying to edit the title of one of my pages, and I cannot find where it is getting defined. It is showing up in my base ERB file as 'title', but that name is sufficiently generic to make it next to impossible to find where it is defined.
I have prodded everywhere I can think, I've tried searching for "title =", but nothing is working. I tried calling source_location on it, but that appears to only work on methods.
Any tricks for finding where a variable is defined?
I can't think of an elegant way. A dumb-but-probably-effective way would be to dump stack trace in your erb, then see what those locations are doing and if title is defined there. It has to enter somewhere between the start of program and invoking your erb.
When I can't find something, I use grep -ri some_string . at the command-line to recursively search all the content of the directory.
It's also a good tactic to let your editor search all the source code, since the ones worth using have the ability to search through all files in a directory.
it is created from a mixture of product names, a site config, and something else
An alternate trick is to add a HTML-comment section in your ERB file, and put the pertinent information for the components used to create the title into that section. Then, let the pages be generated and look inside the page's content to determine what table and row ID it is, the site_config filename, etc.
You really should be able to figure it out based on the parts that are concatenated to build the title and then search your database or files. That information isn't magically created out of thin air by Rails; Someone had to tell Rails how to define the title. But, people move on, or they don't document correctly, so try the embedded information trick.

In Ruby, how can I inspect the class generated by a .html.erb template?

When doing J2EE development, I find it handy for debugging to view the Java classes that are generated by the JSP compiler.
How can I do the equivalent in Ruby? Since it is all in memory, it won't generate a file that I can view. I believe it's the ERB module that generates the corresponding object for a template, so how can I actually view the object? Can I drop a debugger statement somewhere and use rdb? Is there some configuration value I can tell it to dump the object definition? I'm using rails, in case that makes a difference.
I don't think rails generates a class for your view. It basically calls eval after processing the file. Or do you mean inspecting the erb object while it's parsing your template?
If it's the latter you can find erb.rb in lib\ruby\1.9.1 I'd imagine you could just drop a debugger statement throughout that file.
I always make a habit of adding the following to my views (layout) which allows me to inspect or debug the parameters being used by the view in question.
<%= debug(params) %>
This will format all the parameters in yaml and display them in a Hash format.
Have a look at the method in the source code to get a better understanding. SOURCE
There are some differences compared with the Java way due to language differences.
Most template libraries for Ruby follow these steps when compiling/optimizing:
The template is compiled into Ruby source code -- not a class but a long procedure that appends to a string buffer while traversing the logic of the original template.
This ruby code is evaluated in order to be bound for later reference, preferably within a method body. This way, it is only parsed once by the interpreter.
The method (or other context) containing the logic of the parsed template is invoked to render it.
Anyway, the compiled template code therefore looks a lot like a much noisier version of your original template, and will generally not help you debugging, unless you're debugging the template language itself.
Anyone interested in template language implementation might enjoy a look around the Tilt code (use different template languages with the same rendering interface and optimization), and Temple (a great template language meta-implementation).

Resources