Class: ArticlesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

current

Methods included from PathHelper

#finance_group_transactions_path

Instance Method Details

#copyObject


45
46
47
48
# File 'app/controllers/articles_controller.rb', line 45

def copy
  @article = @supplier.articles.find(params[:article_id]).dup
  render :layout => false
end

#createObject


50
51
52
53
54
55
56
57
# File 'app/controllers/articles_controller.rb', line 50

def create
  @article = Article.new(params[:article])
  if @article.valid? && @article.save
    render :layout => false
  else
    render :action => 'new', :layout => false
  end
end

#destroyObject

Deletes article from database. send error msg, if article is used in a current order


76
77
78
79
80
# File 'app/controllers/articles_controller.rb', line 76

def destroy
  @article = Article.find(params[:id])
  @article.mark_as_deleted unless @order = @article.in_open_order # If article is in an active Order, the Order will be returned
  render :layout => false
end

#editObject


59
60
61
62
# File 'app/controllers/articles_controller.rb', line 59

def edit
  @article = Article.find(params[:id])
  render :action => 'new', :layout => false
end

#edit_allObject

Renders a form for editing all articles from a supplier


83
84
85
# File 'app/controllers/articles_controller.rb', line 83

def edit_all
  @articles = @supplier.articles.undeleted
end

#importObject

fills a form whith values of the selected shared_article when the direct parameter is set and the article is valid, it is imported directly


227
228
229
230
231
232
233
234
235
# File 'app/controllers/articles_controller.rb', line 227

def import
  @article = SharedArticle.find(params[:shared_article_id]).build_new_article(@supplier)
  @article. = params[:article_category_id] unless params[:article_category_id].blank?
  if params[:direct] && !params[:article_category_id].blank? && @article.valid? && @article.save
    render :action => 'create', :layout => false
  else
    render :action => 'new', :layout => false
  end
end

#indexObject


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/articles_controller.rb', line 5

