Class: Shoppy::LocationsController

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

Direct Known Subclasses

LocationsController

Instance Method Summary collapse

Instance Method Details

#deleteObject



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

def delete
  stock_location = StockLocation.find_by(id: params[:location_id])
  if stock_location
    if stock_location.check_inventory
        stock_location.destroy
        Log.newEvent("Stock Locations", "Stock Location named '#{stock_location.name}' was deleted", current_admin.name)
        flash[:notice] = "Stock Location has been deleted."
    else
      flash[:warning] = "You can't delete stock location that has inventory"
    end
    if params[:view] == "show"
      redirect_to '/locations/#{@location.id}'
    else
    redirect_to '/locations'
    end
  else
    page_not_found
  end
end

#editObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/shoppy/locations_controller.rb', line 39

def edit
  @stock_location = StockLocation.find_by(id: params[:location_id])
  if @stock_location
    if @stock_location.update_attributes(stock_location_params)
      Log.newEvent("Stock Locations", "Stock Location named '#{@stock_location.name}' was updated", current_admin.name)
      flash[:notice] = "Stock Location has been updated."
    else
      flash[:error] = @stock_location.errors.messages
    end
    redirect_to '/locations/' + @stock_location.id.to_s
  else
    page_not_found
  end
end

#indexObject



5
6
7
# File 'app/controllers/shoppy/locations_controller.rb', line 5

def index
  @stock_locations = StockLocation.all.order("name ASC")
end

#newObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/shoppy/locations_controller.rb', line 9

def new
  @stock_location = StockLocation.new
  @stock_location.name      = params[:name]
  @stock_location.phone     = params[:phone]
  @stock_location.email     = params[:email]
  @stock_location.add_line1 = params[:add_line1]
  @stock_location.add_line2 = params[:add_line2]
  @stock_location.city      = params[:city]
  @stock_location.state     = params[:state]
  @stock_location.zip       = params[:zip]
  if @stock_location.save
    Log.newEvent("Stock Locations", "A new stock Location named '#{@stock_location.name}' was created", current_admin.name)
    flash[:notice] = "Stock Location has been created."
  else
    ## TODO: Handle Errors
    flash[:error] = @stock_location.errors.messages
  end
  redirect_to '/locations'
end

#showObject



30
31
32
33
34
35
36
37
# File 'app/controllers/shoppy/locations_controller.rb', line 30

def show
  sl = StockLocation.find_by(id: params[:location_id])
  if sl
    @stock_location = sl
  else
    page_not_found
  end
end