Module: SpreeWholesale::WholesalerController::ClassMethods

Defined in:
lib/spree_wholesale/wholesaler_controller.rb

Instance Method Summary collapse

Instance Method Details

#after_wholesaler_createObject

Overwrite point



24
25
26
27
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 24

def after_wholesaler_create
  flash[:notice] = "Thank you for your interest in becoming a wholesaler! We'll be in touch shortly."
  redirect_to wholesalers_path
end

#after_wholesaler_failed_createObject



29
30
31
32
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 29

def after_wholesaler_failed_create
  flash[:error] = "Wholesale account could not be created!"
  render :action => :new
end

#createObject



57
58
59
60
61
62
63
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 57

def create
  if validate_wholesaler_parts && @wholesaler.valid? && @wholesaler.save
    return after_wholesaler_create
  else
    return after_wholesaler_failed_create
  end
end

#new_wholesale_userObject



40
41
42
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 40

def new_wholesale_user
  @user = User.new
end

#setup_defaultsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 77

def setup_defaults
  
  params[:wholesaler] ||= {}
  
  if request.get? && params[:dev]
    params[:wholesaler] = {"company"=>"Test Company", "buyer_contact"=>"Mr Contacter", "manager_contact"=>"Mr Manager", "phone"=>"555-555-5555", "fax"=>"555-555-5555 ext 1", "resale_number"=>"13414214", "taxid"=>"555-55-5555", "web_address"=>"testcompany.com", "terms"=>"Credit Card", "notes"=>""}
    params[:user] = {"email"=>"wholesale-#{rand(100)}@example.com", :password => "password" , :password_confirmation => "password" }
    params[:bill_address] = {"firstname"=>"Mister","lastname"=>"Accountant","address1"=>"123 Anystreet", "address2"=>"", "city"=>"Anytown", "state_id"=>"276110813", "zipcode"=>"98765", "country_id"=>"214", "phone"=>"555-555-5555"}
  end

  @roles = Role.all

  case params[:action]
    when 'new', 'create'
      @wholesaler = Wholesaler.new(params[:wholesaler])    
      @user = User.new(params[:user])
      @bill_address = Address.new((params[:bill_address] || {}).merge(:country => default_country))  
      
      if use_billing?
        @ship_address = @bill_address
      else
        @ship_address = Address.new((params[:ship_address] || {}).merge(:country => default_country))  
      end      
      
    when 'edit', 'update', 'destroy'
      @wholesaler = Wholesaler.find(params[:id])
      @user = @wholesaler.user
      @bill_address = @wholesaler.bill_address
      @ship_address = @wholesaler.ship_address
  end
      
  if params[:action] == 'update'
    if !use_billing? && @bill_address.id == @ship_address.id
      @ship_address = Address.new((params[:ship_address] || {}).merge(:country => default_country))  
    elsif use_billing? && @bill_address.id != @ship_address.id
      @wholesaler.ship_address.destroy
      @ship_address = @bill_address          
    end
  end
  
end

#updateObject



65
66
67
68
69
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 65

def update
  validate_wholesaler_parts
  @wholesaler.save
  super
end

#use_billing?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 73

def use_billing?
  @use_billing ||= params[:wholesaler].delete(:use_billing).to_i == 1
end

#validate_wholesaler_partsObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 44

def validate_wholesaler_parts
  uv = @user.valid?
  bv = @bill_address.valid?
  sv = @ship_address.valid?
  valid = uv && bv && sv
  if valid
    @wholesaler.user = @user if @user.save
    @wholesaler.bill_address = @bill_address if @bill_address.save
    @wholesaler.ship_address = @ship_address if @ship_address.save
  end
  @wholesaler.valid? && valid
end

#wholesale_roleObject



36
37
38
# File 'lib/spree_wholesale/wholesaler_controller.rb', line 36

def wholesale_role
  @wholesale_role ||= Role.find_or_create_by_name("wholesaler")
end