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

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

zad6 #281

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
1 change: 1 addition & 0 deletions zad6/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0'
end

gem 'faker'

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
3 changes: 3 additions & 0 deletions zad6/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ GEM
crass (1.0.4)
erubi (1.7.1)
execjs (2.7.0)
faker (1.9.1)
i18n (>= 0.7)
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -171,6 +173,7 @@ DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
coffee-rails (~> 4.2)
faker
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
puma (~> 3.11)
Expand Down
3 changes: 3 additions & 0 deletions zad6/app/assets/javascripts/customer.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions zad6/app/assets/javascripts/products.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions zad6/app/assets/stylesheets/customer.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Customer controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions zad6/app/assets/stylesheets/products.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
5 changes: 5 additions & 0 deletions zad6/app/controllers/customer_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CustomerController < ApplicationController
def index
@customer = Customer.all
end
end
9 changes: 9 additions & 0 deletions zad6/app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ProductsController < ApplicationController

def index
@customer = Customer.find_by!(name: params[:customer_name])
@products = @customer.product.sorted
@products = params.has_key?(:price) ? @products.cheaper_than(params[:price]) : @products
end

end
2 changes: 2 additions & 0 deletions zad6/app/helpers/customer_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CustomerHelper
end
2 changes: 2 additions & 0 deletions zad6/app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ProductsHelper
end
3 changes: 3 additions & 0 deletions zad6/app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Category < ApplicationRecord
has_many :products, through: :category_product
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
3 changes: 3 additions & 0 deletions zad6/app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Customer < ApplicationRecord
has_many :product
end
8 changes: 8 additions & 0 deletions zad6/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Product < ApplicationRecord
belongs_to :customer
has_many :categories, through: :category_product

scope :sorted, -> { order(price: :desc) }
scope :cheaper_than, -> p { where('price < ?', p) }

end
17 changes: 17 additions & 0 deletions zad6/app/views/customer/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="container">
<h2>All customers</h2>
<table class="table">
<thead>
<tr>
<th class="text-center">Name</th>
</tr>
</thead>
<tbody>
<% @customer.each do |customer| %>
<tr>
<td><li> <%= link_to customer.name, customer_products_path(customer_name: customer.name) %> </li></td>
</tr>
<% end %>
</tbody>
</table>
</div>
5 changes: 5 additions & 0 deletions zad6/app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ul>
<% @product.each do |product| %>
<li> <b><%= product.title %></b> | Price: <b><%= product.price %></b> </li>
<% end %>
</ul>
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 :customer, param: :name, only: [:index] do
resources :products, only: [:index]
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
9 changes: 9 additions & 0 deletions zad6/db/migrate/20181210222512_create_customers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateCustomers < ActiveRecord::Migration[5.2]
def change
create_table :customers do |t|
t.string :name, null: false

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions zad6/db/migrate/20181210222523_create_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateProducts < ActiveRecord::Migration[5.2]
def change
create_table :products do |t|
t.decimal :price, precision: 10, scale: 2
t.references :customer, foreign_key: true

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

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions zad6/db/migrate/20181210225347_create_category_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
42 changes: 42 additions & 0 deletions zad6/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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_10_225347) do

create_table "categories", force: :cascade do |t|
t.string "name", null: false
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.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "products", force: :cascade do |t|
t.decimal "price", precision: 10, scale: 2
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
5 changes: 5 additions & 0 deletions zad6/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
50.times do
Customer.create!(
name: Faker::ElderScrolls.first_name
)
end