Class: Shoppy::CustomerAddressesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/shoppy/customer_addresses_controller.rb

Direct Known Subclasses

CustomerAddressesController

Instance Method Summary collapse

Instance Method Details

#deleteObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/shoppy/customer_addresses_controller.rb', line 72

def delete
  customer = Customer.find_by(id: params[:customer_id])
  address  = CustomerAddress.find_by(id: params[:address_id])
  if customer && address && address.customer == customer
    address.destroy
    Log.newEvent("Customr Addresses", "An address for customer '#{@customer.name}' was deleted", current_admin.name)
    flash[:notice] = "Address has been deleted."
    redirect_to "/accounts/"+ @customer.id + "/addresses"
  else
    page_not_found
  end
end

#editObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/shoppy/customer_addresses_controller.rb', line 54

def edit
  customer = Customer.find_by(id: params[:customer_id])
  address  = CustomerAddress.find_by(id: params[:address_id])
  if customer && address && address.customer == customer
    @customer = customer
    @address  = address
    if @address.update_attributes(address_params)
      Log.newEvent("Customr Addresses", "An address for customer '#{@customer.name}' was updated", current_admin.name)
      flash[:notice] = "Address updated successfully."
    else
      flash[:error] = @address.errors.messages
    end
    redirect_to "/accounts/" + @customer.id.to_s + "/addresses/" + address.id.to_s
  else
    page_not_found
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/shoppy/customer_addresses_controller.rb', line 5

def index
  @customer = Customer.find_by(id: params[:customer_id])
  if @customer
    @addresses = @customer.customer_addresses
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @addresses.map { |address| address.as_json(:only => :id, :methods => [:name, :add_line1, :add_line2, :city, :state, :country, :zip, :customer_id]) } }
      format.json { render :json => @addresses.map { |address| address.as_json(:only => :id, :methods => [:name, :add_line1, :add_line2, :city, :state, :country, :zip, :customer_id]) } }
    end
  else
    redirect_to "404.html"
  end
end

#newObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/shoppy/customer_addresses_controller.rb', line 19

def new
  @customer = Customer.find_by(id: params[:customer_id])
  if @customer
    @address = CustomerAddress.new
    @address.customer   = @customer
    @address.name     = params[:name]
    @address.add_line1  = params[:add_line1]
    @address.add_line2  = params[:add_line2]
    @address.city       = params[:city]
    @address.state      = params[:state]
    @address.country    = params[:country]
    @address.zip        = params[:zip]
    if @address.save
      Log.newEvent("Customr Addresses", "A new address for customer '#{@customer.name}' was created", current_admin.name)
      flash[:notice]    = "Address has been created successfully."
    else
      flash[:error]    = @address.errors.messages
    end
    redirect_to "/accounts/" + @customer.id.to_s + "/addresses"
  else
    page_not_found
  end
end

#showObject



43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/shoppy/customer_addresses_controller.rb', line 43

def show
  customer = Customer.find_by(id: params[:customer_id])
  address  = CustomerAddress.find_by(id: params[:address_id])
  if customer && address && address.customer == customer
    @customer = customer
    @address  = address
  else
    page_not_found
  end
end