Skip to content

Commit

Permalink
Merge pull request #8 from leanddro/add_image_in_item
Browse files Browse the repository at this point in the history
Add image in item
  • Loading branch information
leanddro authored May 11, 2023
2 parents 02dc201 + 8ede08c commit 861eb1d
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ def show

def item_params
params.require(:item)
.permit(:name, :description, :weight, :width, :height, :depth, :category_id )
.permit(:name, :description, :weight, :width, :height, :depth, :category_id, :photo)
end
end
7 changes: 7 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class Item < ApplicationRecord
after_initialize :set_default_status, :if => :new_record?
belongs_to :create_by, class_name: "User", foreign_key: "create_by_id"
belongs_to :category
has_one_attached :photo

before_validation :generate_code, on: :create
validates :name, :description, :weight, :width, :height, :depth, presence: true
validates :code, uniqueness: true

enum status: {available: 1, unavailable: 5, sealed: 9}

def dimension
"#{self.width}(L) x #{self.depth}(P) x #{self.height}(A) cm"
Expand All @@ -16,4 +19,8 @@ def dimension
def generate_code
self.code = SecureRandom.alphanumeric(10).upcase
end

def set_default_status
self.status = :available
end
end
4 changes: 4 additions & 0 deletions app/views/items/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<%= f.label :category_id, class: 'label' %>
<%= f.collection_select :category_id, @categories, :id, :name, { prompt: false }, class: 'input input-bordered w-full max-w-ws' %>
</div>
<div class="col-span-4">
<%= f.label :photo, class: 'label' %>
<%= f.file_field :photo, class: 'file-input file-input-bordered w-full' %>
</div>
<div>
<%= f.submit t('.register'), class: 'btn btn-accent' %>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/views/items/_item.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<div class="card w-full bg-base-300 shadow-xl">
<% if item.photo.attached? %>
<figure><%= image_tag item.photo, class: 'w-1/2' %></figure>
<% end %>
<div class="card-body">
<h2 class="card-title"><%= item.name %></h2>
<p><%= item.description %></p>
Expand Down
12 changes: 11 additions & 1 deletion app/views/items/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
<%= render @item %>
<div class="card w-full bg-base-300 shadow-xl flex flex-row">
<% if @item.photo.attached? %>
<figure><%= image_tag @item.photo, class: 'w-1/2' %></figure>
<% end %>
<div class="card-body">
<h2 class="card-title"><%= @item.name %></h2>
<p><%= @item.description %></p>
<h4 class="font-bold"><%= t(".dimension") %></h4>
<p> - <%= @item.dimension %></p>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :active_storage_blobs, id: primary_key_type do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :key ], unique: true
end

create_table :active_storage_attachments, id: primary_key_type do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type
t.references :blob, null: false, type: foreign_key_type

if connection.supports_datetime_with_precision?
t.datetime :created_at, precision: 6, null: false
else
t.datetime :created_at, null: false
end

t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records, id: primary_key_type do |t|
t.belongs_to :blob, null: false, index: false, type: foreign_key_type
t.string :variation_digest, null: false

t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end

private
def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end
32 changes: 31 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
expect(result).to be true
end

it 'o status deve ser :available' do
user = User.create!(name: 'Admir', cpf: '054.732.630-03', email: 'admir@leilaodogalpao.com.br', password: 'password')
category = Category.create!(name: 'Eletrônico', description: 'Incididunt officia occaecat consequat aliquip pariatur aute culpa.')
item = Item.create!(name: 'Notebook Gamer Acer Nitro 5', description: 'Intel Core i7-11800H, GeForce GTX 1650, 8GB RAM, SSD 512GB, 15.6 Full HD 144Hz IPS, Windows 11, Preto - AN515-57-740K', weight: 2300, width: 363.4, height: 24.35, depth: 255, category: category, create_by: user)

expect(item.status).to eq 'available'
end

end

describe 'Deve gera código aleatório' do
Expand Down
Binary file added spec/support/img/Notebook Gamer Acer Nitro 5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions spec/system/items/user_admin_register_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@
expect(page).to have_content '- 363.4(L) x 255.0(P) x 24.35(A) cm'
end

