Add importer model, validations to paper model

This commit is contained in:
Andreas Haller 2015-05-17 17:49:09 +02:00
parent 568abb631f
commit 46de026234
8 changed files with 67 additions and 13 deletions

3
app/models/importer.rb Normal file
View file

@ -0,0 +1,3 @@
class Importer < ActiveRecord::Base
validates :url, presence: true, uniqueness: true
end

View file

@ -5,20 +5,28 @@ class Paper < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
validates_presence_of :body, :content, :name, :originator, :paper_type, :published_at, :reference, :url
validates :url, uniqueness: true
class << self
def import_from_json(json_string)
old_count = count
JSON.parse(json_string).each do |record|
attributes = {
body: record['body'],
content: record['content'],
name: record['name'],
url: record['url'],
reference: record['reference'],
paper_type: record['paper_type'],
resolution: record['resolution'],
originator: record['originator'],
paper_type: record['paper_type'],
published_at: record['published_at'],
reference: record['reference'],
url: record['url'],
}
record = find_or_initialize_by(url: attributes[:url])
record.update_attributes!(attributes)
record.update_attributes(attributes)
end
puts "Imported #{count - old_count} Papers!"
end
end
end