Browse Source

Creating Transactions works

-Need to scope transactions for users
-Only Admins should be able to create transactions
denney-disable-on-select
Jason Denney 11 years ago
parent
commit
2d51bd1d9a
  1. 2
      app/components/app_tab_panel.rb
  2. 10
      app/components/customers.rb
  3. 24
      app/components/transactions.rb
  4. 4
      app/components/transactions_border/javascripts/init_component.js
  5. 12
      app/models/customer.rb
  6. 2
      app/models/transaction.rb
  7. 2
      db/migrate/20121205043759_create_transactions.rb
  8. 4
      db/schema.rb

2
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?

10
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

24
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

4
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);
}

12
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}"

2
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"}

2
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

4
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

Loading…
Cancel
Save