Skip to content

Instantly share code, notes, and snippets.

Created July 15, 2013 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/29ef897f922ad9878d4c to your computer and use it in GitHub Desktop.
Save anonymous/29ef897f922ad9878d4c to your computer and use it in GitHub Desktop.
#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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment