Skip to content

Commit 342a1ec

Browse files
committed
test attachment
1 parent fa44d36 commit 342a1ec

14 files changed

Lines changed: 92 additions & 26 deletions

pdv/app/controllers/products_controller.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class ProductsController < ApplicationController
22
before_action :set_product, only: [:show, :update, :destroy]
3-
skip_before_action :authenticate_request, :only => [:index, :show]
3+
skip_before_action :authenticate_request, :only => [:index, :show, :highlights]
44

55
# GET /products
66
def index
@@ -39,6 +39,12 @@ def destroy
3939
@product.destroy
4040
end
4141

42+
def highlights
43+
@highlights = Product.where(spotlight: true)
44+
45+
render json: {data: @highlights}
46+
end
47+
4248
private
4349
# Use callbacks to share common setup or constraints between actions.
4450
def set_product
@@ -47,6 +53,7 @@ def set_product
4753

4854
# Only allow a trusted parameter "white list" through.
4955
def product_params
50-
params.require(:product).permit(:sku, :item, :color, :size, :cod, :stock)
56+
params.require(:product).permit(:sku, :item, :color, :size, :cod, :stock, images: [])
5157
end
58+
5259
end

pdv/app/controllers/spotlight_controller.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.

pdv/app/models/image.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Image < ApplicationRecord
2+
end

pdv/app/models/product.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
class Product < ApplicationRecord
2+
has_many_attached :images
3+
# Note that implicit association has a plural form in this case
4+
scope :with_eager_loaded_images, -> { eager_load(images_attachments: :blob) }
25
end

pdv/app/models/product_image.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ProductImage < ApplicationRecord
2+
belongs_to :product_id
3+
belongs_to :image_id
4+
end

pdv/config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Rails.application.routes.draw do
2+
get 'products/highligths', to: 'products#highlights'
23
resources :products
34
resources :users
4-
resources :spotlight
55
post 'authenticate', to: 'auth#authenticate'
66
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
77
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateImages < ActiveRecord::Migration[5.2]
2+
def change
3+
create_table :images do |t|
4+
t.string :url
5+
6+
t.timestamps
7+
end
8+
end
9+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateProductImages < ActiveRecord::Migration[5.2]
2+
def change
3+
create_table :product_images do |t|
4+
t.references :product, foreign_key: true
5+
t.references :image, foreign_key: true
6+
7+
t.timestamps
8+
end
9+
end
10+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddGalleryToProducts < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :products, :gallery, :string
4+
end
5+
end

pdv/db/schema.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,26 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_03_21_200122) do
13+
ActiveRecord::Schema.define(version: 2019_03_25_193223) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
1717

18+
create_table "images", force: :cascade do |t|
19+
t.string "url"
20+
t.datetime "created_at", null: false
21+
t.datetime "updated_at", null: false
22+
end
23+
24+
create_table "product_images", force: :cascade do |t|
25+
t.bigint "product_id"
26+
t.bigint "image_id"
27+
t.datetime "created_at", null: false
28+
t.datetime "updated_at", null: false
29+
t.index ["image_id"], name: "index_product_images_on_image_id"
30+
t.index ["product_id"], name: "index_product_images_on_product_id"
31+
end
32+
1833
create_table "products", force: :cascade do |t|
1934
t.integer "sku"
2035
t.string "item"
@@ -28,6 +43,7 @@
2843
t.float "price"
2944
t.integer "quantity"
3045
t.boolean "spotlight"
46+
t.string "gallery"
3147
end
3248

3349
create_table "users", force: :cascade do |t|
@@ -39,4 +55,6 @@
3955
t.datetime "updated_at", null: false
4056
end
4157

58+
add_foreign_key "product_images", "images"
59+
add_foreign_key "product_images", "products"
4260
end

0 commit comments

Comments
 (0)