
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
= RailsCsvImporter
Define configuration in a hash and then import Ruby on Rails model data from CSV with one method call.
Both Rails 2 and 3 are supported.
== Usage
=== In a Rails Model
class Foo < ActiveRecord::Base acts_as_rails_csv_importer end
=== Generate CSV Template
Call Foo.get_csv_import_template(import_config). See below for details on the parameter.
=== Import From CSV
Call Foo.import_from_csv(import_config, content, options = {})
import_config
A hash that defines what data are imported and how.
[:mapping] A hash that defines the columns in the csv. Each hash key is the model attribute name for the column. Each value is a hash defining how the column is processed:
[:name] The heading that identify the column in the csv.
If omitted, the heading will be the humanized form of the key.
(_Optional_)
There are four different ways to specify how the column is processed:
[:value_method] A lambda to convert the column value from csv into the attribute value in the model.
Class Acts::RailsCsvImporter::ValueMethods provides some pre-defined value methods.
(_Optional_)
[:record_method] A lambda to find the associated record if this attribue is a foreign key.
(_Optional_)
[:virtual] If set to true, the column is not mapped to a model attribute.
Usually the column will be used together with other columns.
[None of the above options present] The column value will be used without any conversion.
[:find_existing] A lambda to find the existing record to update for a row. A new record is created if the lambda could not find an existing record and return nil. The row is passed as a hash to the lambda. If this parameter is omitted, a new record is created for each row. (Optional)
content
A string that contains the csv content to import from.
options
A hash of options for the import. (Optional)
[:partial_save] If true, any valid rows in the csv will be saved even if there are some invalid rows. Otherwise, data will be imported only when all rows are valid.
Exception +Acts::RailsCsvImporter::RailsCsvImportError+ is thrown when there are errors duing the importing.
== Example
class Material < ActiveRecord::Base acts_as_rails_csv_importer end
class MaterialsController < ApplicationController IMPORT_CONFIG = { :mapping => { 'name' => {}, 'fragile' => { :name => "Fragile?", :value_method => Acts::RailsCsvImporter::ValueMethods.boolean_value_method }, 'category_id' => {:record_method => lambda { |v, row, mapping| Category.find_by_name(v) } }, }, :find_existing => lambda { |row| Material.find_by_name(row['name']) } }
def download_template headers["Content-Type"] = 'text/csv' headers["Content-Disposition"] = 'attachment; filename="template.csv"' render :text => Material.get_import_template(IMPORT_CONFIG) end
def import num_imported = Material.import_from_csv(IMPORT_CONFIG, params[:file_upload]) flash[:notice] = num_imported.to_s + " records imported successfully." rescue Acts::RailsCsvImporter::RailsCsvImportError => ex @err_message = ex.errors.map { |err, row| err.is_a?(String) ? err : err.full_messages }.join(';') render :template => :error end end
For more examples, refer to +test/rails_csv_importer_test.rb+
== Download and installation
The latest version of RailsCsvImporter can be installed with RubyGems:
% [sudo] gem install rails_csv_importer
Source code can be downloaded on GitHub
== License
RailsCsvImporter is released under the MIT license:
FAQs
Unknown package
We found that rails_csv_importer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.