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

Payment method crud #13

Merged
merged 2 commits into from
Jul 19, 2024
Merged
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
48 changes: 48 additions & 0 deletions app/controllers/payment_methods_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class PaymentMethodsController < ApplicationController
def index
@payment_methods = PaymentMethod.where(user: current_user)
end

def new
@payment_method = PaymentMethod.new
end

def show
@payment_method = PaymentMethod.find(params[:id])
end

def create
@payment_method = PaymentMethod.new(payment_method_params)
@payment_method.user = current_user
if @payment_method.save
redirect_to payment_methods_path
else
render 'new', status: :unprocessable_entity
end
end

def edit
@payment_method = PaymentMethod.find(params[:id])
end

def update
@payment_method = PaymentMethod.find(params[:id])
if @payment_method.update(payment_method_params)
redirect_to payment_methods_path
else
render 'edit', status: :unprocessable_entity
end
end

def destroy
@payment_method = PaymentMethod.find(params[:id])
@payment_method.destroy
redirect_to payment_methods_path
end

private

def payment_method_params
params.require(:payment_method).permit(:name, :owner, :card_number, :due_date, :CVV)
end
end
2 changes: 1 addition & 1 deletion app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def purchase_params_with_defaults
params.permit(:amount, customization_ids: []).merge(gift: selected_gift,
payment_method: first_payment_method,
subtotal:
params[:price].to_i * selected_amount)
params[:price].to_i * selected_amount)
end
end
2 changes: 2 additions & 0 deletions app/models/payment_method.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class PaymentMethod < ApplicationRecord
belongs_to :user
has_many :purchases

