Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request Growstuff#321 from Growstuff/dev
Browse files Browse the repository at this point in the history
Production push: order referral codes and some CSS fixes
  • Loading branch information
Skud committed Sep 19, 2013
2 parents 0650053 + 9af9d2a commit 78ee229
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 87 deletions.
14 changes: 9 additions & 5 deletions app/assets/stylesheets/bootstrap_and_overrides.css.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
@import "twitter/bootstrap/bootstrap";

// this padding needs to be done before the responsive stuff is imported
body {
padding-top: @navbarHeight + 10px;
padding-bottom: @navbarHeight + 10px;
}

@import "twitter/bootstrap/responsive";

// Set the correct sprite paths
Expand Down Expand Up @@ -77,10 +84,7 @@
@dropdownLinkColorHover: @brown;
@dropdownLinkBackgroundHover: lighten(@green, 50%);

body {
padding-top: @navbarHeight + 10px;
padding-bottom: @navbarHeight + 10px;
}

ul.inline > li.first {
padding-left: 0px;
}
Expand Down Expand Up @@ -165,6 +169,6 @@ p.stats {
}
}

li {
li.crop-hierarchy {
list-style-type: disc;
}
30 changes: 3 additions & 27 deletions app/controllers/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,10 @@ def index

def search
authorize! :manage, :all
@orders = []
@orders = Order.search({:by => params[:search_by], :for => params[:search_text]})

if params[:search_text]
case params[:search_by]
when "member"
member = Member.find_by_login_name(params[:search_text])
if member
@orders = member.orders
end
when "order_id"
order = Order.find_by_id(params[:search_text])
if order
@orders = [order]
end
when "paypal_token"
order = Order.find_by_paypal_express_token(params[:search_text])
if order
@orders = [order]
end
when "paypal_payer_id"
order = Order.find_by_paypal_express_payer_id(params[:search_text])
if order
@orders = [order]
end
end
if @orders.empty?
flash[:alert] = "Couldn't find order with #{params[:search_by]} = #{params[:search_text]}"
end
if @orders.empty?
flash[:alert] = "Couldn't find order with #{params[:search_by]} = #{params[:search_text]}"
end

respond_to do |format|
Expand Down
28 changes: 16 additions & 12 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,28 @@ def new
def checkout
@order = Order.find(params[:id])

response = EXPRESS_GATEWAY.setup_purchase(
@order.total,
:items => @order.activemerchant_items,
:currency => Growstuff::Application.config.currency,
:no_shipping => true,
:ip => request.remote_ip,
:return_url => complete_order_url,
:cancel_return_url => shop_url
)
redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
respond_to do |format|
if @order.update_attributes(:referral_code => params[:referral_code])
response = EXPRESS_GATEWAY.setup_purchase(
@order.total,
:items => @order.activemerchant_items,
:currency => Growstuff::Application.config.currency,
:no_shipping => true,
:ip => request.remote_ip,
:return_url => complete_order_url,
:cancel_return_url => shop_url
)
format.html { redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token) }
else
format.html { render action: "show" }
end
end

end

def complete
@order = Order.find(params[:id])

@order.save

if (params[:token] && params['PayerID'])
purchase = EXPRESS_GATEWAY.purchase(
@order.total,
Expand Down
51 changes: 50 additions & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
class Order < ActiveRecord::Base
attr_accessible :member_id, :completed_at
attr_accessible :member_id, :completed_at, :referral_code
belongs_to :member

has_many :order_items, :dependent => :destroy

default_scope order('created_at DESC')

validates :referral_code, :format => {
:with => /\A[a-zA-Z0-9 ]*\z/,
:message => "may only include letters and numbers"
}

before_save :standardize_referral_code

# total price of an order
def total
sum = 0
Expand Down Expand Up @@ -45,4 +52,46 @@ def update_account
end
end

# removes whitespace and forces to uppercase (we're somewhat liberal
# in what we accept, but we clean it up anyway.)
def standardize_referral_code
if referral_code
self.referral_code = referral_code.upcase.gsub /\s/, ''
end
end

# search orders (used by admin/orders)
# usage: Order.search({ :by => 'member', :for => 'Skud' })
# can search by: member, order_id, paypal_token, paypal_payer_id,
def Order.search(args={})
if args[:for]
case args[:by]
when "member"
member = Member.find_by_login_name(args[:for])
if member
return member.orders
end
when "order_id"
order = Order.find_by_id(args[:for])
if order
return [order]
end
when "paypal_token"
order = Order.find_by_paypal_express_token(args[:for])
if order
return [order]
end
when "paypal_payer_id"
order = Order.find_by_paypal_express_payer_id(args[:for])
if order
return [order]
end
when "referral_code"
# coerce to uppercase
return Order.where(:referral_code => args[:for].upcase)
end
end
return []
end

end
2 changes: 1 addition & 1 deletion app/views/admin/orders/_searchform.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
= form_tag(url_for(:controller => 'admin/orders', :action => 'search'), :method => :get, :class => 'form-inline') do
= label_tag :distance, "Search orders:", :class => 'control-label'
= text_field_tag :search_text
= select_tag :search_by, options_for_select({'Member' => 'member', 'Order ID' => 'order_id', 'Paypal Token' => 'paypal_token', 'Paypal Payer ID' => 'paypal_payer_id' })
= select_tag :search_by, options_for_select({'Member' => 'member', 'Referral code' => 'referral_code', 'Order ID' => 'order_id', 'Paypal Token' => 'paypal_token', 'Paypal Payer ID' => 'paypal_payer_id' })
= submit_tag "Search", :class => 'btn btn-primary'
3 changes: 3 additions & 0 deletions app/views/admin/orders/search.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
%th Member
%th Order number
%th Date completed
%th Referral code
%th Items
%th

Expand All @@ -24,6 +25,8 @@
= order.completed_at.to_s
- else
In progress
%td
= order.referral_code
%td
- if order.order_items.count > 0
- order.order_items.each do |o|
Expand Down
2 changes: 1 addition & 1 deletion app/views/crops/_hierarchy.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%ul
- display_crops.each do |c|
%li
%li.crop-hierarchy
= link_to c, c
- if c.varieties.present?
- c.varieties.each do |v|
Expand Down
23 changes: 11 additions & 12 deletions app/views/layouts/_footer.html.haml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
.container
.navbar.navbar-fixed-bottom
.navbar-inner
.container
%ul.nav
%li= link_to "About", "http://wiki.growstuff.org/index.php/About%20Growstuff"
%li= link_to "Contact", url_for(:controller => '/about', :action => 'contact')
%li= link_to "Terms of Service", url_for(:controller => '/policy', :action => 'tos')
%li= link_to "Privacy Policy", url_for(:controller => %'/policy', :action => 'privacy')
%li= link_to "Community Guidelines", url_for(:controller => '/policy', :action => 'community')
%li= link_to "Support/FAQ", url_for(:controller => '/support')
%li= link_to "Open Source", "https://github.com/Growstuff/growstuff"
.navbar.navbar-fixed-bottom
.navbar-inner
.container
%ul.nav
%li= link_to "About", "http://wiki.growstuff.org/index.php/About%20Growstuff"
%li= link_to "Contact", url_for(:controller => '/about', :action => 'contact')
%li= link_to "Terms of Service", url_for(:controller => '/policy', :action => 'tos')
%li= link_to "Privacy Policy", url_for(:controller => %'/policy', :action => 'privacy')
%li= link_to "Community Guidelines", url_for(:controller => '/policy', :action => 'community')
%li= link_to "Support/FAQ", url_for(:controller => '/support')
%li= link_to "Open Source", "https://github.com/Growstuff/growstuff"
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
= alert
= yield

%footer
= render :partial => "layouts/footer"
%footer
= render :partial => "layouts/footer"
/
Javascripts
\==================================================
Expand Down
49 changes: 33 additions & 16 deletions app/views/orders/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@
%strong Date begun:
= @order.created_at.to_s

%p
- if @order.completed_at
- if @order.completed_at
%p
%strong Date completed:
= @order.completed_at.to_s

- if current_member.has_role? :admin
%p
%strong Paypal Express token:
= @order.paypal_express_token
%p
%strong Paypal Express payer ID:
= @order.paypal_express_payer_id
- if @order.referral_code
%p
%strong Referral code:
= @order.referral_code

- if current_member.has_role? :admin
%p
%strong Paypal Express token:
= @order.paypal_express_token
%p
%strong Paypal Express payer ID:
= @order.paypal_express_payer_id

%h2 Order items

Expand Down Expand Up @@ -53,12 +58,24 @@
= price_with_currency(@order.total)
= forex_link(@order.total)

%p
- if can? :destroy, @order
= link_to 'Delete this order', @order, method: :delete, |
data: { confirm: 'Are you sure?' }, :class => 'btn'
- if can? :complete, @order
= link_to 'Checkout with PayPal', checkout_order_path(@order), :class => 'btn btn-primary'
- if @order.errors.any?
.alert
#error_explanation
%h3= "#{pluralize(@order.errors.count, "error")} stopped you from checking out:"
%ul
- @order.errors.full_messages.each do |msg|
%li= msg

- if can? :complete, @order or can? :destroy, @order
= form_tag(checkout_order_path(@order), :method => :get, :class => 'form-inline') do
%p
- if can? :complete, @order
= label_tag :referral_code, "Do you have a referral code?"
= text_field_tag :referral_code, @order.referral_code, :class => 'input-medium'
= submit_tag "Checkout with PayPal", :class => 'btn btn-primary'
- if can? :destroy, @order
= link_to 'Delete this order', @order, method: :delete, |
data: { confirm: 'Are you sure?' }, :class => 'btn'
= link_to "View other orders/order history", orders_path, :class => 'btn'

%p
= link_to "View other orders/order history", orders_path
5 changes: 5 additions & 0 deletions db/migrate/20130913015118_add_referral_code_to_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddReferralCodeToOrder < ActiveRecord::Migration
def change
add_column :orders, :referral_code, :string
end
end
11 changes: 2 additions & 9 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130827105823) do
ActiveRecord::Schema.define(:version => 20130913015118) do

create_table "account_types", :force => true do |t|
t.string "name", :null => false
Expand Down Expand Up @@ -158,21 +158,14 @@
t.integer "member_id"
t.string "paypal_express_token"
t.string "paypal_express_payer_id"
t.string "referral_code"
end

create_table "orders_products", :id => false, :force => true do |t|
t.integer "order_id"
t.integer "product_id"
end

create_table "payments", :force => true do |t|
t.integer "payer_id"
t.string "payment_type"
t.decimal "amount"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "photos", :force => true do |t|
t.integer "owner_id", :null => false
t.string "thumbnail_url", :null => false
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,20 @@

describe Admin::OrdersController do

login_member(:admin_member)

describe "GET search" do
it "assigns @orders" do
order = FactoryGirl.create(:order)
get :search, {:search_by => 'order_id', :search_text => order.id}
assigns(:orders).should eq([order])
end

it "sets an error message if nothing found" do
order = FactoryGirl.create(:order)
get :search, {:search_by => 'order_id', :search_text => 'foo'}
flash[:alert].should match /Couldn't find order with/
end
end

end
9 changes: 9 additions & 0 deletions spec/controllers/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def valid_session
end

describe "GET checkout" do
it 'sets the referral_code' do
member = FactoryGirl.create(:member)
sign_in member
order = Order.create!(:member_id => member.id)
get :checkout, {:id => order.to_param, :referral_code => 'FOOBAR'}
order.reload
order.referral_code.should eq 'FOOBAR'
end

it "redirects to Paypal" do
member = FactoryGirl.create(:member)
sign_in member
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/orders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
factory :completed_order do
completed_at '2013-05-08 01:01:01'
end

factory :referred_order do
referral_code 'CAMPAIGN1'
end
end
end
Loading

0 comments on commit 78ee229

Please sign in to comment.