From 2d51bd1d9a77560ba1292ae8482f6a9d0f131c91 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sun, 20 Jan 2013 16:14:53 -0500 Subject: [PATCH] Creating Transactions works -Need to scope transactions for users -Only Admins should be able to create transactions --- app/components/app_tab_panel.rb | 2 +- app/components/customers.rb | 10 ++++++++ app/components/transactions.rb | 24 +++++++++---------- .../javascripts/init_component.js | 4 +++- app/models/customer.rb | 12 +++++++++- app/models/transaction.rb | 2 +- .../20121205043759_create_transactions.rb | 2 +- db/schema.rb | 4 ++-- 8 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 app/components/customers.rb diff --git a/app/components/app_tab_panel.rb b/app/components/app_tab_panel.rb index d881e54..dddcc10 100644 --- a/app/components/app_tab_panel.rb +++ b/app/components/app_tab_panel.rb @@ -10,7 +10,7 @@ class AppTabPanel < Netzke::Basepack::TabPanel #all users # (had to use hash for borders to get the title to display properly) - @@app_tab_panel_items = [ :transactions, :bikes_border, {layout: :fit, wrappedComponent: :brands_and_models_border, title: "Brands/Models"}] + @@app_tab_panel_items = [ :transactions_border, :bikes_border, {layout: :fit, wrappedComponent: :brands_and_models_border, title: "Brands/Models"}] #for users if controller.current_user.user? diff --git a/app/components/customers.rb b/app/components/customers.rb new file mode 100644 index 0000000..d0e8a2c --- /dev/null +++ b/app/components/customers.rb @@ -0,0 +1,10 @@ +class Customers < Netzke::Basepack::Grid + def configure(c) + c.model = "Customer" + end + + #override with nil to remove actions + def default_bbar + [ :apply, :add_in_form, :search ] + end +end diff --git a/app/components/transactions.rb b/app/components/transactions.rb index cad6825..dcfcc27 100644 --- a/app/components/transactions.rb +++ b/app/components/transactions.rb @@ -4,8 +4,8 @@ class Transactions < Netzke::Basepack::Grid c.model = "Transaction" c.strong_default_attrs = { :vendor_id => controller.current_user.id, - :customer_id => session[:selected_user_id], - :customer_type => session[:selected_type] + :customer_id => session[:selected_customer_id], + :customer_type => session[:selected_customer_type] } c.columns = [ :amount, @@ -23,7 +23,13 @@ class Transactions < Netzke::Basepack::Grid def default_fields_for_forms bike_store = Bike.all.map { |b| [b.id, b.serial_number] } user_store = User.all.map { |u| [u.id, u.to_s] } - customer = User.find_by_id(session[:selected_user_id]) + customer = nil + if session[:selected_customer_type] == "User" + customer = User.find_by_id(session[:selected_customer_id]) + elsif session[:selected_customer_type] == "Customer" + customer = Customer.find_by_id(session[:selected_customer_id]) + end + customer = "No User Selected" if customer.nil? [ { :no_binding => true, :xtype => 'label', :text => "Creating Transaction for: #{customer.to_s}"}, @@ -37,14 +43,8 @@ class Transactions < Netzke::Basepack::Grid ] end - js_configure do |c| - c.mixin :init_component - end - - endpoint :select_user do |params, this| - # store selected boss id in the session for this component's instance - session[:selected_user_id] = params[:user_id] - session[:selected_type] = 'User' + #override with nil to remove actions + def default_bbar + [ :apply, :add_in_form, :search ] end - end diff --git a/app/components/transactions_border/javascripts/init_component.js b/app/components/transactions_border/javascripts/init_component.js index 7f2cd2d..da0c444 100644 --- a/app/components/transactions_border/javascripts/init_component.js +++ b/app/components/transactions_border/javascripts/init_component.js @@ -5,12 +5,14 @@ // setting the 'rowclick' event var user_view = this.getComponent('users').getView(); - var customer_view = this.getComponent('users').getView(); + var customer_view = this.getComponent('customers').getView(); user_view.on('itemclick', function(view, record){ // The beauty of using Ext.Direct: calling 3 endpoints in a row, which results in a single call to the server! + console.log("user: " + record.get('id') ); this.selectCustomer({customer_id: record.get('id'), customer_type: 'User'}); }, this); customer_view.on('itemclick', function(view, record){ + console.log("user: " + record.get('id') ); this.selectCustomer({customer_id: record.get('id'), customer_type: 'Customer'}); }, this); } diff --git a/app/models/customer.rb b/app/models/customer.rb index bc47613..83c1c99 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -2,7 +2,17 @@ class Customer < ActiveRecord::Base attr_accessible :first_name, :last_name, :addrStreet1, :addrStreet2, :addrCity, :addrState, :addrZip, :phone, :email - has_many :transactions + has_many :transactions, :as => :customer + + validates :first_name, :presence => true + validates :last_name, :presence => true + #validates :addrStreet1, :presence => true + #validates :addrStreet2, :presence => true + #validates :addrCity, :presence => true + #validates :addrState, :presence => true + #validates :addrZip, :presence => true + #validates :phone, :presence => true + #validates :email, :presence => true def to_s "#{first_name} #{last_name}" diff --git a/app/models/transaction.rb b/app/models/transaction.rb index bdebc68..66e93f6 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -5,7 +5,7 @@ class Transaction < ActiveRecord::Base belongs_to :vendor, :class_name => 'User', :foreign_key => 'vendor_id' belongs_to :bike - belongs_to :customer + belongs_to :customer, :polymorphic => true validates :vendor_id, :presence => true validates :customer_id, :presence => { :message => "Choose a User or Customer"} diff --git a/db/migrate/20121205043759_create_transactions.rb b/db/migrate/20121205043759_create_transactions.rb index bb325fa..bba51db 100644 --- a/db/migrate/20121205043759_create_transactions.rb +++ b/db/migrate/20121205043759_create_transactions.rb @@ -3,7 +3,7 @@ class CreateTransactions < ActiveRecord::Migration create_table :transactions do |t| t.integer "vendor_id", :null => false t.integer "customer_id", :null => false - t.integer "customer_type", :null => false + t.string "customer_type", :null => false t.integer "bike_id" t.integer "amount", :null => false t.string "item", :null => false diff --git a/db/schema.rb b/db/schema.rb index 244ac23..f76382c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -104,8 +104,8 @@ ActiveRecord::Schema.define(:version => 20130120142249) do create_table "transactions", :force => true do |t| t.integer "vendor_id", :null => false - t.integer "customer_id" - t.integer "customer_type" + t.integer "customer_id", :null => false + t.string "customer_type", :null => false t.integer "bike_id" t.integer "amount", :null => false t.string "item", :null => false