Class: Backend::ActivitiesController

Inherits:
BaseController show all
Includes:
InspectionViewable
Defined in:
app/controllers/backend/activities_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Instance Method Summary collapse

Methods included from InspectionViewable

#set_preference_view

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?

Instance Method Details

#duplicateObject

Duplicate activity basing on campaign


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/backend/activities_controller.rb', line 56

def duplicate
  source = if params[:source_campaign_id]
             Campaign.find(params[:source_campaign_id])
           else
             current_campaign.preceding
           end
  activity = Activity.find_by(id: params[:source_activity_id])

  new_campaign = Campaign.find_by(id: params[:campaign_id]) || current_campaign

  campaign_diff = new_campaign.harvest_year - source.harvest_year

  # Productions
  productions = ActivityProduction.of_campaign(source)
  productions = productions.of_activity(activity) if activity
  productions.each do |production|
    updates = { campaign: new_campaign }
    if production.started_on
      updates[:started_on] = production.started_on + campaign_diff.year
    end
    if production.stopped_on
      updates[:stopped_on] = production.stopped_on + campaign_diff.year
    end
    production.duplicate!(updates)
  end

  # Budgets
  budgets = ActivityBudget.of_campaign(source)
  budgets = budgets.of_activity(activity) if activity
  budgets.each do |budget|
    budget.duplicate!(budget.activity, new_campaign)
  end
  redirect_to params[:redirect] || { action: :index }
end

#familyObject

Returns wanted varieties proposition for given family_name


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/backend/activities_controller.rb', line 92

def family
  unless family = Nomen::ActivityFamily[params[:name]]
    head :not_found
    return
  end
  data = {
    label: family.human_name,
    name: family.name
  }
  if family.cultivation_variety.present?
    data[:cultivation_varieties] = Nomen::Variety.selection_hash(family.cultivation_variety)
  end
  if family.support_variety.present?
    data[:support_varieties] = Nomen::Variety.selection_hash(family.support_variety)
  end
  render json: data
end

#showObject


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/backend/activities_controller.rb', line 40

def show
  return unless @activity = find_and_check

  activity_crops = Plant
                   .joins(:inspections)
                   .where(activity_production_id: @activity.productions.map(&:id),
                          dead_at: nil)
                   .where.not(inspections: { forecast_harvest_week: nil })
                   .uniq

  @crops = initialize_grid(activity_crops, decorate: true)

  t3e @activity
end