Class: Iro::StocksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/iro/stocks_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/iro/stocks_controller.rb', line 5

def create
  @stock = Iro::Stock.new(stock_params)
  authorize! :create, @stock

  if @stock.save
    flash_notice @stock
  else
    flash_alert @stock
  end
  redirect_to action: :index
end

#destroyObject



17
18
19
20
# File 'app/controllers/iro/stocks_controller.rb', line 17

def destroy
  @stock.destroy
  redirect_to stocks_url, notice: 'Stock was successfully destroyed.'
end

#editObject



22
23
# File 'app/controllers/iro/stocks_controller.rb', line 22

def edit
end

#indexObject



25
26
27
28
# File 'app/controllers/iro/stocks_controller.rb', line 25

def index
  @stocks = Iro::Stock.all
  authorize! :index, Iro::Stock
end

#newObject



30
31
32
33
# File 'app/controllers/iro/stocks_controller.rb', line 30

def new
  @stock = Iro::Stock.new
  authorize! :new, @stock
end

#showObject



46
47
# File 'app/controllers/iro/stocks_controller.rb', line 46

def show
end

#syncObject



35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/iro/stocks_controller.rb', line 35

def sync
  authorize! :refresh, Iro::Stock
  tickers = Iro::Stock.all.map { |s| s.ticker }.join(',')
  outs = Tda::Stock.get_quotes tickers
  outs.map do |out|
    Iro::Stock.where( ticker: out[:symbol] ).update( last: out[:last] )
  end
  flash_notice 'refreshed stocks'
  redirect_to request.referrer
end

#updateObject



49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/iro/stocks_controller.rb', line 49

def update
  @stock = Iro::Stock.find params[:id]
  authorize! :update, @stock
  if @stock.update(stock_params)
    flash_notice @stock
  else
    flash_alert @stock
  end
  redirect_to request.referrer
end