Rails PDF generation in model - ruby-on-rails

I am writing and invoicing application in Ruby On Rails, which is supposed to generate a PDF from a model so called out_invoice to an in_invoice under certain conditions.
The PDF for the out_invoice is already separately generated on demand using wicked_pdf gem basically having a HTML template and sending back the PDF inside the action and downloading it.
I have successfully programmed the duplication inside the out_invoice model which works fine for all values of the out_invoice. Including an attached PDF in a separate attribute.
But instead of generating the PDF and attaching it to the in_invoice. It fails. Some Controller/Model MVC thing I assume!
The PDF for the out_invoice is currently generated separately in a controller action so I tried copying the code: I tried rendering the template inside the model which seems to work fine, but "sending" is not defined for Rails models. If I just return the WickedPDF without sending it I get an error: ActiveSupport::MessageVerifier::InvalidSignature
Must be something super simple I am forgetting + I have no clue what is the reason for the error. I am not a front-end guy so don't want to use something else as the already existing HTML template for rendering. But worst case I write the template newly in another engine. Looked at Prawn but this means rewriting everything.
Can somebody help? Thanks!

Related

Nesting pdfs - Prawn

Some context: I have some html.erb files I use to create a pdf using WickedPdf that I can make their own pdf or I can render in another pdf, but I'm switching to Prawn because Prawn::Table is more predictable with page breaks than WickedPdf.
I've got a few POROs that inherit from a BasePdf class that handles all the boilerplate pdf stuff like our company's header, font size, and access to the #document etc. Is it possible to have a Prawn pdf PORO that can be a pdf on its own like the rest of my Prawn pdfs but also be included in another pdf in the middle of a page (as opposed to starting a new page).
Not sure about prawn, but here https://github.com/igorkasyanchuk/rails_pdf/blob/master/docs/report_4.png an example of PDF with a table.
Also on the second page of PDF, it will contain table header which is good.
We use extensively Prawn with plain Ruby or Rails to build complex business reports.
You can use all Ruby goodness to build reports like modules, class inheritance and custom methods. I thing this is one of Prawn's best part!
Each one of them can print/redner a part of a page or used as some kind of utility method. You actually right plain ruby that renders to pdf.
So you can have a base class with your company's lay out and custom modules which print some part of every specific report. Each module can take a hash with all required parameters for that module to render. This way you can also decouple reports from the rest of the app (that is db access etc.) and use or test them independently.

Can a Rails application template include generator parameters?

When I start a new Rails application, I often use certain parameters to rails new like --skip-spring or --skip-turbolinks.
I understand I can put these in ~/.railsrc, but when using multiple dev computers, it can be a bit tedious to keep railsrc files in sync across all of them.
Can I use application templates to add these parameters to the generator? That would be great, because application templates can be referenced by a url and downloaded on the fly.
(Also I know I can use templates to make changes after the application is created, so I can remove spring or turbolinks or whatever, but it would be much nicer to never generate the app that way.)
To answer my own question after looking into how this works, I think the short answer is that it's not possible to add generator parameters from an application template to rails new itself. The reason is that the template code only runs when the application is already generated.

Difference between Public and View of ruby on rails

What are the exact difference between the functionalities of app/View part and Public part of ruby on rails.
It seems that similar type of assets like js functions etc are defined in both.
what are the reasons behind defining the same things twice
In the public directory, Rails stores only static assets, i.e. files which are sent to the client as is without any further processing. These files can be generated using the assets pipeline (e.g. javascript assets or CSS files). However, they are only generated once (typically) during deployment.
The views however are the templates used by rails to generate the response to a dynamic request. Thus, if a user requests a certain (dynamic) action from your application, your controller decides which view to render. Its output is then sent to the browser. The view can thus be highly dynamic so that their output can change for each request.

Find url for webpage based on a partial file

I was introduced to a large team and not very familiar with the site I am developing for. I need to make an edit to a partial (i.e. _partial_file.html.haml) and need to verify the change visually but I don't know where that partial is being displayed on the website. Is there a way I can get a url from a partial file with rails.
For example, could I run a rake routes | grep or do something in the rails console to help find any URL where the partial is displayed?
Thanks
If I understand you correctly. While in your rails app root.
grep -r "partial_file" .
This will allow you to look for instances of partial_file and where it's being rendered. You might see it in a view or a controller. Depending on where you find it, you can rake routes and see just what url you should enter to see your output
When you render a page you should see output in the console for all templates being rendered.
example rendering template show.html.haml from SomeController#index
Use this to locate the template that is including your partial. You can then use that to search the entire project for all templates including that partial.
Rails renders templates by a specific convention that maps controller methods to specific layouts and templates. I would familiarize yourself with http://guides.rubyonrails.org/layouts_and_rendering.html to get a better idea of how the default internals work for you rails app and use that as a starting point.

Rails I18n doesn't work with asset pipeline templates in Rails 3.2.11

I need to localize an app, so I created a file for Spanish translations, but the config/locales/es.yml file doesn't seem to be properly loading, or maybe it loads after my templates are evaluated.
First a little context, I'm using the handlebars_assets and haml_assets gems. The latter allows to use the ActionView helpers within the templates in the asset pipeline and it also has access to the application classes, so one could write templates like:
.some_div
= SomeModel.model_name.human
And it works, except that it's returning the fallback name of the model. If I make these kinds of translations in a view though (instead of .hbs.haml template) I get the proper translated string. This is also true if I execute the translation methods in the rails console.
Following the Rails Guides I created the following initializer:
I18n.default_local = :es
Which is working properly if I consider that translating in views renders the expected output. My application is mostly living on the client side, so I really don't use Rails' views, it's a single page application managed with the help of Backbone. All the UI lives in templates that get rendered through the asset pipeline. So my question is, has anyone managed to make I18n work with this setup?

Resources