Class: Backend::AnimalsController

Inherits:
MattersController show all
Defined in:
app/controllers/backend/animals_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ProductsController

#edit_many, #take, #update_many

Methods inherited from BaseController

account_lettering_states_crit, accounts_range_crit, amount_range_crit, crit_params, journal_entries_states_crit, journal_letter_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name, #session_controller?

Class Method Details

.list_conditionsObject

params:

:q Text search
:s State search
:period Two Dates with _ separator
:variant_id

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/backend/animals_controller.rb', line 27

def self.list_conditions
  code = ''
  code = search_conditions(products: %i[name work_number number description uuid], product_nature_variants: [:name]) + " ||= []\n"
  code << "unless (params[:period].blank? or params[:period].is_a? Symbol)\n"
  code << "  if params[:period] != 'all'\n"
  code << "    if params[:period] == 'interval' \n"
  code << "      started_on = params[:started_on] \n"
  code << "      stopped_on = params[:stopped_on] \n"
  code << "    else \n"
  code << "      interval = params[:period].split('_')\n"
  code << "      started_on = interval.first\n"
  code << "      stopped_on = interval.last\n"
  code << "    end \n"
  code << "    c[0] << \" AND #{Animal.table_name}.born_at::DATE BETWEEN ? AND ?\"\n"
  code << "    c << started_on\n"
  code << "    c << stopped_on\n"
  code << "  end\n "
  code << "end\n "
  # code << "unless (params[:s].blank? or params[:s].is_a? Symbol)\n"
  # code << "  if params[:s] != 'all'\n"
  # code << "    if params[:s] == 'healthy'\n"
  # code << "      c[0] << \" AND #{ProductReading.table_name}.product_id=#{Animal.table_name}.id AND #{ProductReading.table_name}.indicator_name='healthy' AND #{ProductReading.table_name}.boolean_value=true\"\n"
  # code << "    end\n "
  # code << "    if params[:s] == 'illness'\n"
  # code << "      c[0] << \" AND #{ProductReading.table_name}.product_id=#{Animal.table_name}.id AND #{ProductReading.table_name}.indicator_name='healthy' AND #{ProductReading.table_name}.boolean_value=false\"\n"
  # code << "    end\n "
  # code << "  end\n "
  # code << "end\n "
  code << "  if params[:variant_id].to_i > 0\n"
  code << "    c[0] << \" AND \#{ProductNatureVariant.table_name}.id = ?\"\n"
  code << "    c << params[:variant_id].to_i\n"
  code << "  end\n"
  code << "c\n"
  code.c
end

Instance Method Details

#add_groupObject

Insert a group


147
148
149
150
151
152
153
154
# File 'app/controllers/backend/animals_controller.rb', line 147

def add_group
  if params[:name] && variant = ProductNatureVariant.find_by(id: params[:variant_id])
    group = ProductGroup.create!(name: params[:name], variant: variant)
    render json: { id: group.id, name: group.name }, status: :ok
  else
    render json: 'Cannot save group. Parameters are missing.', status: :unprocessable_entity
  end
end

#add_to_containerObject


233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/controllers/backend/animals_controller.rb', line 233

def add_to_container
  return unless find_all
  if request.post?
    container = Product.find(params[:container_id])
    activity_production = ActivityProduction.find(params[:activity_production_id])
    # TODO: fix intervention
    container.add_content_products(@ids, at: params[:started_at], activity_production: activity_production)
    redirect_to params[:redirect] || backend_product_path(container)
  else
    params[:started_at] ||= Time.zone.now
  end
end

#add_to_groupObject


206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/controllers/backend/animals_controller.rb', line 206

def add_to_group
  return unless find_all
  targets = @ids.collect do |id|
    { product_id: id, reference_name: :animal }
  end
  parameters = {
    procedure_name: :animal_group_changing,
    intervention: {
      targets_attributes: targets
    }
  }
  redirect_to new_backend_intervention_path(parameters)
end

#add_to_variantObject


220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/controllers/backend/animals_controller.rb', line 220

