Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added exhibitions
  • Loading branch information
Barry Hoggard committed Jul 26, 2015
1 parent 89bc7c5 commit 7354575
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .rubocop.yml
Expand Up @@ -8,8 +8,9 @@ StringLiterals:
# Max: 120

Metrics/MethodLength:
Max: 12
Max: 10
Exclude:
- lib/tasks/legacy.rake
- test/**/*

AllCops:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -12,6 +12,7 @@ gem 'bootstrap-sass', '~> 3.3.5'
gem 'haml-rails'
gem 'simple_form'
gem 'activerecord-import', require: false
gem 'ranked-model'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Expand Up @@ -157,6 +157,8 @@ GEM
thor (>= 0.18.1, < 2.0)
rainbow (2.0.0)
rake (10.4.2)
ranked-model (0.4.0)
activerecord (>= 3.1.12)
rubocop (0.32.1)
astrolabe (~> 1.3)
parser (>= 2.2.2.5, < 3.0)
Expand Down Expand Up @@ -225,6 +227,7 @@ DEPENDENCIES
newrelic_rpm
pg
rails (= 4.2.3)
ranked-model
rubocop
sass-rails (~> 5.0)
simple_form
Expand Down
1 change: 1 addition & 0 deletions app/models/artist.rb
@@ -1,4 +1,5 @@
class Artist < ActiveRecord::Base
belongs_to :nationality
has_many :works, dependent: :destroy
validates :name, :sort_name, presence: true
end
6 changes: 6 additions & 0 deletions app/models/exhibition.rb
@@ -0,0 +1,6 @@
class Exhibition < ActiveRecord::Base
validates :title, presence: true
has_many :exhibition_works
has_many :works, -> { order "exhibition_works.position" },
through: :exhibition_works, dependent: :destroy
end
8 changes: 8 additions & 0 deletions app/models/exhibition_work.rb
@@ -0,0 +1,8 @@
class ExhibitionWork < ActiveRecord::Base
include RankedModel
belongs_to :exhibition
belongs_to :work
ranks :position, with_same: :exhibition_id

validates :exhibition_id, :work_id, presence: true
end
1 change: 1 addition & 0 deletions app/models/nationality.rb
@@ -1,3 +1,4 @@
class Nationality < ActiveRecord::Base
has_many :artists
validates :name, presence: true, uniqueness: true
end
2 changes: 2 additions & 0 deletions app/models/work.rb
Expand Up @@ -2,5 +2,7 @@ class Work < ActiveRecord::Base
belongs_to :artist
belongs_to :category
belongs_to :location
has_many :exhibition_works
has_many :exhibitions, through: :exhibition_works
validates :location_id, :artist_id, :category_id, presence: true
end
22 changes: 22 additions & 0 deletions db/migrate/20150725234415_create_exhibitions.rb
@@ -0,0 +1,22 @@
class CreateExhibitions < ActiveRecord::Migration
def change
create_table :exhibitions do |t|
t.string :title
t.text :description
t.date :start_date
t.date :end_date
t.timestamps null: false
end

add_index :exhibitions, :start_date
add_index :exhibitions, :end_date

create_table :exhibition_works, id: false do |t|
t.references :exhibition, index: true, foreign_key: true, null: false
t.references :work, index: true, foreign_key: true, null: false
t.integer :position, null: false
end
add_index :exhibition_works, [:exhibition_id, :work_id], unique: true
add_index :exhibition_works, :position, unique: true
end
end
27 changes: 26 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: 20150725195652) do
ActiveRecord::Schema.define(version: 20150725234415) do

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

add_index "categories", ["name"], name: "index_categories_on_name", unique: true, using: :btree

create_table "exhibition_works", id: false, force: :cascade do |t|
t.integer "exhibition_id", null: false
t.integer "work_id", null: false
t.integer "position", null: false
end

add_index "exhibition_works", ["exhibition_id", "work_id"], name: "index_exhibition_works_on_exhibition_id_and_work_id", unique: true, using: :btree
add_index "exhibition_works", ["exhibition_id"], name: "index_exhibition_works_on_exhibition_id", using: :btree
add_index "exhibition_works", ["position"], name: "index_exhibition_works_on_position", unique: true, using: :btree
add_index "exhibition_works", ["work_id"], name: "index_exhibition_works_on_work_id", using: :btree

create_table "exhibitions", force: :cascade do |t|
t.string "title"
t.text "description"
t.date "start_date"
t.date "end_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "exhibitions", ["end_date"], name: "index_exhibitions_on_end_date", using: :btree
add_index "exhibitions", ["start_date"], name: "index_exhibitions_on_start_date", using: :btree

create_table "locations", force: :cascade do |t|
t.string "name", null: false
end
Expand Down Expand Up @@ -102,6 +125,8 @@
add_index "works", ["title"], name: "index_works_on_title", using: :btree
add_index "works", ["work_year"], name: "index_works_on_work_year", using: :btree

add_foreign_key "exhibition_works", "exhibitions"
add_foreign_key "exhibition_works", "works"
add_foreign_key "works", "artists"
add_foreign_key "works", "categories"
add_foreign_key "works", "locations"
Expand Down
24 changes: 22 additions & 2 deletions lib/tasks/legacy.rake
Expand Up @@ -8,6 +8,8 @@ namespace :legacy do
run_import(conn, klass.constantize)
end
import_works(conn)
run_import(conn, Exhibition)
import_exhibition_works(conn)
end
end

Expand All @@ -31,7 +33,7 @@ def transform_row(klass, row)
end

def import_works(conn)
Work.delete_all
return if Work.count > 0
works = []
conn.exec("select * from works") do |result|
result.each do |row|
Expand All @@ -44,6 +46,25 @@ def import_works(conn)
Work.import works
end

def import_exhibition_works(conn)
ExhibitionWork.delete_all
works = []
position = 1
sql = "select exhibition_id, work_id
from exhibition_works, works, artists
where work_id=works.id and artist_id=artists.id
order by artists.sort_name"
conn.exec(sql) do |result|
result.each do |row|
work = ExhibitionWork.new(row)
work.position = position
position += 1
works << work
end
end
ExhibitionWork.import works
end

def transform_work(work)
work.title = '' if work.title.strip.downcase == 'untitled'
work.featured = false if work.featured.nil?
Expand All @@ -56,4 +77,3 @@ def get_tags(conn, work)
work.tags = result.map { |i| i['name'] }
end
end

8 changes: 8 additions & 0 deletions test/fixtures/exhibitions.yml
@@ -0,0 +1,8 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

englishkills:
title: Selections from the Hoggard Wagner Collection at English Kills Art Gallery
description: English Kills Art Gallery has installed a few dozen works from the Hoggard Wagner Art Collection in an exhibition which opens with a reception this Friday, September 21, 2012 from 7 to 10 pm.
start_date: 2012-09-21
end_date: 2012-10-28

7 changes: 7 additions & 0 deletions test/models/exhibition_test.rb
@@ -0,0 +1,7 @@
require 'test_helper'

class ExhibitionTest < ActiveSupport::TestCase
test "validity" do
assert exhibitions(:englishkills).valid?
end
end

0 comments on commit 7354575

Please sign in to comment.