it 'com sucesso com imagem' do
user = User.create!(name: 'Admir', cpf: '054.732.630-03', email: 'admir@leilaodogalpao.com.br', password: 'password')
category = Category.create!(name: 'Eletrônico', description: 'Incididunt officia occaecat consequat aliquip pariatur aute culpa.')

login_as user
visit root_path

click_on 'Itens'
click_on 'Novo'

fill_in 'Nome', with: 'Notebook Gamer Acer Nitro 5'
fill_in 'Descrição', with: 'Intel Core i7-11800H, GeForce GTX 1650, 8GB RAM, SSD 512GB, 15.6 Full HD 144Hz IPS, Windows 11, Preto - AN515-57-740K'
fill_in 'Peso', with: 2300
fill_in 'Largura', with: 363.4
fill_in 'Altura', with: 24.35
fill_in 'Profundidade', with: 255
select 'Eletrônico', from: 'Categoria'
attach_file Rails.root.join('spec/support/img/Notebook Gamer Acer Nitro 5.png')

click_on 'Cadastrar'

img = URI.encode_uri_component 'Notebook Gamer Acer Nitro 5.png'

expect(page).to have_css "img[src*='#{img}']"
expect(page).to have_content 'Notebook Gamer Acer Nitro 5'
expect(page).to have_content 'Intel Core i7-11800H, GeForce GTX 1650, 8GB RAM, SSD 512GB, 15.6 Full HD 144Hz IPS, Windows 11, Preto - AN515-57-740K'
expect(page).to have_content '- 363.4(L) x 255.0(P) x 24.35(A) cm'
end

it 'falha compos obrigatórios' do
user = User.create!(name: 'Admir', cpf: '054.732.630-03', email: 'admir@leilaodogalpao.com.br', password: 'password')
category = Category.create!(name: 'Eletrônico', description: 'Incididunt officia occaecat consequat aliquip pariatur aute culpa.')
Expand Down
25 changes: 25 additions & 0 deletions spec/system/items/user_admin_view_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,30 @@
expect(page).to have_content 'Intel Core i7-11800H, GeForce GTX 1650, 8GB RAM, SSD 512GB, 15.6 Full HD 144Hz IPS, Windows 11, Preto - AN515-57-740K'
expect(page).to have_content '- 363.4(L) x 255.0(P) x 24.35(A) cm'
end

it 'com sucesso com imagem' do
user = User.create!(name: 'Admir', cpf: '054.732.630-03', email: 'admir@leilaodogalpao.com.br', password: 'password')
category = Category.create!(name: 'Eletrônico', description: 'Incididunt officia occaecat consequat aliquip pariatur aute culpa.')
item = Item.create!(name: 'Notebook Gamer Acer Nitro 5', description: 'Intel Core i7-11800H, GeForce GTX 1650, 8GB RAM, SSD 512GB, 15.6 Full HD 144Hz IPS, Windows 11, Preto - AN515-57-740K', weight: 2300, width: 363.4, height: 24.35, depth: 255, category: category, create_by: user)

item.photo.attach(io: File.open(Rails.root.join('spec/support/img/Notebook Gamer Acer Nitro 5.png')), filename: 'Notebook Gamer Acer Nitro 5.png', content_type: "image/png")
item.save!

login_as user
visit root_path

within 'nav' do
click_on 'Itens'
end
find("a[href='/items/#{item.id}']").click

img = URI.encode_uri_component 'Notebook Gamer Acer Nitro 5.png'

expect(page).to have_css "img[src*='#{img}']"
expect(current_path).to eq item_path(item.id)
expect(page).to have_content 'Notebook Gamer Acer Nitro 5'
expect(page).to have_content 'Intel Core i7-11800H, GeForce GTX 1650, 8GB RAM, SSD 512GB, 15.6 Full HD 144Hz IPS, Windows 11, Preto - AN515-57-740K'
expect(page).to have_content '- 363.4(L) x 255.0(P) x 24.35(A) cm'
end
end

0 comments on commit 861eb1d

Please # to comment.