def add_to_variant
  return unless find_all
  if request.post?
    variant = ProductNatureVariant.find(params[:variant_id])
    activity_production = ActivityProduction.find(params[:activity_production_id])
    # TODO: fix intervention
    variant.add_products(@ids, at: params[:started_at], activity_production: activity_production)
    redirect_to params[:redirect] || backend_product_nature_variant_path(variant)
  else
    params[:started_at] ||= Time.zone.now
  end
end

#changeObject


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
116
117
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
143
144
# File 'app/controllers/backend/animals_controller.rb', line 90

def change
  # params[:animals_id]
  # params[:container]
  # params[:group]
  # params[:worker]
  # params[:variant]
  # params[:started_at]
  # params[:stopped_at]
  # check animal exist
  if params[:animals_id]
    animals = params[:animals_id].split(',').collect do |animal_id|
      find_and_check(id: animal_id.to_i)
    end.compact
  end

  errors = []

  begin
    started_at = Date.parse(params[:started_at])
  rescue StandardError => e
    errors = notify_error(e.message.parameterize('_').to_sym.tn, type: :started_at)
  end

  begin
    stopped_at = Date.parse(params[:stopped_at])
  rescue StandardError => e
    errors = notify_error(e.message.parameterize('_').to_sym.tn, type: :stopped_at)
  end

  group = AnimalGroup.find_by(id: params[:group])

  if group.nil? || params[:group].blank?
    errors = notify_error(:unavailable_resource, type: AnimalGroup.model_name.human, id: params[:group].to_s)
  end

  container = Product.find_by(id: params[:container])

  if container.nil? || params[:container].blank?
    errors = notify_error(:unavailable_resource, type: Product.human_attribute_name(:container), id: params[:container].to_s)
  end

  procedure_natures = []

  # massive assignment
  procedure_natures << :animal_moving if params[:container].present?
  procedure_natures << :animal_evolution if params[:variant].present?

  procedure_natures << :animal_group_changing if params[:group].present?

  if errors.any?
    render json: { errors: errors }, status: :unprocessable_entity
  else
    render json: { result: 'ok' }, status: :created
  end
end

#indexObject

Show a list of animal groups


157
158
159
160
161
162
163
164
# File 'app/controllers/backend/animals_controller.rb', line 157

def index
  @animals = Animal.all
  # passing a parameter to Jasper for company full name and id
  @entity_of_company_full_name = Entity.of_company.full_name
  @entity_of_company_id = Entity.of_company.id

  respond_with @animals, methods: %i[picture_path sex_text variety_text], include: %i[initial_father initial_mother nature variant]
end

#keepObject


187
188
189
190
191
192
193
194
195
# File 'app/controllers/backend/animals_controller.rb', line 187

def keep
  return head :unprocessable_entity unless params[:id].nil? || (params[:id] && find_all)

  begin
    current_user.prefer! 'products_for_intervention', params[:id], :string
  end

  render json: { id: 'products_for_intervention' }
end

#load_animalsObject


83
84
85
86
87
88
# File 'app/controllers/backend/animals_controller.rb', line 83

def load_animals
  @read_at = params[:scope] == 'now' ? { at: Time.zone.now } : @read_at = { at: false }

  @animal_groups = AnimalGroup.availables(@read_at).order(:name)
  @animals = Animal.availables(@read_at).order(:name)
end

#matching_interventionsObject


197
198
199
200
201
202
203
204
# File 'app/controllers/backend/animals_controller.rb', line 197

def matching_interventions
  return head :unprocessable_entity unless params[:id].nil? || (params[:id] && find_all)
  varieties = Animal.where(id: @ids).pluck(:variety).uniq if @ids

  respond_to do |format|
    format.js { render partial: 'matching_interventions', locals: { varieties: varieties } }
  end
end

#showObject

Show one animal with params_id


174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/backend/animals_controller.rb', line 174

def show
  return unless @animal = find_and_check
  # TODO: remove it. On animal show dialog (Golumn), add issue. Break redirect on submit.
  params.delete('dialog')

  t3e @animal, nature: @animal.nature_name
  respond_with(@animal, methods: %i[picture_path sex_text variety_text], include: [:father, :mother, :variant, :nature, :variety,
                                                                                   { readings: {} },
                                                                                   { intervention_product_parameters: { include: :intervention } },
                                                                                   { memberships: { include: :group } },
                                                                                   { localizations: { include: :container } }])
end