ActiveAdmin download as only csv format - ruby-on-rails-3

I would like for ActiveAdmin gem only allow to download collection as csv file . Default it is set to download as xml, csv and json. I would like only allow to download as csv.

You can pass an array with the formats that you want to allow downloading. For instance, for only enabling csv you can do the following:
index download_links: [:csv] do
column :author
column :title
end

I found the link on the bottom of that post. I don't see a date on it, so it might not work anymore, or there might be a better way than monkey patching ActiveAdmin...
https://coderwall.com/p/qzlssg?i=1&p=1&q=author%3Adaviec85&t%5B%5D=daviec85

Related

How use the one Template for multiple pages in a XWPFDocument with Java

I would like to know, how can i reuse one template (with one page inside and some variables) multiple times a XWPFDocument object.
My idea is:
load the template once in a XWPFDocument as an template-object
clone/create/copy the template-object with all his styles and headers etc
fill the clone with content
add this clone to the destination-XWPFDocument
I got this work for one single page only.
When i try to clone/create/copy the template-object it will lose all his style informations.
How to copy a paragraph of .docx to another .docx withJava and retain the style
How to copy some content in one .docx to another .docx , using POI without losing format?
POI probably does not support this out of the box, but I have done a similar thing in my project poi-mail-merge, it works with the underlying XML to repeatedly replace markers in a template Microsoft Word document and combine the results into one resulting document.
So it basically duplicates the template document multiple times into the resulting document.
See here for how I do it there, basically I work on the XML body text and do replacements/changes there and then append it onto the result document.
POI Mail Merge propably helps in other cases but in my case it doesn't work.
My Workaround is to update my Template-XWPFDocument to the needed structure first, save it temporarily and read it back into a XWPFDocument-object.
Here the steps:
Read the template-file into a XWPFDocument
Read the records from data-file e.g. csv
Calculate the numbers of pages related to the data-records
Get the Bodyelements-Objects from the Template-XWPFDocument
Create new Bodyelements (depending to the numbers of pages) in the Template-XWPFDocument and replace them with the same Objects that we get before
Save the updated Template-XWPFDocument temporarily
Read the temporarily saved Template into a XWPFDocument
Replace all placeholder and fill them with your CSV-Data
Hope this helps somebody

I want to export searched data to excel file in rails app

I have implemented search option in my project. It searches records on basis of many fields given by user And then displays record according to it. Now I want those filtered records to be exported as excel. How can I do that?
You'll need to merge the search parameters. The parameter merging allows you to reuse whatever parameters were passed in the page and apply the xls format. Example below.
=link_to 'Export', users_path(request.parameters.merge({:format => :xls}))
(tested on Rails 5.0)
In my case, adding download: 'projects.xlsx' solved my problem!
<%= link_to 'Export',
projects_path(request.parameters.merge({format: :xlsx})),
download: 'projects.xlsx' %>
I have found a method where I can send params in download link.
So after the user submits the form and result is displayed url contains all the params of the form. So what I have done is that I have taken the current url(which contains all the search params) and append the format as xls to it. Example is given below -
This solution works fine for me

How to import CSV files in rails?

I am making an application in which it import a csv file names user.csv.But the problem i am facing is that it gives an error
ArgumentError in CsvimportController#import
wrong number of arguments (1 for 0)
And the code of the CsvimportController is
require 'csv'
class CsvimportController < ApplicationController
def import
results = import('anas.csv') do
read_attributes_from_file
end
end
end
And i have also give the specification of csv-mapper and fastercsv in gem file.
Can anyone help me???
Any help would be appreciated..
Thanks
Take a look at Railscast 396 on how to import data from CSV and Excel files.
The smarter_csv project aims to provide a better dealing with CSV files so it would worth to take a look.
It's easy if you use the Gem smarter_csv.
All you need to do is this:
require 'smarter_csv'
def import(filename)
results = SmarterCSV.process( filename, options_hash )
end
and you need to specify the options in the options_hash according to the documentation of smarter_csv
There are tons of useful options, including manipulation of the headers, custom-headers, ignoring columns, and type-conversion of values.
If your CSV file is large, you can also chunk the incoming data for parallel processing.

Paperclip not saving attachment

I am unable to get Paperclip to save my attachment. Rather than saving a single image (such as an avatar as seems to be the common usage), I need to be able to upload/save multiple files; therefore I have a User model and an Asset model. The file information gets properly stored in the asset table, but the attachment itself is not saved in the filesystem as expected.
My log shows the message:
"[paperclip] Saving attachments."
but the attachment is not saved.
Here's a gist with the details: https://gist.github.com/1717415
It's gotta be something simple that I'm missing...
OK... found the problem and it's now working.
The first issue was my naming of the columns in the asset model. I had used simple names: i.e., :description, :file_name, :file_size, :content_type. What I needed to use was: :upload_description, :upload_file_name, :upload_file_size, :upload_content_type where the 'upload' (or whatever you want to use) is a prefix that Paperclip will recognize.And of course that changed my Asset model to reference :upload not :asset, as in:
has_attached_file :upload
Secondly (and this post Adding :multipart => true throws Undefined Method "name" error was key to understanding this) was that you cannot specify the full column name (:upload_file_name) in your view, just specify the prefix and Paperclip magically understands what you want.
Hope this helps someone else!
Did you install ImageMagick?
Did you added image_magick command_path via initializer?
if not, checkout this answer:
Weird paperclip error message

Systematic way to upgrade from attachment_fu to carrierwave?

I'm working on upgrading an app to Rails 3, and attachment_fu is broken so I'm moving to carrierwave. Is there a systematic process that I can go through to upgrade from attachment_fu to carrierwave? Or a tutorial for it? Right now, I'm more interested in getting everything on the database end right. I'm using the filesystem store option for attachment_fu and carrierwave.
I've found a module, UploaderFu from http://ruby.simapse.com/2011/03/migrate-attachmentfu-to-carrierwave.html that tells carrierwave to use the same directories and filenames as attachment_fu. But it's not the entire answer, just part of it.
For example, in the db, I have a UserImage model, with :filename, :content_type, :size, :width, :height, and :user_id attributes. I added a :user_avatar column, and the following to my model
attr_accessible :user_avatar
mount_uploader :user_avatar, UserAvatarUploader
What exactly gets stored in :user_avatar. Is it just the filename? or something else? Do I just need to write a migration to move the data in :filename (stored like "hello_world.png") to :user_avatar? If that's the case I should just use the original :filename instead of creating a :user_avatar column, right?
The column you mount the uploader on is supposed to store an "identifier" for the uploaded file. By default it's just the filename, but you can override it to be almost anything apart from the ID of the record (because you can't know what that is until after saving).
To override: in your uploader class, add this definition :
def identifier
# This is what gets put in the database column!
model.created_on
end
In this example I've used the created_on attribute from the model. If you want to create your own storage mechanism then you need to be able to uniquely identify files by this identifier so be careful what you choose.
I would suggest renaming the column so it describes the file that's being uploaded (like in the carrierwave example). Then you can always change the identifier from filename to something else later.