Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Create 'record types'
Browse files Browse the repository at this point in the history
  • Loading branch information
frankieroberto committed Apr 25, 2016
1 parent 9e6786a commit c840ff0
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Procfile
@@ -1,2 +1,2 @@
web: bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq
worker: bundle exec sidekiq -c $SIDEKIQ_CONCURRENCY
13 changes: 13 additions & 0 deletions app/controllers/types_controller.rb
@@ -0,0 +1,13 @@
class TypesController < ApplicationController

def index
@types = Type.order('records_count desc')
end

def show
@type = Type.find(params[:id].gsub('T',''))

@records = @type.records.select(:identifier, :title).limit(100)
end

end
29 changes: 29 additions & 0 deletions app/jobs/update_types_from_metadata_job.rb
@@ -0,0 +1,29 @@
class UpdateTypesFromMetadataJob < ActiveJob::Base
queue_as :default

def perform(record)

genres = record.metadata.fetch('655', [])

genres.each do |genre|

genre_name = genre['a'].gsub(/\.\z/, '')

genre_reference = genre_name.downcase

if genre_name

type = Type.find_by_reference(genre_reference) || Type.create!(references: [genre_reference], name: genre_name)

begin
record.types << type
rescue ActiveRecord::RecordNotUnique
# Already added - ignore.
end

end

end

end
end
3 changes: 3 additions & 0 deletions app/models/record.rb
Expand Up @@ -6,6 +6,9 @@ class Record < ActiveRecord::Base
has_many :taggings
has_many :subjects, through: :taggings

has_many :record_types, dependent: :destroy
has_many :types, through: :record_types

scope :digitized, -> { where(digitized: true) }

before_save :set_cover_image_uris, :set_pdf_thumbnail_url
Expand Down
6 changes: 6 additions & 0 deletions app/models/record_type.rb
@@ -0,0 +1,6 @@
class RecordType < ActiveRecord::Base

belongs_to :type, counter_cache: :records_count
belongs_to :record

end
15 changes: 15 additions & 0 deletions app/models/type.rb
@@ -0,0 +1,15 @@
class Type < ActiveRecord::Base

has_many :record_types, dependent: :destroy
has_many :records, through: :record_types


def to_param
"T#{id}"
end

def self.find_by_reference(reference)
find_by(["? = ANY(types.references)", reference])
end

end
15 changes: 15 additions & 0 deletions app/views/types/index.html.erb
@@ -0,0 +1,15 @@
<h1>Types of things</h1>

<ul>
<% @types.each do |type| %>
<li><%= link_to type.name, type %>

<small>
<%= number_with_delimiter(type.digitized_records_count) %>
of
<%= number_with_delimiter(type.records_count) %>
things digitised
</small>
</li>
<% end %>
</ul>
11 changes: 11 additions & 0 deletions app/views/types/show.html.erb
@@ -0,0 +1,11 @@
<h1><%= @type.name %></h1>

<p><%= pluralize_with_delimiter(@type.records_count, 'record') %></p>

<ul>
<% @records.each do |record| %>

<li><%= link_to record.title, thing_path(record) %></li>

<% end %>
</ul>
2 changes: 2 additions & 0 deletions config/routes.rb
Expand Up @@ -36,6 +36,8 @@ def matches?(request)
end
end

resources :types, only: ['index', 'show']

resource :search, only: 'show', controller: 'search'

resources :subjects, only: ['show'], controller: 'subjects_lookup'
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20160425103426_create_types.rb
@@ -0,0 +1,13 @@
class CreateTypes < ActiveRecord::Migration
def change
create_table :types do |t|
t.text :name, null: false
t.text :description
t.text :references, array: true, null: false, default: []
t.integer :records_count, null: false, default: 0
t.integer :digitized_records_count, null: false, default: 0
end

add_index :types, :references
end
end
10 changes: 10 additions & 0 deletions db/migrate/20160425104440_create_record_types.rb
@@ -0,0 +1,10 @@
class CreateRecordTypes < ActiveRecord::Migration
def change
create_table :record_types do |t|
t.integer :record_id, null: false
t.integer :type_id, null: false
end

add_index :record_types, [:record_id, :type_id], unique: true
end
end
19 changes: 18 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160114172444) do
ActiveRecord::Schema.define(version: 20160425104440) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -63,6 +63,13 @@

add_index "people", ["highlighted"], name: "index_people_on_highlighted", where: "(highlighted IS TRUE)", using: :btree

create_table "record_types", force: :cascade do |t|
t.integer "record_id", null: false
t.integer "type_id", null: false
end

add_index "record_types", ["record_id", "type_id"], name: "index_record_types_on_record_id_and_type_id", unique: true, using: :btree

create_table "records", force: :cascade do |t|
t.text "title", null: false
t.text "identifier", null: false
Expand Down Expand Up @@ -113,6 +120,16 @@
add_index "taggings", ["record_id", "subject_id"], name: "index_taggings_on_record_id_and_subject_id", unique: true, using: :btree
add_index "taggings", ["subject_id"], name: "index_taggings_on_subject_id", using: :btree

create_table "types", force: :cascade do |t|
t.text "name", null: false
t.text "description"
t.text "references", default: [], null: false, array: true
t.integer "records_count", default: 0, null: false
t.integer "digitized_records_count", default: 0, null: false
end

add_index "types", ["references"], name: "index_types_on_references", using: :btree

create_table "users", force: :cascade do |t|
t.text "email", null: false
t.text "password_digest", null: false
Expand Down
23 changes: 23 additions & 0 deletions lib/tasks/records.rake
Expand Up @@ -84,4 +84,27 @@ namespace :records do

end


desc 'Update Record Types from records'
task queue_all_to_update_record_types: :environment do

time = Time.now

Record.select(:id)
.find_in_batches(batch_size: 500)
.with_index do |batch, batch_number|

print "Processing batch #{batch_number + 1}... "

batch.each do |record|
UpdateTypesFromMetadataJob.perform_later(record)
end

puts "Done in #{Time.now - time} seconds"
time = Time.now

end

end

end

0 comments on commit c840ff0

Please sign in to comment.