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.

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?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Class Method Details

.best_for_cultivation(family, cultivation_variety) ⇒ Object

Find nearest family on cultivation variety and support variety


393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'app/models/activity.rb', line 393

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


319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'app/models/activity.rb', line 319

def color(family, variety)
  colors = { gold: '#FFD700', golden_rod: '#DAA520', yellow: '#FFFF00',
             orange: '#FF8000', red: '#FF0000', green: '#80BB00',
             green_yellow: '#ADFF2F', spring_green: '#00FF7F',
             dark_green: '#006400', lime: '#00FF00', dark_turquoise: '#00FFFF',
             blue: '#0000FF', purple: '#BF00FF', gray: '#A4A4A4',
             slate_gray: '#708090', dark_magenta: '#8B008B', violet: '#EE82EE',
             teal: '#008080', fuchsia: '#FF00FF', brown: '#6A2B1A' }
  activity_family = Nomen::ActivityFamily.find(family)
  variety = Nomen::Variety.find(variety)
  crop_sets = []
  # if there no variety (no land cases)
  if variety
    crop_sets = Nomen::CropSet.select do |i|
      i.varieties.detect { |v| variety <= v }
    end.map { |i| i.name.to_sym }
  end
  return colors[:gray] unless activity_family
  if activity_family <= :plant_farming && (variety || crop_sets.any?)
    # MEADOW
    if crop_sets.include?(:meadow)
      colors[:dark_green]
    # CEREALS
    elsif crop_sets.include?(:cereals)
      if variety <= :zea || variety <= :sorghum
        colors[:orange]
      elsif variety <= :hordeum || variety <= :avena || variety <= :secale
        '#EEDD99'
      elsif variety <= :triticum || variety <= :triticosecale
        colors[:gold]
      else
        colors[:golden_rod]
      end
    # OILSEED
    elsif crop_sets.include?(:oleaginous)
      colors[:green_yellow]
    # PROTEINS
    elsif crop_sets.include?(:proteaginous)
      colors[:teal]
    # FIBER
    elsif variety <= :linum ||
          variety <= :cannabis
      colors[:slate_gray]
    # LEGUMINOUS
    elsif crop_sets.include?(:leguminous)
      colors[:lime]
    elsif crop_sets.include?(:vegetables)
      colors[:red]
    elsif crop_sets.include?(:arboricultural)
      colors[:blue]
    # VINE
    elsif variety <= :vitaceae
      colors[:purple]
    elsif crop_sets.include?(:aromatics_and_medicinals)
      colors[:dark_turquoise]
    elsif crop_sets.include?(:tropicals)
      colors[:fuchsia]
    elsif variety <= :nicotiana
      colors[:dark_turquoise]
    else
      colors[:green]
    end
  elsif activity_family <= :animal_farming
    colors[:brown]
  elsif activity_family <= :administering
    colors[:brown]
  elsif activity_family <= :tool_maintaining
    colors[:blue]
  else
    colors[:gray]
  end
end

.find_best_family(cultivation_variety, _support_variety = nil) ⇒ Object

Find nearest family on cultivation variety and support variety


414
415
416
417
418
419
420
421
422
423
424
425
# File 'app/models/activity.rb', line 414

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


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

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

#budget_of(campaign) ⇒ Object


249
250
251
252
# File 'app/models/activity.rb', line 249

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

#colorObject

Returns a specific color for the given activity


298
299
300
301
302
303
304
# File 'app/models/activity.rb', line 298

def color
  if cultivation_variety
    self.class.color(family, cultivation_variety)
  else
    return '#000000'
  end
end

#count_during(campaign) ⇒ Object


254
255
256
# File 'app/models/activity.rb', line 254

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

#cultivation_variety_nameObject

Returns human_name of support variety


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

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

#estimate_yield_from_budget_of(options = {}) ⇒ Object

return estimate yield from first budget in revenus item for given variety


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/models/activity.rb', line 218

def estimate_yield_from_budget_of(options = {})
  # set default parameter if theres no one given
  options[:unit] ||= :quintal_per_hectare
  options[:variety] ||= 'grain'

  activity_working_unit = size_unit_name
  target_variety = Nomen::Variety[options[:variety]]

  selected_budget = budget_of(options[:campaign])
  if selected_budget
    r = []
    selected_budget.revenues.each do |item|
      item_unit = "#{item.variant_unit}_per_#{activity_working_unit}" if activity_working_unit
      if item.variant && item.variant_unit && Nomen::Variety[item.variant.variety] <= target_variety
        r << item.quantity.in(item_unit).convert(options[:unit])
      end
    end
    return r.compact.sum
  else
    return nil
  end
end

#family_labelObject

Returns human name of activity family


293
294
295
# File 'app/models/activity.rb', line 293

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

#intervention_parametersObject


245
246
247
# File 'app/models/activity.rb', line 245

def intervention_parameters
  InterventionParameter.of_activity(self)
end

#interventionsObject


241
242
243
# File 'app/models/activity.rb', line 241

def interventions
  Intervention.of_activity(self)
end

#interventions_duration(campaign) ⇒ Object


436
437
438
439
# File 'app/models/activity.rb', line 436

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

#is_of_family?(family) ⇒ Boolean

Returns:

  • (Boolean)

441
442
443
# File 'app/models/activity.rb', line 441

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

#of_campaign?(campaign) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#real_expense_amount(campaign) ⇒ Object


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

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

#size_during(campaign) ⇒ Object


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

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


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

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


279
280
281
282
283
# File 'app/models/activity.rb', line 279

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

#used_during?(campaign) ⇒ Boolean

Returns:

  • (Boolean)

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

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