mirror of
https://github.com/CodeforLeipzig/stadtratmonitor.git
synced 2025-04-20 07:11:33 +02:00
Add importer model, validations to paper model
This commit is contained in:
parent
568abb631f
commit
46de026234
8 changed files with 67 additions and 13 deletions
3
app/models/importer.rb
Normal file
3
app/models/importer.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Importer < ActiveRecord::Base
|
||||
validates :url, presence: true, uniqueness: true
|
||||
end
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue