From cbca8c19a5bdd48f7b9cb07b06a44f31c846dd55 Mon Sep 17 00:00:00 2001 From: Skud Date: Wed, 29 May 2013 14:37:42 +1000 Subject: [PATCH] added admin controller and index view --- app/assets/javascripts/admin/orders.js.coffee | 3 ++ app/controllers/admin/orders_controller.rb | 45 +++++++++++++++++++ app/helpers/admin/orders_helper.rb | 2 + app/views/admin/index.html.haml | 12 +++++ app/views/admin/orders/_searchform.html.haml | 5 +++ app/views/admin/orders/index.html.haml | 3 ++ app/views/admin/orders/search.html.haml | 36 +++++++++++++++ app/views/home/index.html.haml | 10 ----- app/views/layouts/_header.html.haml | 2 + app/views/orders/show.html.haml | 8 ++++ config/routes.rb | 2 + .../admin/orders_controller_spec.rb | 5 +++ spec/helpers/admin/orders_helper_spec.rb | 15 +++++++ spec/views/admin/index_spec.rb | 17 +++++++ spec/views/home/index_spec.rb | 10 ----- 15 files changed, 155 insertions(+), 20 deletions(-) create mode 100644 app/assets/javascripts/admin/orders.js.coffee create mode 100644 app/controllers/admin/orders_controller.rb create mode 100644 app/helpers/admin/orders_helper.rb create mode 100644 app/views/admin/orders/_searchform.html.haml create mode 100644 app/views/admin/orders/index.html.haml create mode 100644 app/views/admin/orders/search.html.haml create mode 100644 spec/controllers/admin/orders_controller_spec.rb create mode 100644 spec/helpers/admin/orders_helper_spec.rb create mode 100644 spec/views/admin/index_spec.rb diff --git a/app/assets/javascripts/admin/orders.js.coffee b/app/assets/javascripts/admin/orders.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/admin/orders.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/controllers/admin/orders_controller.rb b/app/controllers/admin/orders_controller.rb new file mode 100644 index 000000000..fb94cf1ae --- /dev/null +++ b/app/controllers/admin/orders_controller.rb @@ -0,0 +1,45 @@ +class Admin::OrdersController < ApplicationController + def index + authorize! :manage, :all + respond_to do |format| + format.html # index.html.haml + end + end + + def search + @orders = nil + + if params[:search_text] + case params[:search_by] + when "member" + begin + @member = Member.find(params[:search_text]) + @orders = @member.orders + rescue ActiveRecord::RecordNotFound + flash[:alert] = "Couldn't find member with name #{params[:search_text]}" + end + when "order_id" + begin + @orders = [ Order.find(params[:search_text]) ] + rescue ActiveRecord::RecordNotFound + flash[:alert] = "Couldn't find order with id #{params[:search_text]}" + end + when "paypal_token" + @orders = [ Order.find_by_paypal_express_token(params[:search_text]) ] + if @orders.nil? + flash[:alert] = "Couldn't find order with paypal token #{params[:search_text]}" + end + when "paypal_payer_id" + @orders = [ Order.find_by_paypal_express_payer_id(params[:search_text]) ] + if @orders.nil? + flash[:alert] = "Couldn't find order with paypal payer id #{params[:search_text]}" + end + end + end + + respond_to do |format| + format.html # index.html.haml + end + + end +end diff --git a/app/helpers/admin/orders_helper.rb b/app/helpers/admin/orders_helper.rb new file mode 100644 index 000000000..863374ff6 --- /dev/null +++ b/app/helpers/admin/orders_helper.rb @@ -0,0 +1,2 @@ +module Admin::OrdersHelper +end diff --git a/app/views/admin/index.html.haml b/app/views/admin/index.html.haml index ef7530bca..0ce432ccd 100644 --- a/app/views/admin/index.html.haml +++ b/app/views/admin/index.html.haml @@ -1 +1,13 @@ -content_for :title, 'Admin' + +%h2 Manage + +%ul + %li= link_to "Account types", account_types_path + %li= link_to "Products", products_path + %li= link_to "Roles", roles_path + %li= link_to "Forums", forums_path + +%h2 Orders + +=render "admin/orders/searchform" diff --git a/app/views/admin/orders/_searchform.html.haml b/app/views/admin/orders/_searchform.html.haml new file mode 100644 index 000000000..8cbb7a91b --- /dev/null +++ b/app/views/admin/orders/_searchform.html.haml @@ -0,0 +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' }) + = submit_tag "Search", :class => 'btn btn-primary' diff --git a/app/views/admin/orders/index.html.haml b/app/views/admin/orders/index.html.haml new file mode 100644 index 000000000..42626c777 --- /dev/null +++ b/app/views/admin/orders/index.html.haml @@ -0,0 +1,3 @@ +-content_for :title, 'Admin Orders' + +=render "admin/orders/searchform" diff --git a/app/views/admin/orders/search.html.haml b/app/views/admin/orders/search.html.haml new file mode 100644 index 000000000..b553bec5d --- /dev/null +++ b/app/views/admin/orders/search.html.haml @@ -0,0 +1,36 @@ +-content_for :title, 'Search Orders' + +=render "admin/orders/searchform" + +- if @orders + %h2 + Found + = pluralize(@orders.count, "result") + + %table.table.table-striped + %tr + %th Member + %th Order number + %th Date completed + %th Items + %th + + - @orders.each do |order| + %tr + %td= link_to order.member.login_name, order.member + %td= order.id + %td + - if order.completed_at + = order.completed_at.to_s + - else + In progress + %td + - if order.order_items.count > 0 + - order.order_items.each do |o| + = o.quantity + x + = o.product.name + @ + = price_with_currency(o.price) + %br/ + %td= link_to 'Details', order, :class => 'btn btn-mini' diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 8819e544e..a9329bb05 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -35,16 +35,6 @@ %br/ - if current_member == current_member && !current_member.is_paid? = link_to "Upgrade and Support Growstuff", shop_path, :class => 'btn btn-primary' - - if current_member.has_role?(:admin) - %p - %b You are an ADMIN USER. - - - if current_member.forums.count > 0 - %p - %b Forums you administer: - %ul - - current_member.forums.each do |f| - %li= link_to f.name, f .row .span6 diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index da6b91970..622244364 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -13,6 +13,8 @@ %li= link_to "Posts", posts_path %li= link_to "Forums", forums_path %li= link_to "Shop", shop_path + -if member_signed_in? and current_member.has_role?(:admin) + %li= link_to "Admin", admin_path %li.divider-vertical - if member_signed_in? %li.dropdown< diff --git a/app/views/orders/show.html.haml b/app/views/orders/show.html.haml index 514c78acc..8136c13ec 100644 --- a/app/views/orders/show.html.haml +++ b/app/views/orders/show.html.haml @@ -17,6 +17,14 @@ %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 + %h2 Order items %table.table.table-striped diff --git a/config/routes.rb b/config/routes.rb index aeea090df..f69f07355 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -99,6 +99,8 @@ Growstuff::Application.routes.draw do match '/shop' => 'shop#index' match '/shop/:action' => 'shop#:action' + match '/admin/orders' => 'admin/orders#index' + match '/admin/orders/:action' => 'admin/orders#:action' match '/admin' => 'admin#index' match '/admin/:action' => 'admin#:action' diff --git a/spec/controllers/admin/orders_controller_spec.rb b/spec/controllers/admin/orders_controller_spec.rb new file mode 100644 index 000000000..b987ca78b --- /dev/null +++ b/spec/controllers/admin/orders_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Admin::OrdersController do + +end diff --git a/spec/helpers/admin/orders_helper_spec.rb b/spec/helpers/admin/orders_helper_spec.rb new file mode 100644 index 000000000..a80e748a5 --- /dev/null +++ b/spec/helpers/admin/orders_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the Admin::OrdersHelper. For example: +# +# describe Admin::OrdersHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe Admin::OrdersHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/admin/index_spec.rb b/spec/views/admin/index_spec.rb new file mode 100644 index 000000000..d63f897ee --- /dev/null +++ b/spec/views/admin/index_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe 'admin/index.html.haml', :type => "view" do + before(:each) do + @member = FactoryGirl.create(:admin_member) + sign_in @member + controller.stub(:current_user) { @member } + render + end + + it "includes links to manage various things" do + assert_select "a", :href => account_types_path + assert_select "a", :href => products_path + assert_select "a", :href => roles_path + assert_select "a", :href => forums_path + end +end diff --git a/spec/views/home/index_spec.rb b/spec/views/home/index_spec.rb index 49f9e5cf1..585fc8d4f 100644 --- a/spec/views/home/index_spec.rb +++ b/spec/views/home/index_spec.rb @@ -46,8 +46,6 @@ describe 'home/index.html.haml', :type => "view" do @forum = FactoryGirl.create(:forum, :owner => @member) @post = FactoryGirl.create(:post, :author => @member) assign(:posts, [@post]) - @role = FactoryGirl.create(:admin) - @member.roles << @role render end @@ -82,13 +80,5 @@ describe 'home/index.html.haml', :type => "view" do rendered.should contain "Upgrade" end - it 'shows admin status' do - rendered.should contain "You are an ADMIN USER" - end - - it 'shows forum list' do - assert_select "a[href=#{url_for(@forum)}]", @forum.name - end - end end