Class: Activity

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/activity.rb

Overview

Activity represents a type of work in the farm like common wheats, pigs, fish etc.. Activities are expected to last in years. Activity productions are production done inside the given activity with same work method.

Constant Summary collapse

COLORS_INDEX =
Rails.root.join('db', 'nomenclatures', 'colors.yml').freeze
COLORS =
(COLORS_INDEX.exist? ? YAML.load_file(COLORS_INDEX) : {}).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Class Method Details

.best_for_cultivation(family, cultivation_variety) ⇒ Object

Find nearest family on cultivation variety and support variety


368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'app/models/activity.rb', line 368

def best_for_cultivation(family, cultivation_variety)
  return nil unless any?
  searched = Nomen::Variety.find(cultivation_variety)
  activities = of_family(family).select do |activity|
    searched <= activity.cultivation_variety
  end
  return activities.first if activities.count == 1
  best = nil
  littlest_degree_of_kinship = nil
  activities.each do |a|
    degree = searched.degree_of_kinship_with(a.cultivation_variety)
    next unless degree
    if littlest_degree_of_kinship.nil? || littlest_degree_of_kinship > degree
      littlest_degree_of_kinship = degree
      best = a
    end
  end
  best
end

.color(family, variety) ⇒ Object

Returns a color for given family and variety short-way solution, can be externalized in mid-way solution


348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'app/models/activity.rb', line 348

def color(family, variety)
  activity_family = Nomen::ActivityFamily.find(family)
  variety = Nomen::Variety.find(variety)
  return 'White' unless activity_family
  if activity_family <= :plant_farming
    list = COLORS['varieties']
    return 'Gray' unless list
    variety.rise { |i| list[i.name] } unless variety.nil?
  elsif activity_family <= :animal_farming
    'Brown'
  elsif activity_family <= :administering
    'RoyalBlue'
  elsif activity_family <= :tool_maintaining
    'SlateGray'
  else
    'DarkGray'
  end
end

.find_best_family(cultivation_variety, _support_variety = nil) ⇒ Object

Find nearest family on cultivation variety and support variety


389
390
391
392
393
394
395
396
397
398
399
400
# File 'app/models/activity.rb', line 389

def find_best_family(cultivation_variety, _support_variety = nil)
  if cultivation_variety.present?
    family = :plant_farming if cultivation_variety <= :plant
    family = :animal_farming if cultivation_variety <= :animal
    family = :tool_maintaining if cultivation_variety <= :equipment ||
                                  cultivation_variety <= :building ||
                                  cultivation_variety <= :building_division
    family = :wine_making if cultivation_variety <= :wine
  end
  family ||= :administering
  Nomen::ActivityFamily.find(family)
end

Instance Method Details

#budget_expenses_amount(campaign) ⇒ Object


324
325
326
327
328
# File 'app/models/activity.rb', line 324

def budget_expenses_amount(campaign)
  budget = budget_of(campaign)
  return 0.0 unless budget
  budget.expenses_amount
end

#budget_of(campaign) ⇒ Object


267
268
269
270
# File 'app/models/activity.rb', line 267

def budget_of(campaign)
  return nil unless campaign
  budgets.find_by(campaign: campaign)
end

#colorObject

Returns a specific color for the given activity


316
317
318
# File 'app/models/activity.rb', line 316

def color
  self.class.color(family, cultivation_variety)
end

#count_during(campaign) ⇒ Object


272
273
274
# File 'app/models/activity.rb', line 272

def count_during(campaign)
  productions.of_campaign(campaign).count
end

#cultivation_variety_nameObject

Returns human_name of support variety


304
305
306
307
308
# File 'app/models/activity.rb', line 304

def cultivation_variety_name
  item = Nomen::Variety.find(cultivation_variety)
  return nil unless item
  item.human_name
end

#family_labelObject

Returns human name of activity family


311
312
313
# File 'app/models/activity.rb', line 311

def family_label
  Nomen::ActivityFamily.find(family).human_name
end

#inspectionable?Boolean

Returns:

  • (Boolean)

420
421
422
# File 'app/models/activity.rb', line 420

def inspectionable?
  use_gradings && inspection_calibration_scales.any? && inspections.any?
end

#intervention_parametersObject


263
264
265
# File 'app/models/activity.rb', line 263

def intervention_parameters
  InterventionParameter.of_activity(self)
end

#interventionsObject


259
260
261
# File 'app/models/activity.rb', line 259

def interventions
  Intervention.of_activity(self)
end

#interventions_duration(campaign) ⇒ Object


