secret
Created

  • Download Gist
purchases_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#coding=UTF-8
class PurchasesController < ApplicationController
layout "objects"
before_filter :require_login#, :only => :index
load_and_authorize_resource
# GET /purchases
# GET /purchases.json
def index
@purchases = Purchase.all
 
respond_to do |format|
format.html # index.html.erb
format.json { render json: @purchases }
end
end
 
# GET /purchases/1
# GET /purchases/1.json
def show
@purchase = Purchase.find(params[:id])
 
respond_to do |format|
format.html # show.html.erb
format.json { render json: @purchase }
end
end
 
# GET /purchases/new
# GET /purchases/new.json
def new
@purchase = Purchase.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @purchase }
end
end
 
# GET /purchases/1/edit
def edit
@purchase = Purchase.find(params[:id])
end
 
# POST /purchases
# POST /purchases.json
def create
@purchase = Purchase.new(params[:purchase])
@settings = Setting.first
respond_to do |format|
if @purchase.save
cb_value = ((@purchase.shop.cashback.to_f*@settings.leader_ratio.to_f)/100.0)
if @purchase.shop.ratio == "%"
@purchase.cash = (((@purchase.total.to_f*cb_value.to_f)/100.0)).round(2).to_s
else# eg. "€"
@purchase.cash = cb_value
end
@purchase.save
format.html { redirect_to @purchase, notice: 'La transaction a été créée.' }
format.json { render json: @purchase, status: :created, location: @purchase }
else
format.html { render action: "new" }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
 
# PUT /purchases/1
# PUT /purchases/1.json
def update
@purchase = Purchase.find(params[:id])
@settings = Setting.first
respond_to do |format|
if @purchase.update_attributes(params[:purchase])
cb_value = ((@purchase.shop.cashback*@settings.leader_ratio)/100)
if @purchase.shop.ratio == "%"
@purchase.cash = ((@purchase.total.to_f*cb_value)/100).to_s
else# eg. "€"
@purchase.cash = cb_value
end
@purchase.save
format.html { redirect_to @purchase, notice: 'La transaction a été mise à jour.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @purchase.errors, status: :unprocessable_entity }
end
end
end
 
# DELETE /purchases/1
# DELETE /purchases/1.json
def destroy
@purchase = Purchase.find(params[:id])
@purchase.destroy
 
respond_to do |format|
format.html { redirect_to purchases_url }
format.json { head :ok }
end
end
end
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
class User < ActiveRecord::Base
authenticates_with_sorcery!
has_many :purchases
validates_confirmation_of :password, :on => :update
validates_presence_of :password, :on => :create
validates_presence_of :password_confirmation, :on => :create
validates_presence_of :username, :on => :create
validates_presence_of :firstname
validates_presence_of :lastname
#validates_presence_of :gender
# validates_presence_of :email
validates :email, :presence => true, :email => true
#other presences... FILL!!!
validates_uniqueness_of :username
validates_uniqueness_of :email
extend FriendlyId
friendly_id :username, use: :slugged
mount_uploader :avatar, AvatarUploader
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_avatar
def self.find_usernames (username)
users = []
query = User.select('slug').where "slug like ?", '%'+username+'%'
for u in query do
users << u[:slug]
end
users
end
def crop_avatar
avatar.recreate_versions! if crop_x.present?
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.