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 #264

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

zad6 #264

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
6 changes: 6 additions & 0 deletions zad6/app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CustomersController < ApplicationController
def index
@customers = Customer.all
end

end
6 changes: 6 additions & 0 deletions zad6/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ProductsController < ApplicationController
def index
@customer = Customer.find_by!(id: params[:customer_id])
@products = @customer.products.sorted
end
end
4 changes: 4 additions & 0 deletions zad6/app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Category < ApplicationRecord
has_many :products, through: :category_product
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
9 changes: 9 additions & 0 deletions zad6/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Product < ApplicationRecord
belongs_to :customer
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ta linia nie pozwala na stworzenie produktu, który nie ma jeszcze przypisanego customera. Można to poprawić w następujący sposób:

belongs_to :customer, optional: true

has_many :categories, through: :category_product
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ta relacja zawsze będzie podnosiła błąd, tak jak tutaj:
screen shot 2018-12-11 at 15 07 15
Rzeczywiście nie ma tutaj zdefiniowanej relacji :category_product. Powinno być tak:

has_many :category_products
has_many :categories, through: :category_products

Analogicznie z drugiej strony relacji w modelu Category 😉

validates :name, presence: true
validates :price, numericality: {greater_than: 0.00}

scope :sorted, -> { order(price: :desc, id: :asc)}

end
20 changes: 20 additions & 0 deletions zad6/app/views/customers/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="container">
<h2>All customers</h2>

<table class="table">
<thead>
<tr>
<th class="text-center">Name</th>
<th class="text-center">Mail</th>
</tr>
</thead>
<tbody>
<% @customers.each do |customer| %>
<tr>
<td><%= customer.name %></td>
<td><%= customer.mail %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
21 changes: 21 additions & 0 deletions zad6/app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="container">
<h2>Products (<%= @customer.name %>)</h2>

<table class="table">
<thead>
<tr>
<th class="text-center">Product Name</th>
<th class="text-center">Product Price</th>
</tr>
</thead>
<tbody>
<% @products.each do |p| %>
<tr>
<td><%= p.name %></td>
<td><%= p.price %></td>
</tr>
<% end %>
</tbody>
</table>
</div>

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
resources :customers do
resources :products
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
12 changes: 12 additions & 0 deletions zad6/db/migrate/20181209183630_create_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateProducts < ActiveRecord::Migration[5.2]
def change
create_table :products do |t|
t.string :name, null: false
t.decimal :price, precision: 10, scale: 2
t.text :description
t.belongs_to :customer, foreign_key: true

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions zad6/db/migrate/20181209184023_create_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateCustomers < ActiveRecord::Migration[5.2]
def change
create_table :customers do |t|
t.string :name, null: false
t.string :mail, null: false
Copy link
Collaborator

Choose a reason for hiding this comment

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

na poziomie bazy jest NULL CONSTRAINT na kolumnie mail, ale nie ma walidacji w modelu Customer - powinna się tam znaleźć żeby nie dostawać brzydkich błędów z bazy danych tylko błędy z modelu. 😉



t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions zad6/db/migrate/20181209184902_create_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCategories < ActiveRecord::Migration[5.2]
def change
create_table :categories do |t|
t.string :name
t.text :descripton

t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions zad6/db/migrate/20181209185037_create_category_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateCategoryProducts < ActiveRecord::Migration[5.2]
def change
create_table :category_products do |t|
t.belongs_to :product, index: true
t.belongs_to :category, index: true
t.timestamps
end
end
end
48 changes: 48 additions & 0 deletions zad6/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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_194146) do

create_table "categories", force: :cascade do |t|
t.string "name"
t.text "descripton"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
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.string "mail", 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.decimal "price", precision: 10, scale: 2
t.text "description"
t.integer "customer_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["customer_id"], name: "index_products_on_customer_id"
end

end