Browse Source

Nested isn't working, another approach, add trans validations

User a transactions border, select the customer from a view of users and customers
denney-disable-on-select
Jason Denney 12 years ago
parent
commit
1b7daa45ca
  1. 55
      app/components/transactions.rb
  2. 30
      app/components/transactions_border.rb
  3. 17
      app/components/transactions_border/javascripts/init_component.js
  4. 17
      app/models/transaction.rb
  5. 4
      db/migrate/20121205043759_create_transactions.rb

55
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

30
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

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

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

4
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

Loading…
Cancel
Save