def index
  if params['sort']
    sort = case params['sort']
             when "name"  then "articles.name"
             when "unit"   then "articles.unit"
             when "article_category" then "article_categories.name"
             when "note"   then "articles.note"
             when "availability" then "articles.availability"
             when "name_reverse"  then "articles.name DESC"
             when "unit_reverse"   then "articles.unit DESC"
             when "article_category_reverse" then "article_categories.name DESC"
             when "note_reverse" then "articles.note DESC"
             when "availability_reverse" then "articles.availability DESC"
             end
  else
    sort = "article_categories.name, articles.name"
  end

  @articles = Article.undeleted.where(supplier_id: @supplier, :type => nil).includes(:article_category).order(sort)

  if request.format.csv?
    send_data ArticlesCsv.new(@articles, encoding: 'utf-8').to_csv, filename: 'articles.csv', type: 'text/csv'
    return
  end

  @articles = @articles.where('articles.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?

  @articles = @articles.page(params[:page]).per(@per_page)

  respond_to do |format|
    format.html
    format.js { render :layout => false }
  end
end

#newObject


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

def new
  @article = @supplier.articles.build(:tax => FoodsoftConfig[:tax_default])
  render :layout => false
end

#parse_uploadObject

Update articles from a spreadsheet


150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/articles_controller.rb', line 150

def parse_upload
  uploaded_file = params[:articles]['file'] or raise I18n.t('articles.controller.parse_upload.no_file')
  options = {filename: uploaded_file.original_filename}
  options[:outlist_absent] = (params[:articles]['outlist_absent'] == '1')
  options[:convert_units] = (params[:articles]['convert_units'] == '1')
  @updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_from_file uploaded_file.tempfile, options
  if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty?
    redirect_to supplier_articles_path(@supplier), :notice => I18n.t('articles.controller.parse_upload.notice')
  end
  @ignored_article_count = 0
rescue => error
  redirect_to upload_supplier_articles_path(@supplier), :alert => I18n.t('errors.general_msg', :msg => error.message)
end

#sharedObject

renders a view to import articles in local database


216
217
218
219
220
221
222
223
# File 'app/controllers/articles_controller.rb', line 216

def shared
  # build array of keywords, required for ransack _all suffix
  params[:q][:name_cont_all] = params[:q][:name_cont_all].split(' ') if params[:q]
  # Build search with meta search plugin
  @search = @supplier.shared_supplier.shared_articles.search(params[:q])
  @articles = @search.result.page(params[:page]).per(10)
  render :layout => false
end

#syncObject

sync all articles with the external database renders a form with articles, which should be updated


166
167
168
169
170
171
172
173
174
175
176
# File 'app/controllers/articles_controller.rb', line 166

def sync
  # check if there is an shared_supplier
  unless @supplier.shared_supplier
    redirect_to supplier_articles_url(@supplier), :alert => I18n.t('articles.controller.sync.shared_alert', :supplier => @supplier.name)
  end
  # sync articles against external database
  @updated_article_pairs, @outlisted_articles, @new_articles = @supplier.sync_all
  if @updated_article_pairs.empty? && @outlisted_articles.empty? && @new_articles.empty?
    redirect_to supplier_articles_path(@supplier), :notice => I18n.t('articles.controller.sync.notice')
  end
end

#updateObject

Updates one Article and highlights the line if succeded


65
66
67
68
69
70
71
72
73
# File 'app/controllers/articles_controller.rb', line 65

def update
  @article = Article.find(params[:id])

  if @article.update_attributes(params[:article])
    render :layout => false
  else
    render :action => 'new', :layout => false
  end
end

#update_allObject

Updates all article of specific supplier


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/articles_controller.rb', line 88

def update_all
  invalid_articles = false

  begin
    Article.transaction do
      unless params[:articles].blank?
        # Update other article attributes...
        @articles = Article.find(params[:articles].keys)
        @articles.each do |article|
          unless article.update_attributes(params[:articles][article.id.to_s])
            invalid_articles = true unless invalid_articles # Remember that there are validation errors
          end
        end

        raise ActiveRecord::Rollback  if invalid_articles # Rollback all changes
      end
    end
  end

  if invalid_articles
    # An error has occurred, transaction has been rolled back.
    flash.now.alert = I18n.t('articles.controller.error_invalid')
    render :edit_all
  else
    # Successfully done.
    redirect_to supplier_articles_path(@supplier), notice: I18n.t('articles.controller.update_all.notice')
  end
end

#update_selectedObject

makes different actions on selected articles


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/articles_controller.rb', line 118

def update_selected
  raise I18n.t('articles.controller.error_nosel') if params[:selected_articles].nil?
  articles = Article.find(params[:selected_articles])
  Article.transaction do
    case params[:selected_action]
      when 'destroy'
        articles.each(&:mark_as_deleted)
        flash[:notice] = I18n.t('articles.controller.update_sel.notice_destroy')
      when 'setNotAvailable'
        articles.each {|a| a.update_attribute(:availability, false) }
        flash[:notice] = I18n.t('articles.controller.update_sel.notice_unavail')
      when 'setAvailable'
        articles.each {|a| a.update_attribute(:availability, true) }
        flash[:notice] = I18n.t('articles.controller.update_sel.notice_avail')
      else
        flash[:alert] = I18n.t('articles.controller.update_sel.notice_noaction')
    end
  end
  # action succeded
  redirect_to supplier_articles_url(@supplier, :per_page => params[:per_page])

rescue => error
  redirect_to supplier_articles_url(@supplier, :per_page => params[:per_page]),
              :alert => I18n.t('errors.general_msg', :msg => error)
end

#update_synchronizedObject

Updates, deletes articles when upload or sync form is submitted


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/controllers/articles_controller.rb', line 179

def update_synchronized
  @outlisted_articles = Article.find(params[:outlisted_articles].try(:keys)||[])
  @updated_articles = Article.find(params[:articles].try(:keys)||[])
  @updated_articles.map{|a| a.assign_attributes(params[:articles][a.id.to_s]) }
  @new_articles = (params[:new_articles]||[]).map{|a| @supplier.articles.build(a) }

  has_error = false
  Article.transaction do
    # delete articles
    begin
      @outlisted_articles.each(&:mark_as_deleted)
    rescue
      # raises an exception when used in current order
      has_error = true
    end
    # Update articles
    @updated_articles.each {|a| a.save or has_error=true }
    # Add new articles
    @new_articles.each {|a| a.save or has_error=true }

    raise ActiveRecord::Rollback if has_error
  end

  if !has_error
    redirect_to supplier_articles_path(@supplier), notice: I18n.t('articles.controller.update_sync.notice')
  else
    @updated_article_pairs = @updated_articles.map do |article|
      orig_article = Article.find(article.id)
      [article, orig_article.unequal_attributes(article)]
    end
    flash.now.alert = I18n.t('articles.controller.error_invalid')
    render params[:from_action] == 'sync' ? :sync : :parse_upload
  end
end

#uploadObject

lets start with parsing articles from uploaded file, yeah Renders the upload form


146
147
# File 'app/controllers/articles_controller.rb', line 146

def upload
end