-
Notifications
You must be signed in to change notification settings - Fork 58
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
base: master
Are you sure you want to change the base?
zad6 #264
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class CustomersController < ApplicationController | ||
def index | ||
@customers = Customer.all | ||
end | ||
|
||
end |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class CategoryProduct < ApplicationRecord | ||
belongs_to :product | ||
belongs_to :category | ||
end |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Product < ApplicationRecord | ||
belongs_to :customer | ||
has_many :categories, through: :category_product | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
validates :name, presence: true | ||
validates :price, numericality: {greater_than: 0.00} | ||
|
||
scope :sorted, -> { order(price: :desc, id: :asc)} | ||
|
||
end |
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> |
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> | ||
|
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 |
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. na poziomie bazy jest NULL CONSTRAINT na kolumnie |
||
|
||
|
||
t.timestamps | ||
end | ||
end | ||
end |
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 |
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 |
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 |
There was a problem hiding this comment.
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: