Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zad6 #262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

zad6 #262

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zad6/Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.3'
ruby '2.5.1'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruby 2.5.3 był jak najbardziej ok, nie trzeba było zmieniać wersji 😉 Żeby zainstalować brakującą wersję wystarczy puścić rvm install 2.5.3(zakładając, że używamy rvm, ale z tego co to było narzędzie pokazywane na wykładzie).


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.1'
Expand Down
2 changes: 1 addition & 1 deletion zad6/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ DEPENDENCIES
web-console (>= 3.3.0)

RUBY VERSION
ruby 2.5.3p105
ruby 2.5.1p57

BUNDLED WITH
1.17.1
7 changes: 7 additions & 0 deletions zad6/app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CustomersController < ApplicationController

def index
@customers = Customer.all
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do wcięć w rubym używamy 2 spacji 😉

end

end
8 changes: 8 additions & 0 deletions zad6/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ProductsController < ApplicationController

def index
@customer = Customer.find(params[:customer_id])
@products = @customer.products.order(price: :desc).includes(:categories)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O, jest nawet preloading categories - to celowo? 🤔 😄

end

end
5 changes: 5 additions & 0 deletions zad6/app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Category < ApplicationRecord
has_many :category_products
has_many :products, through: :category_products
validates :name, presence: true
end
4 changes: 4 additions & 0 deletions zad6/app/models/category_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class CategoryProduct < ApplicationRecord
belongs_to :product
belongs_to :category
end
4 changes: 4 additions & 0 deletions zad6/app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Customer < ApplicationRecord
has_many :products
validates :name, presence: true
end
7 changes: 7 additions & 0 deletions zad6/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Product < ApplicationRecord
belongs_to :customer, optional: true
has_many :category_products
has_many :categories, through: :category_products
validates :name, presence: true
validates :price, presence: true, numericality: {greater_than: 0} #format: { with: /^\d+??(?:\.\d{0,2})?$/ }
end
18 changes: 18 additions & 0 deletions zad6/app/views/customers/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>All customers</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Products</th>
</tr>
</thead>
<tbody>
<% @customers.each do |customer| %>
<tr>
<td><%= customer.name %></td>
<td><%= link_to 'Show all', customer_products_path(customer) %></td>
</tr>
<% end %>
</tbody>
</table>
1 change: 1 addition & 0 deletions zad6/app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to 'Customers', customers_path %>
3 changes: 2 additions & 1 deletion zad6/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Zad6</title>
<title>IKEA</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

Expand All @@ -10,6 +10,7 @@
</head>

<body>
<%= render 'layouts/header' %>
<%= yield %>
</body>
</html>
24 changes: 24 additions & 0 deletions zad6/app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1>All products owned by <%= @customer.name %> </h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td>
<% product.categories.each do |category| %>
<%= category.name %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
3 changes: 3 additions & 0 deletions zad6/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :customers, only: %i[index] do
resources :products, only: %i[index]
end
end
8 changes: 8 additions & 0 deletions zad6/db/migrate/20181209145358_create_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateCustomers < ActiveRecord::Migration[5.2]
def change
create_table :customers do |t|
t.string :name, null: false
t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions zad6/db/migrate/20181209145421_create_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateProducts < ActiveRecord::Migration[5.2]
def change
create_table :products do |t|
t.string :name, null: false
t.float :price, null: false
t.timestamps
end
end
end
7 changes: 7 additions & 0 deletions zad6/db/migrate/20181209145433_create_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateCategories < ActiveRecord::Migration[5.2]
def change
create_table :categories do |t|
t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions zad6/db/migrate/20181209153631_add_name_to_category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNameToCategory < ActiveRecord::Migration[5.2]
def change
add_column :categories, :name, :string
end
end
6 changes: 6 additions & 0 deletions zad6/db/migrate/20181209154003_add_customer_to_product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddCustomerToProduct < ActiveRecord::Migration[5.2]
def change
add_reference :products, :customer
add_reference :products, :categories
end
end
14 changes: 14 additions & 0 deletions zad6/db/migrate/20181209165041_create_category_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateCategoryProducts < ActiveRecord::Migration[5.2]
def change
create_table :category_products do |t|

t.references :product
t.references :category

t.timestamps
end

remove_reference :products, :categories

end
end
45 changes: 45 additions & 0 deletions zad6/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_12_09_165041) do

create_table "categories", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
end

create_table "category_products", force: :cascade do |t|
t.integer "product_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["category_id"], name: "index_category_products_on_category_id"
t.index ["product_id"], name: "index_category_products_on_product_id"
end

create_table "customers", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "products", force: :cascade do |t|
t.string "name", null: false
t.float "price", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "customer_id"
t.index ["customer_id"], name: "index_products_on_customer_id"
end

end
35 changes: 35 additions & 0 deletions zad6/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,38 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
customer1 = Customer.create!(name: "Alicja")
customer2 = Customer.create!(name: "Adrianna")

category1 = Category.create!(name: "Kuchnia")
category2 = Category.create!(name: "Sypialnia")
category3 = Category.create!(name: "Lazienka")

p1 = customer1.products.create!(name: "Zmywarka", price: 50)
p2 = customer1.products.create!(name: "Lodówka", price: 60)
p3 = customer1.products.create!(name: "Widelce", price: 70.50)
p4 = customer1.products.create!(name: "Miska", price: 8000)
p5 = customer1.products.create!(name: "Wazon", price: 90.30)
p6 = customer1.products.create!(name: "Komoda", price: 90.30)
p7 = customer1.products.create!(name: "Szafka", price: 90.30)
p8 = customer1.products.create!(name: "Stolik", price: 90.30)
p9 = customer1.products.create!(name: "Prysznic", price: 90.30)
p10 = customer1.products.create!(name: "Wanna", price: 90.30)

CategoryProduct.create!(category_id: category1.id, product_id: p1.id)
CategoryProduct.create!(category_id: category1.id, product_id: p2.id)
CategoryProduct.create!(category_id: category1.id, product_id: p3.id)
CategoryProduct.create!(category_id: category1.id, product_id: p4.id)
CategoryProduct.create!(category_id: category1.id, product_id: p5.id)

CategoryProduct.create!(category_id: category2.id, product_id: p4.id)
CategoryProduct.create!(category_id: category2.id, product_id: p5.id)
CategoryProduct.create!(category_id: category2.id, product_id: p6.id)
CategoryProduct.create!(category_id: category2.id, product_id: p7.id)
CategoryProduct.create!(category_id: category2.id, product_id: p8.id)

CategoryProduct.create!(category_id: category3.id, product_id: p6.id)
CategoryProduct.create!(category_id: category3.id, product_id: p7.id)
CategoryProduct.create!(category_id: category3.id, product_id: p8.id)
CategoryProduct.create!(category_id: category3.id, product_id: p9.id)
CategoryProduct.create!(category_id: category3.id, product_id: p10.id)