Class: StockitController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

current

Methods included from PathHelper

#finance_group_transactions_path

Instance Method Details

#articles_searchObject

TODO: Fix this!!



84
85
86
87
# File 'app/controllers/stockit_controller.rb', line 84

def articles_search
  @articles = Article.not_in_stock.limit(8).where('name LIKE ?', "%#{params[:term]}%")
  render json: @articles.map(&:name)
end

#copyObject

(2) StockArticle as template



33
34
35
36
37
# File 'app/controllers/stockit_controller.rb', line 33

def copy
  @stock_article = StockArticle.find(params[:stock_article_id]).dup

  render layout: false
end

#createObject



52
53
54
55
56
57
58
# File 'app/controllers/stockit_controller.rb', line 52

def create
  @stock_article = StockArticle.new({ quantity: 0 }.merge(params[:stock_article]))
  @stock_article.save!
  render layout: false
rescue ActiveRecord::RecordInvalid
  render action: 'new', layout: false
end

#deriveObject

(3) non-stock Article as template



40
41
42
43
44
# File 'app/controllers/stockit_controller.rb', line 40

def derive
  @stock_article = Article.find(params[:old_article_id]).becomes(StockArticle).dup

  render layout: false
end

#destroyObject



74
75
76
77
78
79
80
81
# File 'app/controllers/stockit_controller.rb', line 74

def destroy
  @stock_article = StockArticle.find(params[:id])
  @stock_article.mark_as_deleted
  render layout: false
rescue StandardError => e
  render partial: 'destroy_fail', layout: false,
         locals: { fail_msg: I18n.t('errors.general_msg', msg: e.message) }
end

#editObject



46
47
48
49
50
# File 'app/controllers/stockit_controller.rb', line 46

def edit
  @stock_article = StockArticle.find(params[:id])

  render layout: false
end

#indexObject



2
3
4
5
# File 'app/controllers/stockit_controller.rb', line 2

def index
  @stock_articles = StockArticle.undeleted.includes(:supplier, :article_category)
                                .order('suppliers.name, article_categories.name, articles.name')
end

#index_on_stock_article_createObject

See publish/subscribe design pattern in /doc.



7
8
9
10
11
# File 'app/controllers/stockit_controller.rb', line 7

def index_on_stock_article_create # See publish/subscribe design pattern in /doc.
  @stock_article = StockArticle.find(params[:id])

  render layout: false
end

#index_on_stock_article_updateObject

See publish/subscribe design pattern in /doc.



13
14
15
16
17
# File 'app/controllers/stockit_controller.rb', line 13

def index_on_stock_article_update # See publish/subscribe design pattern in /doc.
  @stock_article = StockArticle.find(params[:id])

  render layout: false
end

#newObject

three possibilites to fill a new_stock_article form (1) start from blank or use params



26
27
28
29
30
# File 'app/controllers/stockit_controller.rb', line 26

def new
  @stock_article = StockArticle.new(params[:stock_article])

  render layout: false
end

#showObject



19
20
21
22
# File 'app/controllers/stockit_controller.rb', line 19

def show
  @stock_article = StockArticle.find(params[:id])
  @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC')
end

#show_on_stock_article_updateObject

See publish/subscribe design pattern in /doc.



68
69
70
71
72
# File 'app/controllers/stockit_controller.rb', line 68

def show_on_stock_article_update # See publish/subscribe design pattern in /doc.
  @stock_article = StockArticle.find(params[:id])

  render layout: false
end

#updateObject



60
61
62
63
64
65
66
# File 'app/controllers/stockit_controller.rb', line 60

def update
  @stock_article = StockArticle.find(params[:id])
  @stock_article.update!(params[:stock_article])
  render layout: false
rescue ActiveRecord::RecordInvalid
  render action: 'edit', layout: false
end