Class: Backend::InterventionsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/interventions_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Class Method Summary collapse

Instance Method Summary collapse

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
:state State search
:campaign_id
:product_nature_id
:support_id

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
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/backend/interventions_controller.rb', line 35

def self.list_conditions
  code = ''
  # , productions: [:name], campaigns: [:name], activities: [:name], products: [:name]

  code = search_conditions(interventions: [:state, :number]) + " ||= []\n"
  code << "unless params[:state].blank?\n"
  code << "  c[0] << ' AND #{Intervention.table_name}.state IN (?)'\n"
  code << "  c << params[:state].flatten\n"
  code << "end\n"
  code << "unless params[:procedure_name].blank?\n"
  code << "  c[0] << ' AND #{Intervention.table_name}.procedure_name IN (?)'\n"
  code << "  c << params[:procedure_name]\n"
  code << "end\n"
  code << "c[0] << ' AND ' + params[:nature].join(' AND ') unless params[:nature].blank?\n"

  # Current campaign
  code << "if current_campaign\n"
  code << "  c[0] << \" AND EXTRACT(YEAR FROM #{Intervention.table_name}.started_at) = ?\"\n"
  code << "  c << current_campaign.harvest_year\n"
  code << "end\n"

  # Support
  code << "if params[:product_id].to_i > 0\n"
  code << "  c[0] << ' AND #{Intervention.table_name}.id IN (SELECT intervention_id FROM intervention_parameters WHERE type = \\'InterventionTarget\\' AND product_id IN (?))'\n"
  code << "  c << params[:product_id].to_i\n"
  code << "end\n"

  # ActivityProduction || Activity
  code << "if params[:production_id].to_i > 0\n"
  code << "  c[0] << ' AND #{Intervention.table_name}.id IN (SELECT intervention_id FROM intervention_parameters WHERE type = \\'InterventionTarget\\' AND product_id IN (SELECT target_id FROM target_distributions WHERE activity_production_id = ?))'\n"
  code << "  c << params[:production_id].to_i\n"
  code << "elsif params[:activity_id].to_i > 0\n"
  code << "  c[0] << ' AND #{Intervention.table_name}.id IN (SELECT intervention_id FROM intervention_parameters WHERE type = \\'InterventionTarget\\' AND product_id IN (SELECT target_id FROM target_distributions WHERE activity_id = ?))'\n"
  code << "  c << params[:activity_id].to_i\n"
  code << "end\n"
  code << "c\n "
  code.c
end

Instance Method Details

#computeObject

Computes impacts of a updated value in an intervention input context


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/backend/interventions_controller.rb', line 121

def compute
  unless params[:intervention]
    head(:unprocessable_entity)
    return
  end
  intervention_params = params[:intervention].deep_symbolize_keys
  procedure = Procedo.find(intervention_params[:procedure_name])
  unless procedure
    head(:not_found)
    return
  end
  intervention = Procedo::Engine.new_intervention(intervention_params)
  begin
    intervention.impact_with!(params[:updater])
    updater_id = 'intervention_' + params[:updater].gsub('[', '_attributes_').tr(']', '_')
    # raise intervention.to_hash.inspect
    respond_to do |format|
      # format.xml  { render xml: intervention.to_xml }
      format.json { render json: { updater_id: updater_id, intervention: intervention, handlers: intervention.handlers_states }.to_json }
    end
  rescue Procedo::Error => e
    respond_to do |format|
      # format.xml  { render xml:  { errors: e.message }, status: 500 }
      format.json { render json: { errors: e.message }, status: 500 }
    end
  end
end

#showObject

Show one intervention with params_id


111
112
113
114
115
116
117
118
# File 'app/controllers/backend/interventions_controller.rb', line 111

def show
  return unless @intervention = find_and_check
  t3e @intervention, procedure_name: @intervention.name
  respond_with(@intervention, methods: [:cost, :earn, :status, :name, :duration, :human_working_zone_area, :human_actions_names],
                              include: [{ parameters: { methods: [:reference_name, :default_name, :working_zone_svg, :human_quantity, :human_working_zone_area], include: { product: { methods: [:picture_path, :nature_name, :unit_name] } } } },
                                        { prescription: { include: [:prescriptor, :attachments] } }],
                              procs: proc { |options| options[:builder].tag!(:url, backend_intervention_url(@intervention)) })
end