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

#take

Methods inherited from BaseController

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Class Method Details

.list_conditionsObject

params:

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

26
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
# File 'app/controllers/backend/animals_controller.rb', line 26

def self.list_conditions
  code = ''
  code = search_conditions(products: [: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 << "    interval = params[:period].split('_')\n"
  code << "    first_date = interval.first\n"
  code << "    last_date = interval.last\n"
  code << "    c[0] << \" AND #{Animal.table_name}.born_at BETWEEN ? AND ?\"\n"
  code << "    c << first_date\n"
  code << "    c << last_date\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


185
186
187
188
189
190
191
192
# File 'app/controllers/backend/animals_controller.rb', line 185

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


248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/controllers/backend/animals_controller.rb', line 248

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


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

def add_to_group
  return unless find_all
  if request.post?
    group = AnimalGroup.find(params[:group_id])
    activity_production = ActivityProduction.find(params[:activity_production_id])
    # TODO: fix intervention
    group.add_animals(@ids, at: params[:started_at], activity_production: activity_production)
    redirect_to params[:redirect] || backend_animal_group_path(group)
  else
    params[:started_at] ||= Time.zone.now
  end
end

#add_to_variantObject


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

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


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/backend/animals_controller.rb', line 134

def change
  # params[:animals_id]
  # params[:container_id]
  # params[:group_id]
  # 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

  procedure_natures = []

  procedure_natures << :animal_moving if params[:container_id].present?
  procedure_natures << :animal_group_changing if params[:group_id].present?
  procedure_natures << :animal_evolution if params[:variant_id].present?

  Intervention.write(*procedure_natures, short_name: :animal_changing, started_at: params[:started_at], stopped_at: params[:stopped_at], production_support: ActivityProduction.find_by(id: params[:production_support_id])) do |i|
    i.cast :caregiver, Product.find_by(id: params[:worker_id]), role: 'animal_moving-doer', position: 1
    ah = nil
    ag = nil
    av = nil
    if procedure_natures.include?(:animal_moving)
      ah = i.cast :animal_housing, Product.find_by(id: params[:container_id]), role: ['animal_moving-target'], position: 2
    end
    if procedure_natures.include?(:animal_group_changing)
      ag = i.cast :herd, Product.find_by(id: params[:group_id]), role: ['animal_group_changing-target'], position: 3
    end
    if procedure_natures.include?(:animal_evolution)
      av = i.cast :new_animal_variant, ProductNatureVariant.find_by(id: params[:variant_id]), role: ['animal_evolution-variant'], position: 4, variant: true
    end
    animals.each_with_index do |a, index|
      ac = i.cast :animal, a, role: ['animal_moving-input', 'animal_group_changing-input', 'animal_evolution-target'], position: index + 5
      if procedure_natures.include?(:animal_moving)
        i.task :entering, product: ac, localizable: ah
      end
      if procedure_natures.include?(:animal_group_changing)
        i.task :group_inclusion, member: ac, group: ag
        # i.group_inclusion :animal, :herd
      end
      if procedure_natures.include?(:animal_evolution)
        i.task :variant_cast, product: ac, variant: av
        # i.variant_cast :animal, :new_animal_variant
      end
    end
  end

  render json: { result: 'ok' }
end

#indexObject

Show a list of animal groups


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

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: [:picture_path, :sex_text, :variety_text], include: [:initial_father, :initial_mother, :nature, :variant]
end

#load_animalsObject


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/backend/animals_controller.rb', line 77

def load_animals
  @grouped_animals = []
  AnimalGroup.all.select(:id, :name).each do |group|
    @grouped_animals << { group: group, places_and_animals: group.members_with_places_at }
  end

  preference = current_user.preference("golumn.#{params[:golumn_id]}", {}.to_yaml)
  user_pref = YAML.load(preference.value)

  @sorted = []
  user_pref.deep_symbolize_keys!
  if user_pref[:positions].present?
    user_pref[:positions].each do |g|
      @grouped_animals.each do |h|
        @sorted << h if h[:group].id == g[:id]
      end
    end
  else
    @sorted = @grouped_animals
  end

  without_container = []
  Animal.of_enjoyer(Entity.of_company).select(:id, :name, :identification_number, :nature_id, :dead_at).each do |a|
    if a.container.nil? || a.memberships.empty?
      without_container << { animal: a.to_json(methods: [:picture_path, :sex_text, :status]) }
    end
  end
  @sorted << { others: without_container } if without_container.any?
  render json: @sorted.to_json
end

#load_containersObject


108
109
110
111
# File 'app/controllers/backend/animals_controller.rb', line 108

def load_containers
  @containers = Product.select(:id, :name).of_expression('can store(animal)')
  render json: @containers
end

#load_naturesObject


118
119
120
121
# File 'app/controllers/backend/animals_controller.rb', line 118

def load_natures
  @natures = ProductNatureVariant.of_variety(:animal).select(:id, :name).all
  render json: @natures
end

#load_production_supportsObject


123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/backend/animals_controller.rb', line 123

def load_production_supports
  prod = {}
  arr = []
  ActivityProduction.where(support: params[:group_id]).each do |p|
    prod[:id] = p.id
    prod[:name] = p.production.name + " (#{p.production.campaign.name})"
    arr << prod
  end
  render json: arr, status: 200
end

#load_workersObject


113
114
115
116
# File 'app/controllers/backend/animals_controller.rb', line 113

def load_workers
  @workers = Worker.select(:id, :name).all
  render json: @workers
end

#showObject

Show one animal with params_id


212
213
214
215
216
217
218
219
220
# File 'app/controllers/backend/animals_controller.rb', line 212

def show
  return unless @animal = find_and_check
  t3e @animal, nature: @animal.nature_name
  respond_with(@animal, methods: [:picture_path, :sex_text, :variety_text], include: [:father, :mother, :variant, :nature, :variety,
                                                                                      { readings: {} },
                                                                                      { intervention_parameters: { include: :intervention } },
                                                                                      { memberships: { include: :group } },
                                                                                      { localizations: { include: :container } }])
end