Class: StockitController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

current

Instance Method Details

#articles_searchObject

TODO: Fix this!!


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

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


29
30
31
32
33
# File 'app/controllers/stockit_controller.rb', line 29

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

  render :layout => false
end

#createObject


42
43
44
45
46
47
48
# File 'app/controllers/stockit_controller.rb', line 42

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


36
37
38
39
40
# File 'app/controllers/stockit_controller.rb', line 36

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

  render :layout => false
end

#destroyObject


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

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

#editObject


50
51
52
53
54
# File 'app/controllers/stockit_controller.rb', line 50

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

  render :layout => false
end

#fill_new_stock_article_formObject


90
91
92
93
94
95
96
97
98
# File 'app/controllers/stockit_controller.rb', line 90

def fill_new_stock_article_form
  article = Article.find(params[:article_id])
  @supplier = article.supplier
  stock_article = @supplier.stock_articles.build(
    article.attributes.reject { |attr| attr == ('id' || 'type')}
  )

  render :partial => 'form', :locals => {:stock_article => stock_article}
end

#indexObject


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

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.


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

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.


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

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


22
23
24
25
26
# File 'app/controllers/stockit_controller.rb', line 22

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

  render :layout => false
end

#showObject


64
65
66
67
# File 'app/controllers/stockit_controller.rb', line 64

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.


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

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

  render :layout => false
end

#updateObject


56
57
58
59
60
61
62
# File 'app/controllers/stockit_controller.rb', line 56

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