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

Finalizando plataforma #4

Merged
merged 7 commits into from
Sep 18, 2017
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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ gem 'paypal-sdk-rest'
gem 'aasm'
#Credit Card Validator
gem 'credit_card_validator'
#Gema para gráficas
gem "chartkick"
gem "groupdate"

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ GEM
sass (>= 3.3.4)
builder (3.2.3)
byebug (9.1.0)
chartkick (2.2.4)
climate_control (0.2.0)
cocaine (0.5.8)
climate_control (>= 0.0.3, < 1.0)
Expand Down Expand Up @@ -87,6 +88,8 @@ GEM
ffi (1.9.18)
globalid (0.4.0)
activesupport (>= 4.2.0)
groupdate (3.2.0)
activesupport (>= 3)
haml (5.0.3)
temple (>= 0.8.0)
tilt
Expand Down Expand Up @@ -248,10 +251,12 @@ DEPENDENCIES
bootstrap-material-design
bootstrap-sass
byebug
chartkick
coffee-rails (~> 4.1.0)
credit_card_validator
devise
factory_girl_rails (~> 4.0)
groupdate
haml-rails
jbuilder (~> 2.0)
jquery-rails
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
//= require turbolinks
//= require bootstrap
//= require bootstrap-material-design
//= require Chart.bundle
//= require chartkick
//= require_tree .
8 changes: 7 additions & 1 deletion app/assets/javascripts/welcome.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
$(document).on "ready page:load", ()->
$.material.init()
$.material.init()
$(".close-parent").on "click",()->
$(this).parent().slideUp()
$(document).on "ready page:change", ()->
$.material.init()
$(".close-parent").on "click",()->
$(this).parent().slideUp()
8 changes: 8 additions & 0 deletions app/assets/stylesheets/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ img {
margin-right: 1em;
}

.close-parent {
cursor: pointer;
}

