From d39f00165e4215029e239c9c4a214485b2adba15 Mon Sep 17 00:00:00 2001 From: Andreas Haller Date: Mon, 13 Apr 2015 22:09:28 +0200 Subject: [PATCH] Add Paper model, Paper.import_from_csv --- app/models/paper.rb | 19 +++++++++++++++++++ db/migrate/20150413193656_create_papers.rb | 17 +++++++++++++++++ db/schema.rb | 14 +++++++++++++- lib/tasks/import_papers.rake | 7 +++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 app/models/paper.rb create mode 100644 db/migrate/20150413193656_create_papers.rb create mode 100644 lib/tasks/import_papers.rake diff --git a/app/models/paper.rb b/app/models/paper.rb new file mode 100644 index 0000000..2ec25f2 --- /dev/null +++ b/app/models/paper.rb @@ -0,0 +1,19 @@ +require 'csv' + +class Paper < ActiveRecord::Base + class << self + def import_from_csv(csv_string) + CSV.parse(csv_string, headers: true) do |row| + attributes = { + name: row['name'], + url: row['url'], + reference: row['reference'], + paper_type: row['paper_type'], + originator: row['originator'], + published_at: row['published_at'], + } + create!(attributes) + end + end + end +end diff --git a/db/migrate/20150413193656_create_papers.rb b/db/migrate/20150413193656_create_papers.rb new file mode 100644 index 0000000..5dad952 --- /dev/null +++ b/db/migrate/20150413193656_create_papers.rb @@ -0,0 +1,17 @@ +class CreatePapers < ActiveRecord::Migration def change + create_table :papers do |t| + t.string :name + t.string :url + t.string :reference + t.string :name + t.datetime :published_at + t.datetime :scraped_at + t.string :paper_type + t.string :originator + t.text :resolution + t.text :content + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f5fa696..a25cb86 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141117201926) do +ActiveRecord::Schema.define(version: 20150413193656) do + + create_table "papers", force: true do |t| + t.string "name" + t.string "url" + t.string "reference" + t.datetime "published_at" + t.datetime "scraped_at" + t.string "paper_type" + t.string "originator" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "users", force: true do |t| t.string "email" diff --git a/lib/tasks/import_papers.rake b/lib/tasks/import_papers.rake new file mode 100644 index 0000000..87bd129 --- /dev/null +++ b/lib/tasks/import_papers.rake @@ -0,0 +1,7 @@ +namespace :import_papers do + + desc 'Import Paper records from CSV' + task :from_csv, [:csv_file] => :environment do |t, args| + Paper.import_from_csv(File.read(args[:csv_file])) + end +end