diff --git a/app/components/transactions.rb b/app/components/transactions.rb index f202646..cad6825 100644 --- a/app/components/transactions.rb +++ b/app/components/transactions.rb @@ -2,7 +2,11 @@ class Transactions < Netzke::Basepack::Grid def configure(c) super c.model = "Transaction" - c.strong_default_attrs = { :vendor_id => controller.current_user.id } + c.strong_default_attrs = { + :vendor_id => controller.current_user.id, + :customer_id => session[:selected_user_id], + :customer_type => session[:selected_type] + } c.columns = [ :amount, :item, @@ -19,49 +23,28 @@ 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 = "No User Selected" if customer.nil? [ + { :no_binding => true, :xtype => 'label', :text => "Creating Transaction for: #{customer.to_s}"}, :amount, :item, { :name => :for_bike, :checkboxName => :bike_item, :inputValue => true, :title => "Selling a bike?", :xtype => 'fieldset', :checkboxToggle => true, :collapsed => true, :items => [ {:xtype => 'combo', :no_binding => true, :name => :bike_id, :title => 'Bike', :fieldLabel => 'Bike', :store => bike_store} ] - }, - { - xtype: 'fieldcontainer', - fieldLabel: 'Customer Type', - defaultType: 'radiofield', - defaults: { - flex: 1 - }, - layout: 'hbox', - items: [ - { - no_binding: true, - boxLabel: 'Customer', - name: 'customer_type', - inputValue: 'Customer', - id: 'customer_radio' - }, - { - no_binding: true, - boxLabel: 'User', - name: 'customer_type', - inputValue: 'User', - id: 'user_radio' - } - ] - }, - { :name => :for_user, :checkboxName => :customer_type, :inputValue => true, :title => "Customer a User?", - :xtype => 'fieldset', :collapsible => true, :collapsed => true, :items => [ - {:xtype => 'combo', :no_binding => true, :name => :customer_id, :title => 'User', :fieldLabel => 'User', :store => user_store} - ] - }, - { :name => :for_customer, :checkboxName => :customer_type, :inputValue => true, :title => "New Customer?", - :xtype => 'fieldset', :collapsible => true, :collapsed => true, :items => [ - { :xtype => 'textfield', :no_binding => true, :name => 'customer[first_name]'}, - ] } ] 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' + end + end diff --git a/app/components/transactions_border.rb b/app/components/transactions_border.rb new file mode 100644 index 0000000..c347975 --- /dev/null +++ b/app/components/transactions_border.rb @@ -0,0 +1,30 @@ +class TransactionsBorder < Netzke::Base + # Remember regions collapse state and size + include Netzke::Basepack::ItemPersistence + #users and customers components are required for the transactions form + component :transactions + component :users + component :customers + + def configure(c) + super + c.header = false + c.items = [ + { netzke_component: :transactions, region: :west, width: 300, split: true }, + { netzke_component: :users, region: :center, width: 300, split: true }, + { netzke_component: :customers, region: :east, width: 300, split: true } + ] + end + + js_configure do |c| + c.layout = :border + c.border = false + c.mixin :init_component + end + + endpoint :select_customer do |params, this| + session[:selected_customer_id] = params[:customer_id] + session[:selected_customer_type] = params[:customer_type] + end + +end diff --git a/app/components/transactions_border/javascripts/init_component.js b/app/components/transactions_border/javascripts/init_component.js new file mode 100644 index 0000000..7f2cd2d --- /dev/null +++ b/app/components/transactions_border/javascripts/init_component.js @@ -0,0 +1,17 @@ +{ + initComponent: function(){ + // calling superclass's initComponent + this.callParent(); + + // setting the 'rowclick' event + var user_view = this.getComponent('users').getView(); + var customer_view = this.getComponent('users').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! + this.selectCustomer({customer_id: record.get('id'), customer_type: 'User'}); + }, this); + customer_view.on('itemclick', function(view, record){ + this.selectCustomer({customer_id: record.get('id'), customer_type: 'Customer'}); + }, this); + } +} diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 0afc26a..bdebc68 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -1,22 +1,17 @@ class Transaction < ActiveRecord::Base acts_as_loggable - attr_accessible :vendor_id, :customer_id, :customer_type, :bike_id, :amount, :item, - :customer_attributes + attr_accessible :vendor_id, :customer_id, :customer_type, :bike_id, :amount, :item belongs_to :vendor, :class_name => 'User', :foreign_key => 'vendor_id' belongs_to :bike belongs_to :customer - accepts_nested_attributes_for :customer, allow_destroy: false - - before_save :check_customer_type - - def check_customer_type - puts "_________------------_________------------_________------------" - puts self.inspect - puts "_________------------_________------------_________------------" - end + validates :vendor_id, :presence => true + validates :customer_id, :presence => { :message => "Choose a User or Customer"} + validates :customer_type, :presence => { :message => "Choose a User or Customer"} + validates :amount, :presence => true + validates :item, :presence => true def to_s "#{amount} #{item} #{bike_id}" diff --git a/db/migrate/20121205043759_create_transactions.rb b/db/migrate/20121205043759_create_transactions.rb index 827e7a8..bb325fa 100644 --- a/db/migrate/20121205043759_create_transactions.rb +++ b/db/migrate/20121205043759_create_transactions.rb @@ -2,8 +2,8 @@ class CreateTransactions < ActiveRecord::Migration def change create_table :transactions do |t| t.integer "vendor_id", :null => false - t.integer "customer_id" - t.integer "customer_type" + t.integer "customer_id", :null => false + t.integer "customer_type", :null => false t.integer "bike_id" t.integer "amount", :null => false t.string "item", :null => false