Class: Studies::AssetGroupsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/studies/asset_groups_controller.rb

Instance Method Summary collapse

Instance Method Details

#addObject


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/studies/asset_groups_controller.rb', line 106

def add
  @asset_group = AssetGroup.find(params[:id])
  @study = Study.find(params[:study_id])
  if params[:asset]
    ids = params[:asset].map { |a| a[1] == '1' ? a[0] : nil }.select { |a| !a.nil? }
    @assets = Asset.find(ids)
    @asset_group.assets << @assets
  end

  respond_to do |format|
    format.html { redirect_to(study_asset_group_url(@study, @asset_group)) }
    format.xml  { render xml: @assets }
    format.json { render json: @assets }
  end
end

#createObject


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/studies/asset_groups_controller.rb', line 41

def create
  @study = Study.find(params[:study_id])
  @asset_group = AssetGroup.new(params[:asset_group])
  @asset_group.study = @study

  respond_to do |format|
    if @asset_group.save
      flash[:notice] = 'AssetGroup was successfully created.'
      format.html { redirect_to study_asset_group_path(@study, @asset_group) }
      format.xml  { render xml: @asset_group, status: :created, location: @asset_group }
      format.json { render json: @asset_group, status: :created, location: @asset_group }
    else
      format.html { render action: 'new' }
      format.xml  { render xml: @asset_group.errors, status: :unprocessable_entity }
      format.json { render json: @asset_group.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject


76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/studies/asset_groups_controller.rb', line 76

def destroy
  @asset_group = AssetGroup.find(params[:id])
  @asset_group.destroy
  @study = Study.find(params[:study_id])

  respond_to do |format|
    format.html { redirect_to(study_asset_groups_url(@study)) }
    format.xml  { head :ok }
  end
end

#editObject


36
37
38
39
# File 'app/controllers/studies/asset_groups_controller.rb', line 36

def edit
  @asset_group = AssetGroup.find(params[:id])
  @study = Study.find(params[:study_id])
end

#indexObject


6
7
8
9
10
11
12
13
14
# File 'app/controllers/studies/asset_groups_controller.rb', line 6

def index
  @study = Study.find(params[:study_id])
  @asset_groups = @study.asset_groups

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render xml: @asset_groups }
  end
end

#newObject


26
27
28
29
30
31
32
33
34
# File 'app/controllers/studies/asset_groups_controller.rb', line 26

def new
  @asset_group = AssetGroup.new
  @study = Study.find(params[:study_id])

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render xml: @asset_group }
  end
end

127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/studies/asset_groups_controller.rb', line 127

def print
  @asset_group = AssetGroup.find(params[:id])
  @study = Study.find(params[:study_id])

  @assets = @asset_group ? @asset_group.assets.select { |asset| asset.is_a?(Barcode::Barcodeable) } : []

  unbarcoded = @asset_group.assets.reject { |asset| asset.is_a?(Barcode::Barcodeable) }
  @unbarcoded_types = unbarcoded.map { |ub| ub.sti_type.pluralize.humanize }.uniq.to_sentence
  @unbarcoded_count = unbarcoded.length
  @containers = unbarcoded.map { |ub| ub.labware }.uniq.select { |labware| labware.is_a?(Barcode::Barcodeable) }
end

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/controllers/studies/asset_groups_controller.rb', line 139

def print_labels
  @asset_group = AssetGroup.find(params[:id])
  @study = Study.find(params[:study_id])

  print_job = LabelPrinter::PrintJob.new(params[:printer],
                                         LabelPrinter::Label::AssetRedirect,
                                         printables: params[:printables])
  if print_job.execute
    flash[:notice] = print_job.success
    redirect_to study_asset_groups_path(@study)
  else
    flash[:error] = print_job.errors.full_messages.join('; ')
    redirect_to print_study_asset_group_path(@study, @asset_group)
  end
end

#printingObject


122
123
124
125
# File 'app/controllers/studies/asset_groups_controller.rb', line 122

def printing
  @study = Study.find(params[:study_id])
  @asset_groups = @study.asset_groups
end

#searchObject


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/studies/asset_groups_controller.rb', line 87

def search
  @study = Study.find(params[:study_id])
  query = params[:q]
  if query.blank? or query.length < 2
    # We should not blame the user, we should instead help.
    # - By returning the X most recent ones together with an explanation.
    flash[:error] = 'Search too wide. Please make your query more specific.'
    redirect_to study_asset_groups_path(@study)
    return
  else
    @assets = Labware.where(['name like ?', "%#{query}%"])
  end
  @asset_group = AssetGroup.find(params[:id])
  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render xml: @assets }
  end
end

#showObject


16
17
18
19
20
21
22
23
24
# File 'app/controllers/studies/asset_groups_controller.rb', line 16

def show
  @asset_group = AssetGroup.find(params[:id])
  @study = Study.find(params[:study_id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render xml: @asset_group }
  end
end

#updateObject


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/studies/asset_groups_controller.rb', line 60

def update
  @asset_group = AssetGroup.find(params[:id])
  @study = Study.find(params[:study_id])

  respond_to do |format|
    if @asset_group.update(params[:asset_group])
      flash[:notice] = 'AssetGroup was successfully updated.'
      format.html { redirect_to study_asset_group_path(@study, @asset_group) }
      format.xml  { head :ok }
    else
      format.html { render action: 'edit' }
      format.xml  { render xml: @asset_group.errors, status: :unprocessable_entity }
    end
  end
end