.payment-btn {
border-radius: 3px;
border: none;
Expand All @@ -18,6 +22,10 @@ img {
outline: none;
}

a, a:hover {
color: inherit !important;
}

.paypal-btn {
background-color: #FE1935;
}
Expand Down
8 changes: 8 additions & 0 deletions app/assets/stylesheets/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
@import "bootstrap-sprockets";
@import "bootstrap";

body {
overflow : hidden;
overflow-y : scroll;
}
/* Texto */
.bold {
font-weight: bold;
Expand Down Expand Up @@ -34,6 +38,10 @@ $colors : (
padding: 5px 10px;
}

.small-padding {
padding: 0.5em;
}

.big-padding {
padding: 2em;
}
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class AttachmentsController < ApplicationController
before_action :authenticate_user!
before_action :set_attachment, only: [:destroy,:show]
before_action :set_attachment, only: [:destroy,:show,:destroy_all]
before_action :set_product, only: [:destroy]
before_action :authenticate_owner!, except: [:show]

Expand All @@ -25,6 +25,11 @@ def destroy
redirect_to @product
end

def destroy_all
@attachment.destroy_all
redirect_to @product
end

private
def set_attachment
@attachment = Attachment.find(params[:id])
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/link_attachments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class LinkAttachmentsController < ApplicationController
before_action :set_link_attachment, only: [:destroy]
def destroy_all
@linkattachment.destroy_all
end
private
def set_link_attachment
@linkattachment = LinkAttachment.where(params[:id])
end
end
9 changes: 8 additions & 1 deletion app/controllers/links_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class LinksController < ApplicationController
before_action :find_link
before_action :find_attachment_link, only: :download_attachment
before_action :set_attachment, only: [:download_attachment]
before_action :validate_link
before_action :validate_attachment_link, only: :download_attachment

Expand All @@ -12,13 +13,19 @@ def download
end

def download_attachment
send_data(Rails.root.join("archivos","#{@link_attachment.attachment_id}","original",@link_attachment.attachment.archivo_file_name), :disposition => "attachment")
send_file(@attachment.archivo.path)
end

def destroy
end

private
def find_attachment_link
@link_attachment = LinkAttachment.find(params[:attachment_id])
end
def set_attachment
@attachment = Attachment.find(@link_attachment.attachment_id)
end
def validate_attachment_link
if @link_attachment.is_invalid?
redirect_to invalid_path, notice:"Expiró el tiempo para descargar este archivo."
Expand Down
18 changes: 1 addition & 17 deletions app/controllers/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ def checkout
end
end

def process_card
paypal_helper = Stores::Paypal.new(@shopping_cart.total,
@shopping_cart.items,
return_url: checkout_url,
cancel_url: carrito_url)
if paypal_helper.process_card(params).create
@my_payment = MyPayment.create!(paypal_id: paypal_helper.payment.id,
ip: request.remote_ip,
email: params[:email],
shopping_cart_id: cookies[:shopping_cart_id])
@my_payment.pay!
redirect_to carrito_path, notice: "El pago se realizó correctamente."
else
redirect_to carrito_path notice: paypal_helper.payment.error
end
end

def create
paypal_helper = Stores::Paypal.new(@shopping_cart.total,
@shopping_cart.items,
Expand All @@ -41,6 +24,7 @@ def create
if paypal_helper.process_payment.create
@my_payment = MyPayment.create!(paypal_id: paypal_helper.payment.id,
ip: request.remote_ip,
email: params[:email],
shopping_cart_id: cookies[:shopping_cart_id])
redirect_to paypal_helper.payment.links.find{|v| v.method == "REDIRECT"}.href
else
Expand Down
21 changes: 17 additions & 4 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class ProductsController < ApplicationController
# GET /products.json
def index
@products = Product.all
end
if params[:search]
@products = Product.search(params[:search]).order("created_at DESC")
else
@products = Product.all.order('created_at DESC')
end
end

# GET /products/1
# GET /products/1.json
Expand All @@ -32,7 +37,7 @@ def create
@product = current_user.products.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.html { redirect_to @product, notice: 'Producto creado con exito.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
Expand All @@ -46,7 +51,7 @@ def create
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.html { redirect_to @product, notice: 'Producto editado con exito.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
Expand All @@ -58,9 +63,17 @@ def update
# DELETE /products/1
# DELETE /products/1.json
def destroy

@attachment = Attachment.where(product_id: params[:id])
if @attachment
#@attachment.destroy_all
@linkattachment = LinkAttachment.where(attachment_id: @attachment)
@linkattachment.destroy_all
end

@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.html { redirect_to products_url, notice: 'Producto eliminado con exito.' }
format.json { head :no_content }
end
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class WelcomeController < ApplicationController
def index
@orders = current_user.orders.order("created_at desc").index_by {|r| r[:shopping_cart_id]}.values
end
def unregistered
render layout: "landing"
Expand Down
2 changes: 1 addition & 1 deletion app/form_builders/bootstrap_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
[:text_field,:text_area,:url_field,:email_field,:number_field].each do |metodo|
define_method metodo do |name,*args|
options = args.extract_options!
aditional_classes = "form-control floating-label"
aditional_classes = "form-control floating-label top-space"
if options.has_key?(:class)
options[:class] += aditional_classes
else
Expand Down
2 changes: 1 addition & 1 deletion app/models/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Link < ActiveRecord::Base
before_create :set_defaults
after_create :send_email
belongs_to :product
has_many :link_attachments
has_many :link_attachments, dependent: :destroy

def is_invalid?
(DateTime.now > self.expiration_date || self.downloads >= 5)
Expand Down
7 changes: 6 additions & 1 deletion app/models/my_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

class MyPayment < ActiveRecord::Base
include AASM
belongs_to :shopping_cart
belongs_to :shopping_cart, dependent: :destroy
has_many :products, through: :shopping_cart

aasm column: "status" do
state :created, initial: true
Expand All @@ -30,4 +31,8 @@ class MyPayment < ActiveRecord::Base
transitions from: [:created], to: :payed
end
end

def products_by_user(user)
self.products.where(products:{user_id:user.id})
end
end
20 changes: 18 additions & 2 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@

class Product < ActiveRecord::Base
belongs_to :user
has_many :attachments
has_many :attachments, dependent: :destroy
has_many :in_shopping_carts, dependent: :destroy
has_one :shopping_cart, through: :in_shopping_carts
has_many :my_payments, through: :shopping_cart
has_many :links, dependent: :destroy
validates_presence_of :name, :user, :pricing
validates :pricing, numericality: { greater_than: 0}
has_attached_file :avatar, styles: { medium: "300x300", thumb:"100x100" }, default_url: "missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

def self.search(search)
where("name ILIKE ? OR description ILIKE ?", "%#{search}%", "%#{search}%")
end

def paypal_form
{name: name,sku: :item, price: (pricing/100),currency:"USD",quantity:1}
{name: name,sku: :item, price: (pricing),currency:"USD",quantity:1}
end

def sales
my_payments.payed
end

def sales_total
my_payments.payed.count * self.pricing
end
end
18 changes: 1 addition & 17 deletions app/models/stores/paypal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ def process_payment
self.payment
end

def process_card(card_data)
options = payment_options
options[:payer][:payment_method] = "credit_card"
options[:payer][:funding_instruments] = [{
credit_card: {
type: CreditCardValidator::Validator.card_type(card_data[:number]),
number: card_data[:number],
expire_month: card_data[:expire_month],
expire_year: card_data[:expire_year],
cvv2: card_data[:cvv2]
}
}]
self.payment = Payment.new(options)
self.payment
end

def payment_options
{
intent: "sale",
Expand All @@ -51,7 +35,7 @@ def payment_options
items: self.items
},
amount: {
total: (self.total/100),
total: (self.total),
currency: "USD"
},
description: "Compra de tus productos en nuestra plataforma."
Expand Down
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :products

def orders
MyPayment.joins(:products)
.joins("LEFT JOIN users ON products.user_id = users.id")
.where(users:{id: self.id})
end
end
2 changes: 1 addition & 1 deletion app/views/attachments/_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
= f.errors
= f.hidden_field :product_id, value: product_id
= f.file_field :archivo
= f.submit "Enviar"
= f.submit "Cargar"
16 changes: 16 additions & 0 deletions app/views/devise/confirmations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Resend confirmation instructions</h2>

<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
</div>

<div class="actions">
<%= f.submit "Resend confirmation instructions" %>
</div>
<% end %>

<%= render "devise/shared/links" %>
5 changes: 5 additions & 0 deletions app/views/devise/mailer/confirmation_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
Loading