validates :name, :owner, :card_number, :due_date, :CVV, presence: true
end
40 changes: 40 additions & 0 deletions app/views/payment_methods/_data_payment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<%= form_with model: payment_method do |form| %>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.label :name, 'Nombre', class:'fs-2 fw-light' %>
<%= form.text_field :name, disabled: disable, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4', placeholder: 'Nombre' %>
<% payment_method.errors.full_messages_for(:name).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.label :owner, 'Propietario', class:'fs-2 fw-light' %>
<%= form.text_field :owner, disabled: disable, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4' %>
<% payment_method.errors.full_messages_for(:owner).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.label :card_number, 'Número de tarjeta', class:'fs-2 fw-light' %>
<%= form.text_field :card_number, disabled: disable, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4' %>
<% payment_method.errors.full_messages_for(:card_number).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.label :due_date, 'Fecha de expiración', class:'fs-2 fw-light' %>
<%= form.date_field :due_date, disabled: disable, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4' %>
<% payment_method.errors.full_messages_for(:due_date).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.label :CVV, 'CVV', class:'fs-2 fw-light' %>
<%= form.text_field :CVV, disabled: disable, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4' %>
<% payment_method.errors.full_messages_for(:CVV).each do |message| %>
<p class = "text-danger"><%= message %></p>
<% end %>
</div>
<div class=" <%= disable ? 'd-none' : 'actions' %>">
<%= form.submit 'Actualizar', class: 'color-white bg-Blue-lg-30deg border-1 rounded-2 hover-bg-white fs-3 fs-lg-4 fs-md-6 hover-border-solid fs-xl-4 hover-border-color-mixBlue border-color-transparent w-100' %>
</div>
<% end %>
15 changes: 15 additions & 0 deletions app/views/payment_methods/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% content_for :header do %>
<% render 'header', show_mobile: true, categories: @categories, filters: @filter, selected_tab: :none %>
<% end %>
<div class="px-md-6 py-md-6 d-flex justify-content-between align-items-baseline">
<div class="d-flex flex-column w-lg-50">
<h1 class= "bg-text-gradient-blue-20 text-fill-color-transparent mb-5">Información</h1>
<%= render 'data_payment', payment_method: @payment_method, disable: false%>
<div class="d-flex d-md-none mt-5">
<%= link_to 'Volver', payment_method_path(@payment_method), class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
<div class="d-none d-md-flex fs-md-5 fs-lg-3 ">
<%= link_to 'Volver', payment_method_path(@payment_method), class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
25 changes: 25 additions & 0 deletions app/views/payment_methods/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<% content_for :header do %>
<% render 'header', show_mobile: true, categories: @categories, filters: @filter, selected_tab: :none %>
<% end %>
<div class="d-flex justify-content-between align-items-baseline">
<div class="d-flex flex-column">
<h1 class= "bg-text-gradient-blue-20 text-fill-color-transparent">Mis métodos de pago</h1>
<ul class='mb-8'>
<% @payment_methods.each do |payment_method| %>
<li>
<%= link_to payment_method.name, payment_method_path(payment_method), class: 'fw-light text-blue-2 text-decoration-none mb-0' %> - <%= payment_method.owner %>
</li>
<% end %>
</ul>
<div class="d-flex flex-column d-md-none">
<%= link_to 'Agregar nuevo método de pago', new_payment_method_path, class: 'fw-light text-blue-2 text-decoration-none mb-3' %>
<%= link_to 'Volver', users_show_path, class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
<div class="">
<div class="d-none d-md-flex flex-column align-items-end fs-md-5 fs-lg-3">
<%= link_to 'Agregar nuevo método de pago', new_payment_method_path, class: 'fw-light text-blue-2 text-decoration-none mb-1' %>
<%= link_to 'Volver', users_show_path, class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
</div>
49 changes: 49 additions & 0 deletions app/views/payment_methods/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<% content_for :header do %>
<% render 'header', show_mobile: true, categories: @categories, filters: @filter, selected_tab: :none %>
<% end %>
<div class="px-md-6 py-md-6 d-flex justify-content-between align-items-baseline">
<div class="d-flex flex-column w-lg-50">
<h1 class= "bg-text-gradient-blue-20 text-fill-color-transparent mb-5">Nuevo método de pago</h1>
<%= form_with model: @payment_method do |form| %>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.text_field :name, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4', placeholder: 'Nombre' %>
<% @payment_method.errors.full_messages_for(:name).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.text_field :owner, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4', placeholder: 'Propietario' %>
<% @payment_method.errors.full_messages_for(:owner).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.text_field :card_number, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4', placeholder: 'Número de tarjeta' %>
<% @payment_method.errors.full_messages_for(:card_number).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.date_field :due_date, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4', placeholder: 'Fecha de expiración' %>
<% @payment_method.errors.full_messages_for(:due_date).each do |message| %>
<p class= "text-danger"><%= message %></p>
<% end %>
</div>
<div class="field mb-lg-5 mb-6 mb-md-8">
<%= form.text_field :CVV, class: 'border-0 border-bottom-solid outline-none text-role-grey bg-transparent grey-placeholder w-100 fs-md-6 fs-lg-4', placeholder: 'CVV' %>
<% @payment_method.errors.full_messages_for(:CVV).each do |message| %>
<p class = "text-danger"><%= message %></p>
<% end %>
</div>
<div class="actions">
<%= form.submit 'Agregar', class: 'color-white bg-Blue-lg-30deg border-1 rounded-2 hover-bg-white fs-3 fs-lg-4 fs-md-6 hover-border-solid fs-xl-4 hover-border-color-mixBlue border-color-transparent w-100' %>
</div>
<% end %>
<div class="d-flex d-md-none mt-5">
<%= link_to 'Volver', payment_methods_path, class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
<div class="d-none d-md-flex">
<%= link_to 'Volver', payment_methods_path, class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
19 changes: 19 additions & 0 deletions app/views/payment_methods/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% content_for :header do %>
<% render 'header', show_mobile: true, categories: @categories, filters: @filter, selected_tab: :none %>
<% end %>
<div class="px-md-6 py-md-6 d-flex justify-content-between align-items-baseline">
<div class="d-flex flex-column w-lg-50">
<h1 class= "bg-text-gradient-blue-20 text-fill-color-transparent mb-5">Información</h1>
<%= render 'data_payment', payment_method: @payment_method, disable: true%>
<div class="d-flex flex-column d-md-none mt-5">
<%= link_to 'Editar', edit_payment_method_path(@payment_method), class: 'fw-light text-blue-2 text-decoration-none mb-2' %>
<%= button_to 'Eliminar', payment_method_path(@payment_method), data: { confirm: "¿estás seguro?", turbo_confirm: "¿estás seguro?" }, method: :delete, class: 'fw-light text-blue-2 text-decoration-none mb-2 bg-transparent border-0 p-0' %>
<%= link_to 'Volver', payment_methods_path, class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
<div class="d-none d-md-flex flex-column fs-md-5 fs-lg-3">
<%= link_to 'Editar', edit_payment_method_path(@payment_method), class: 'fw-light text-blue-2 text-decoration-none mb-2' %>
<%= button_to 'Eliminar', payment_method_path(@payment_method), data: { confirm: "¿estás seguro?", turbo_confirm: "¿estás seguro?" }, method: :delete, class: 'fw-light text-blue-2 text-decoration-none mb-2 bg-transparent border-0 p-0' %>
<%= link_to 'Volver', payment_methods_path, class: 'fw-light text-blue-2 text-decoration-none mb-0' %>
</div>
</div>
4 changes: 1 addition & 3 deletions app/views/purchases/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@
</div>
<div class="d-flex flex-column mt-sm-22 mt-13 mb-4 mb-lg-2 mt-md-23 mt-lg-0">
<h4 class="fs-3 fs-sm-5 mt-md-5 fw-medium fs-lg-2 mt-lg-3 fw-lg-normal">Método de Pago</h4>
<p class="fs-2 fs-sm-4 fw-light text-role-grey mb-0 fs-lg-1">Pago a través de <%= @purchase.payment_method_name %></p>
<%= form.hidden_field :payment_method_id, value: @purchase.payment_method.id %>
<p class= 'fw-light text-blue-2 text-decoration-none fs-1 fs-sm-3 fs-lg-1 mb-lg-0'> Cambiar </p>
<p class="fs-2 fs-sm-4 fw-light text-role-grey mb-0 fs-lg-1">Pago a través de <%= form.collection_select :payment_method_id, PaymentMethod.where(user: current_user), :id, :name %></p>
</div>
<div class="d-flex flex-column">
<h4 class='fw-medium fs-3 fs-sm-5 fs-lg-2 fw-lg-normal mb-0'>Información de facturación</h4>
Expand Down
1 change: 1 addition & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</div>
<div class="mt-7 d-flex flex-column fs-2 fs-md-5 fs-lg-4 align-items-lg-end">
<%= link_to 'Editar información', edit_user_registration_path, class: 'fw-light text-blue-2 text-decoration-none mb-3' %>
<%= link_to "Ver métodos de pago", payment_methods_path, class: 'fw-light text-blue-2 text-decoration-none mb-3' %>
<%= button_to "Cerrar cuenta", registration_path(@user), data: { confirm: "¿estás seguro?", turbo_confirm: "¿estás seguro?" }, method: :delete, class: 'fw-light text-blue-2 text-decoration-none bg-lg-white bg-transparent border-0 p-0' %>
</div>
</div>
12 changes: 12 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ es:
activerecord:
errors:
models:
payment_method:
attributes:
name:
blank: "Debe agregar un nombre"
owner:
blank: "Debe agregar un propietario"
card_number:
blank: "Debe agregar un número de tarjeta"
due_date:
blank: "Debe agregar una fecha de expiración"
CVV:
blank: "Debe agregar un CVV"
user:
attributes:
email:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# root "posts#index"

root 'gifts#index'
resources :payment_methods, path: 'metodos-de-pago'

resources :gifts, path: 'regalos', only: [:index, :show]
resources :catering, only: [:index]
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/payment_methods_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class PaymentMethodsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
Loading