411
412
413
414
# File 'app/models/activity.rb', line 411

def interventions_duration(campaign)
  # productions.of_campaign(campaign).map(&:duration).compact.sum
  productions.of_campaign(campaign).collect { |p| p.interventions.real.sum(:working_duration) }.sum
end

#is_of_family?(family) ⇒ Boolean

Returns:

  • (Boolean)

416
417
418
# File 'app/models/activity.rb', line 416

def is_of_family?(family)
  Nomen::ActivityFamily[self.family] <= family
end

#measure_something?Boolean

Returns:

  • (Boolean)

424
425
426
# File 'app/models/activity.rb', line 424

def measure_something?
  measure_grading_items_count || measure_grading_net_mass || measure_grading_sizes
end

#of_campaign?(campaign) ⇒ Boolean

Returns:

  • (Boolean)

286
287
288
# File 'app/models/activity.rb', line 286

def of_campaign?(campaign)
  productions.of_campaign(campaign).any?
end

#pfi_activity_ratio(campaign) ⇒ Object


246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/models/activity.rb', line 246

def pfi_activity_ratio(campaign)
  pfi_activity = 0
  global_area = []
  production_pfi_per_area = []
  productions.of_campaign(campaign).each do |production|
    area_in_hectare = production.net_surface_area.to_d(:hectare)
    production_pfi_per_area << (production.pfi_parcel_ratio * area_in_hectare).round(2)
    global_area << area_in_hectare
  end
  pfi_activity = (production_pfi_per_area.compact.sum / global_area.compact.sum).round(2) unless global_area.compact.empty? || global_area.compact.sum.zero?
  pfi_activity
end

#quandl_datasetObject


330
331
332
333
334
335
336
337
338
339
340
# File 'app/models/activity.rb', line 330

def quandl_dataset
  if Nomen::Variety[self.cultivation_variety.to_sym] <= :triticum_aestivum
    'CHRIS/LIFFE_EBM4'
  elsif Nomen::Variety[self.cultivation_variety.to_sym] <= :brassica_napus
    'CHRIS/LIFFE_ECO4'
  elsif Nomen::Variety[self.cultivation_variety.to_sym] <= :hordeum_hexastichum
    'CHRIS/ICE_BW2'
  elsif Nomen::Variety[self.cultivation_variety.to_sym] <= :zea
    'CHRIS/LIFFE_EMA10'
  end
end

#real_expense_amount(campaign) ⇒ Object


320
321
322
# File 'app/models/activity.rb', line 320

def real_expense_amount(campaign)
  Intervention.of_campaign(campaign).of_activity(self).map(&:cost).compact.sum
end

#size_during(campaign) ⇒ Object


290
291
292
293
294
# File 'app/models/activity.rb', line 290

def size_during(campaign)
  total = productions.of_campaign(campaign).map(&:size).sum
  total = total.in(size_unit) if size_unit
  total
end

#support_shape_area(*campaigns) ⇒ Object Also known as: net_surface_area


403
404
405
406
407
# File 'app/models/activity.rb', line 403

def support_shape_area(*campaigns)
  options = campaigns.extract_options!
  productions.of_campaign(*campaigns).map(&:support_shape_area)
             .compact.sum.in(options[:unit] || :square_meter)
end

#support_variety_nameObject

Returns human_name of support variety


297
298
299
300
301
# File 'app/models/activity.rb', line 297

def support_variety_name
  item = Nomen::Variety.find(support_variety)
  return nil unless item
  item.human_name
end

#unit_choicesObject


428
429
430
431
432
# File 'app/models/activity.rb', line 428

def unit_choices
  %i[items_count net_mass]
    .reject { |e| e == :items_count && !measure_grading_items_count }
    .reject { |e| e == :net_mass && !measure_grading_net_mass }
end

#unit_preference(user, unit = nil) ⇒ Object


434
435
436
437
438
439
440
441
# File 'app/models/activity.rb', line 434

def unit_preference(user, unit = nil)
  unit_preference_name = "activity_#{id}_inspection_view_unit"
  user.prefer!(unit_preference_name, unit.to_sym) if unit.present?
  pref = user.preference(unit_preference_name).value
  pref ||= :items_count
  pref = unit_choices.find { |c| c.to_sym == pref.to_sym }
  pref ||= unit_choices.first
end

#used_during?(campaign) ⇒ Boolean

Returns:

  • (Boolean)

276
277
278
# File 'app/models/activity.rb', line 276

def used_during?(campaign)
  productions.of_campaign(campaign).any?
end