Module: Cortex::ContentItemHelper

Included in:
ContentItemsController
Defined in:
app/helpers/cortex/content_item_helper.rb

Instance Method Summary collapse

Instance Method Details

#content_item_paramsObject



35
36
37
38
39
40
41
# File 'app/helpers/cortex/content_item_helper.rb', line 35

def content_item_params
  params.require(:content_item).permit(
    :creator_id,
    :content_type_id,
    field_items_attributes: field_items_attributes_params,
  )
end

#content_item_reload(content_item) ⇒ Object



27
28
29
30
31
32
33
# File 'app/helpers/cortex/content_item_helper.rb', line 27

def content_item_reload(content_item)
  @content_item = content_item
  content_type.fields.each do |field|
    @content_item.field_items << Cortex::FieldItem.new(field: field, data: field_item_param_data(params_lookup[field.id]))
  end
  @content_item
end

#content_typeObject



3
4
5
# File 'app/helpers/cortex/content_item_helper.rb', line 3

def content_type
  @content_type ||= Cortex::ContentType.find_by_id(params[:content_type_id])
end

#create_content_itemObject



7
8
9
10
11
12
13
14
15
# File 'app/helpers/cortex/content_item_helper.rb', line 7

def create_content_item
  CreateContentItemTransaction.new
    .with_step_args(
      execute_content_item_state_change: [state: params[:content_item][:state]]
    )
    .call(id: params[:id], content_type: content_type,
          content_item_params: content_item_params, current_user: current_user)
    .value!
end

#field_item_param_data(field_item_params) ⇒ Object



95
96
97
98
99
# File 'app/helpers/cortex/content_item_helper.rb', line 95

def field_item_param_data(field_item_params)
  return {} unless field_item_params
  params_hash = field_item_params.to_unsafe_h
  params_hash['data'] || {}
end

#field_items_attributes_paramsObject



43
44
45
46
47
48
49
50
# File 'app/helpers/cortex/content_item_helper.rb', line 43

def field_items_attributes_params
  field_items_attributes_as_array = params['content_item']['field_items_attributes'].values

  permitted_keys = {}
  field_items_attributes_as_array.each {|hash| hash.each_key {|key| permitted_keys[key.to_s] = []}}

  permit_attribute_params(field_items_attributes_as_array, permitted_keys)
end

#index_parametersObject



83
84
85
86
87
88
89
# File 'app/helpers/cortex/content_item_helper.rb', line 83

def index_parameters
  @index_parameters = {}
  params['content_item']['field_items_attributes'].each do |param|
    @index_parameters[params['content_item']['field_items_attributes'][param]['field_id']] = params['content_item']['field_items_attributes'][param]
  end
  @index_parameters
end

#params_lookupObject



91
92
93
# File 'app/helpers/cortex/content_item_helper.rb', line 91

def params_lookup
  @params_lookup ||= index_parameters
end

#permit_attribute_params(param_array, permitted_keys) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/cortex/content_item_helper.rb', line 52

def permit_attribute_params(param_array, permitted_keys)
  param_array.each do |param_hash|
    permitted_keys.keys.each do |key|
      if param_hash[key].is_a?(Hash)
        permitted_keys[key] << permit_param(param_hash[key])
      end
      permitted_keys[key].flatten!
    end
  end

  sanitize_parameters(permitted_keys)
end

#permit_param(param) ⇒ Object



65
66
67
68
69
70
71
# File 'app/helpers/cortex/content_item_helper.rb', line 65

def permit_param(param)
  if param.values[0].is_a?(Hash)
    {param.keys[0].to_sym => param.values[0].keys}
  else
    param.keys
  end
end

#sanitize_parameters(permitted_keys) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'app/helpers/cortex/content_item_helper.rb', line 73

def sanitize_parameters(permitted_keys)
  permitted_keys.map do |key, value|
    if value.empty?
      key
    else
      {key => value.uniq}
    end
  end
end

#update_content_itemObject



17
18
19
20
21
22
23
24
25
# File 'app/helpers/cortex/content_item_helper.rb', line 17

def update_content_item
  UpdateContentItemTransaction.new
    .with_step_args(
      execute_content_item_state_change: [state: params[:content_item][:state]]
    )
    .call(id: params[:id], content_type: content_type,
          content_item_params: content_item_params, current_user: current_user)
    .value!
end

#validation_message(base_message) ⇒ Object



101
102
103
104
# File 'app/helpers/cortex/content_item_helper.rb', line 101

def validation_message(base_message)
  msg_array = base_message.gsub('Validation failed:', '').gsub('Field items', '').split(',')
  msg_array.map {|message| message.strip.titleize}
end