Class: ForestLiana::ActionsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/forest_liana/actions_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#route_not_found

Instance Method Details

#changeObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/forest_liana/actions_controller.rb', line 95

def change
  action = get_action(params[:collectionName])

  if !action
    render status: 500, json: {error: 'Error in smart action change hook: cannot retrieve action from collection'}
  else
    # Transform fields from array to an object to ease usage in hook.
    context = get_smart_action_change_ctx(params[:fields])
    formatted_fields = context[:fields].clone # clone for following test on is_same_data_structure

    # Call the user-defined change hook.
    result = action.hooks[:change][params[:changedField]].(context)

    handle_result(result, formatted_fields, action)
  end
end

#get_action(collection_name) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'app/controllers/forest_liana/actions_controller.rb', line 12

def get_action(collection_name)
  collection = get_collection(collection_name)
  begin
     collection.actions.find {|action| ActiveSupport::Inflector.parameterize(action.name) == params[:action_name]}
  rescue => error
    FOREST_LOGGER.error "Smart Action get action retrieval error: #{error}"
    nil
  end
end

#get_collection(collection_name) ⇒ Object



8
9
10
# File 'app/controllers/forest_liana/actions_controller.rb', line 8

def get_collection(collection_name)
  ForestLiana.apimap.find { |collection| collection.name.to_s == collection_name }
end

#get_recordObject



22
23
24
25
26
27
# File 'app/controllers/forest_liana/actions_controller.rb', line 22

def get_record
  model = ForestLiana::SchemaUtils.find_model_from_collection_name(params[:collectionName])
  redord_getter = ForestLiana::ResourceGetter.new(model, {:id => params[:recordIds][0]})
  redord_getter.perform
  redord_getter.record
end

#get_smart_action_change_ctx(fields) ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/controllers/forest_liana/actions_controller.rb', line 37

def get_smart_action_change_ctx(fields)
  fields = fields.reduce({}) do |p, c|
    field = c.permit!.to_h.symbolize_keys
    ForestLiana::WidgetsHelper.set_field_widget(field)
    p.update(c[:field] => field)
  end
  {:record => get_record, :fields => fields}
end

#get_smart_action_load_ctx(fields) ⇒ Object



29
30
31
32
33
34
35
# File 'app/controllers/forest_liana/actions_controller.rb', line 29

def get_smart_action_load_ctx(fields)
  fields = fields.reduce({}) do |p, c|
    ForestLiana::WidgetsHelper.set_field_widget(c)
    p.update(c[:field] => c.merge!(value: nil))
  end
  {:record => get_record, :fields => fields}
end

#handle_result(result, formatted_fields, action) ⇒ Object



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
73
74
75
76
# File 'app/controllers/forest_liana/actions_controller.rb', line 46

def handle_result(result, formatted_fields, action)
  if result.nil? || !result.is_a?(Hash)
    return render status: 500, json: { error: 'Error in smart action load hook: hook must return an object' }
  end
  is_same_data_structure = ForestLiana::IsSameDataStructureHelper::Analyser.new(formatted_fields, result, 1)
  unless is_same_data_structure.perform
    return render status: 500, json: { error: 'Error in smart action hook: fields must be unchanged (no addition nor deletion allowed)' }
  end

  # Apply result on fields (transform the object back to an array), preserve order.
  fields = action.fields.map do |field|
    updated_field = result[field[:field]]

    # Reset `value` when not present in `enums` (which means `enums` has changed).
    if updated_field[:enums].is_a?(Array)
      # `value` can be an array if the type of fields is `[x]`
      if updated_field[:type].is_a?(Array) && updated_field[:value].is_a?(Array) && !(updated_field[:value] - updated_field[:enums]).empty?
        updated_field[:value] = nil
      end

      # `value` can be any other value
      if !updated_field[:type].is_a?(Array) && !updated_field[:enums].include?(updated_field[:value])
        updated_field[:value] = nil
      end
    end

    updated_field
  end

  render serializer: nil, json: { fields: fields}, status: :ok
end

#loadObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/forest_liana/actions_controller.rb', line 78

def load
  action = get_action(params[:collectionName])

  if !action
    render status: 500, json: {error: 'Error in smart action load hook: cannot retrieve action from collection'}
  else
    # Transform fields from array to an object to ease usage in hook, adds null value.
    context = get_smart_action_load_ctx(action.fields)
    formatted_fields = context[:fields].clone # clone for following test on is_same_data_structure

    # Call the user-defined load hook.
    result = action.hooks[:load].(context)

    handle_result(result, formatted_fields, action)
  end
end

#valuesObject



4
5
6
# File 'app/controllers/forest_liana/actions_controller.rb', line 4

def values
  render serializer: nil, json: {}, status: :ok
end