Skip to content

Commit

Permalink
Add models
Browse files Browse the repository at this point in the history
  • Loading branch information
fredplante committed Jan 18, 2023
1 parent 93d4fd8 commit 86277da
Show file tree
Hide file tree
Showing 57 changed files with 412 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/models/artwork.rb
@@ -0,0 +1,41 @@
# == Schema Information
#
# Table name: artworks
#
# id :bigint not null, primary key
# name :string
# author :string
# category :string
# year :integer
# tags :string default([]), is an Array
# created_at :datetime not null
# updated_at :datetime not null
#
class Artwork < ApplicationRecord
TAGS = %w[
B&W
Color
Landscape
Street
Paris
USA
Animals
Sunset
Cinema
Music
]

CATEGORIES = %w[
illustration
poster
photography
]

has_many :prints, dependent: :destroy

scope :posters, -> { where(category: "poster") }
scope :photos, -> { where(category: "photography") }
scope :illustrations, -> { where(category: "illustration") }

has_one_attached :cover_image
end
43 changes: 43 additions & 0 deletions app/models/listing.rb
@@ -0,0 +1,43 @@
# == Schema Information
#
# Table name: listings
#
# id :bigint not null, primary key
# price :integer
# sold_at :datetime
# print_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Listing < ApplicationRecord
include PgSearch::Model

belongs_to :print
has_one :artwork, through: :print

delegate :name, :cover_image, :author, to: :artwork
delegate :serial_number, :format, to: :print

scope :for_sale, -> { where(sold_at: nil) }
scope :price_between, ->(min, max) { where(price: min..max) }
scope :with_formats, ->(options) { joins(:print).where(prints: {format: options}) if options.present? }
scope :from_categories, ->(options) { joins(:artwork).where(artworks: {category: options}) if options.present? }
# Will work as an OR filter
scope :with_tags, ->(options) { joins(:artwork).where("artworks.tags && ?", "{#{options.join(",")}}") if options.present? }
scope :search, ->(query) { basic_search(query) if query.present? }

pg_search_scope :basic_search,
associated_against: {
artwork: [:name, :author]
},
using: {
tsearch: {prefix: true}
}

# Caching the max price to avoid unnecessary queries at each object initialization
def self.max_price
Rails.cache.fetch("Listing/max_floor_cents", expires_in: 1.day) do
Listing.for_sale.maximum(:price) || 10_000
end
end
end
29 changes: 29 additions & 0 deletions app/models/print.rb
@@ -0,0 +1,29 @@
# == Schema Information
#
# Table name: prints
#
# id :bigint not null, primary key
# serial_number :integer
# format :string
# artwork_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Print < ApplicationRecord
FORMATS = %w[
18x24
30x40
50x60
]

belongs_to :artwork
has_many :listings, dependent: :destroy

scope :small, -> { where(format: "18x24") }
scope :medium, -> { where(format: "30x40") }
scope :large, -> { where(format: "50x60") }

scope :posters, -> { joins(:artwork).merge(Artwork.posters) }
scope :photos, -> { joins(:artwork).merge(Artwork.photos) }
scope :illustrations, -> { joins(:artwork).merge(Artwork.illustrations) }
end
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
29 changes: 29 additions & 0 deletions db/migrate/20230118095248_create_models.rb
@@ -0,0 +1,29 @@
class CreateModels < ActiveRecord::Migration[7.0]
def change
create_table :artworks do |t|
t.string :name
t.string :author
t.string :category
t.integer :year
t.string :tags, array: true, default: []

t.timestamps
end

create_table :prints do |t|
t.integer :serial_number
t.string :format
t.references :artwork, null: false, foreign_key: true

t.timestamps
end

create_table :listings do |t|
t.integer :price
t.datetime :sold_at
t.references :print, null: false, foreign_key: true

t.timestamps
end
end
end
77 changes: 77 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 86277da

Please sign in to comment.