Skip to content

Commit

Permalink
feat: add routes to upsert and delete images
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasconstancio committed Jan 26, 2022
1 parent 663c720 commit dbe7bca
Show file tree
Hide file tree
Showing 25 changed files with 395 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ gem 'jbuilder', '~> 2.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

gem 'active_storage-postgresql'

gem 'devise'

# Reduces boot times through caching; required in config/boot.rb
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_storage-postgresql (0.3.0)
pg (>= 1.0)
rails (>= 6.0)
activejob (6.1.4.4)
activesupport (= 6.1.4.4)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -218,6 +221,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
active_storage-postgresql
bootsnap (>= 1.4.4)
byebug
capybara (>= 3.26)
Expand Down
49 changes: 49 additions & 0 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class ImagesController < ApplicationController
before_action :set_image, only: %i[show edit update destroy]

def index
@images = Image.all
end

def show; end

def new
@image = Image.new
end

def edit; end

def create
@image = Image.new(image_params)

if @image.save
redirect_to @image, notice: 'Imagem criada!'
else
render :new, status: :unprocessable_entity
end
end

def update
if @image.update(image_params)
redirect_to @image, notice: 'Imagem atualizada!'
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@image.destroy

redirect_to images_url, notice: 'Imagem deletada!'
end

private

def set_image
@image = Image.find(params[:id])
end

def image_params
params.require(:image).permit(:title, :file, :user_id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/images_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ImagesHelper
end
8 changes: 8 additions & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ Rails.start()
Turbolinks.start()
ActiveStorage.start()

document.addEventListener('turbolinks:load', () => {
$('.custom-file-input').on('change',function(){
const fileName = $(this).val().split('\\');

$(this).next('.custom-file-label').html(fileName[fileName.length - 1]);
})
})

5 changes: 5 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Image < ApplicationRecord
belongs_to :user

has_one_attached :file
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable

has_many :images
end
6 changes: 3 additions & 3 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<h5 class="card-title">Bem Vindo!</h5>
<% if user_signed_in? %>
<p class="card-text">Seja bem vindo ao meu site :D</p>
<p class="card-text">Por favor, faça login ou se cadastre:</p>
<div class="btn-group" role="group">
<%= link_to 'Upload de Imagens', '', :class => 'navbar-link' %>
<p class="card-text">Escolha uma das ações a serem feitas:</p>
<div class="btn-group d-flex" role="group">
<%= link_to 'Upload de Imagens', images_path, :class => 'btn btn-primary' %>
</div>
<% else %>
<p class="card-text">Seja bem vindo ao meu site :D</p>
Expand Down
32 changes: 32 additions & 0 deletions app/views/images/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= form_with(model: image, local: true) do |form| %>
<% if image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(image.errors.count, "error") %> prohibited this image from being saved:</h2>

<ul>
<% image.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form.hidden_field(:user_id, value: current_user.id) %>

<div class="form-group">
<%= form.label :title, 'Título', class: 'form-label' %>
<%= form.text_field :title, required: true, class: 'form-control' %>
</div>

<div class="form-group">
<div class="custom-file">
<%= form.file_field :file, required: true, class: 'custom-file-input' %>
<%= form.label :file, 'Escolha o arquivo...', class: 'custom-file-label' %>
<div class="invalid-feedback">Example invalid custom file feedback</div>
</div>
</div>

<div class="btn-group d-flex">
<%= form.submit 'Enviar', class: 'btn btn-success' %>
</div>
<% end %>
12 changes: 12 additions & 0 deletions app/views/images/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<h1 class="text-center">Editar imagem</h1>

<%= render 'form', image: @image %>

<hr>

<div class="btn-group">
<%= link_to 'Detalhes', @image, class: 'btn btn-primary' %>
<%= link_to 'Voltar', images_path, class: 'btn btn-secondary' %>
</div>
</div>
29 changes: 29 additions & 0 deletions app/views/images/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Images</h1>

<table>
<thead>
<tr>
<th>Title</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @images.each do |image| %>
<tr>
<td><%= image.title %></td>
<td><%= image.user_id %></td>
<td><%= link_to 'Show', image %></td>
<td><%= link_to 'Edit', edit_image_path(image) %></td>
<td><%= link_to 'Destroy', image, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Image', new_image_path %>
8 changes: 8 additions & 0 deletions app/views/images/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
<h1 class="text-center">Nova Imagem</h1>
<%= render 'form', image: @image %>

<hr>

<%= link_to 'Voltar', images_path, class: 'btn btn-secondary' %>
</div>
14 changes: 14 additions & 0 deletions app/views/images/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Title:</strong>
<%= @image.title %>
</p>

<p>
<strong>User:</strong>
<%= @image.user_id %>
</p>

<%= link_to 'Edit', edit_image_path(@image) %> |
<%= link_to 'Back', images_path %>
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
end

# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local
config.active_storage.service = :mirror

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local
config.active_storage.service = :mirror

# Mount Action Cable outside main process or domain.
# config.action_cable.mount_path = nil
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :images
devise_for :users
root 'home#index', as: :home
end
39 changes: 10 additions & 29 deletions config/storage.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

local:
service: Disk
root: <%= Rails.root.join("storage") %>
root: <%= Rails.root.join('storage') %>

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
# amazon:
# service: S3
# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
# region: us-east-1
# bucket: your_own_bucket
pg:
service: PostgreSQL

# Remember not to checkin your GCS keyfile to a repository
# google:
# service: GCS
# project: your_project
# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %>
# bucket: your_own_bucket
mirror:
service: Mirror
primary: local
mirrors: [ pg ]

# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key)
# microsoft:
# service: AzureStorage
# storage_account_name: your_account_name
# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %>
# container: your_container_name

# mirror:
# service: Mirror
# primary: local
# mirrors: [ amazon, google, microsoft ]
test:
service: Disk
root: <%= Rails.root.join('tmp/storage') %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs 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, null: false
t.datetime :created_at, null: false

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

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

t.datetime :created_at, null: false

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 do |t|
t.belongs_to :blob, null: false, index: false
t.string :variation_digest, null: false

t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This migration comes from active_storage_postgresql (originally 20180530020601)
class CreateActiveStoragePostgresqlTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_postgresql_files do |t|
t.oid :oid
t.string :key

t.index :key, unique: true
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20220126131316_create_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateImages < ActiveRecord::Migration[6.1]
def change
create_table :images do |t|
t.string :title
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
Loading

0 comments on commit dbe7bca

Please sign in to comment.