class Artist
attr_reader :name, :years_experience
@@all = []
def initialize(name, years_experience)
@name = name
@years_experience = years_experience
@@all << self
end
def self.all
@@all
end
def paintings
Painting.all.select{|painting| painting.artist == self}
end
def galleries
self.paintings.map{|painting| painting.gallery}.uniq
end
def cities
self.galleries.map{|gallery| gallery.city}.uniq
end
def self.total_experience
Artist.all.sum{|artist| artist.years_experience}
end
def proficiency
self.paintings.count.to_f / self.years_experience
end
def self.most_prolific
Artist.all.max_by{|artist| artist.proficiency}
end
def create_painting(title, price, gallery)
Painting.new("#{title}", "#{price}", self, "#{gallery}")
end
end
class Painting
attr_reader :title, :price, :artist, :gallery
@@all = []
def initialize(title, price, artist, gallery)
@title = title
@price = price
@artist = artist
@gallery = gallery
@@all << self
end
def self.all
@@all
end
def self.total_price
Painting.all.map{|painting| painting.price}.sum
end
end
class Gallery
attr_reader :name, :city
@@all = []
def initialize(name, city)
@name = name
@city = city
@@all << self
end
def self.all
@@all
end
def paintings
Painting.all.select{|painting| painting.gallery == self}
end
def artists
self.paintings.map{|painting| painting.artist}.uniq
end
def artist_names
self.paintings.map{|painting| painting.artist.name}.uniq
end
def most_expensive_painting
self.paintings.max_by{|painting| painting.price}
end
end
*console.rb
require_relative '../config/environment.rb'
ben = Artist.new("Ben", 1)
maggie = Artist.new("Maggie", 9)
fra = Gallery.new("FRA", "New York")
buckets = Gallery.new("Galerie Buckets", "Cologne")
shark_tonk_2 = Painting.new("Shark Tonk II", 5000, ben, fra)
shark_tonk = Painting.new("Shark Tonk", 5000, ben, fra)
heart_fountain = Painting.new("Heart Fountain", 10000, maggie, buckets)
unhappy_break_up = Painting.new("Unhappy Break Up", 12000, maggie, buckets)
unhappy_break_up_2 = Painting.new("Unhappy Break Up II", 12000, maggie, fra)
tile = Painting.new("Tile", 6000, maggie, buckets)
binding.pry
puts "Bob Ross rules."
# Art Gallery Lab
In this project, we will be practicing object relationships in Ruby, with a particular emphasis on the `has_many` `through` relationship (aka: many-to-many). Please read the whole README before writing any code!
### Introduction
You got yourself a fancy new job and you've been put in charge of making a program to keep track of paintings, and the galleries that are selling them.
Each painting has a price, and a title. Each artist has a name and an amount of years of experience, and each gallery has a name and a city.
The initial files and basic attributes have already been built, but you'll need to figure out the relationships and build the rest of the app out yourself!
## Deliverables
### Model the Domain
An artist can, of course, have many paintings. Paintings can only be in one gallery at a time, and only have one artist. Galleries can have many paintings.
* What are your models?
* What does your schema look like?
* What are the relationships between your models?
### Class Attributes and Methods
**ARTIST**
<!--
* `Artist.all`
* Returns an `array` of all the artists
* `Artist#paintings`
* Returns an `array` all the paintings by an artist
* `Artist#galleries`
* Returns an `array` of all the galleries that an artist has paintings in -->
<!--
* `Artist#cities`
* Return an `array` of all cities that an artist has paintings in -->
<!--
* `Artist.total_experience`
* Returns an `integer` that is the total years of experience of all artists
* `Artist.most_prolific`
* Returns an `instance` of the artist with the highest amount of paintings per year of experience.
* `Artist#create_painting`
* Given the arguments of `title`, `price` and `gallery`, creates a new painting belonging to that artist -->
**PAINTING**
<!--
* `Painting.all`
* Returns an `array` of all the paintings
* `Painting.total_price`
* Returns an `integer` that is the total price of all paintings -->
**GALLERY**
<!-- * `Gallery.all`
* Returns an `array` of all the galleries
* `Gallery#paintings`
* Returns an `array` of all paintings in a gallery -->
<!-- * `Gallery#artists`
* Returns an `array` of all artists that have a painting in a gallery -->
<!--
* `Gallery#artist_names`
* Returns an `array` of the names of all artists that have a painting in a gallery -->
* `Gallery#most_expensive_painting`
* Returns an `instance` of the most expensive painting in a gallery