Module: Cortex::ContentItemHelper

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

Instance Method Summary collapse

Instance Method Details

#clean_error_messages(messages) ⇒ Object



111
112
113
# File 'app/helpers/cortex/content_item_helper.rb', line 111

def clean_error_messages(messages)
  messages.map {|message| message.gsub('Field items', '').strip.titleize}
end

#content_item_paramsObject



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

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



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

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
# File 'app/helpers/cortex/content_item_helper.rb', line 7

def create_content_item
  CreateContentItemTransaction.new
end

#field_item_param_data(field_item_params) ⇒ Object



105
106
107
108
109
# File 'app/helpers/cortex/content_item_helper.rb', line 105

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



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

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



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

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



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

def params_lookup
  @params_lookup ||= index_parameters
end

#permit_attribute_params(param_array, permitted_keys) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/cortex/content_item_helper.rb', line 62

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



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

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

#render_create_content_item_errorObject



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

def render_create_content_item_error
  @content_item = content_item_reload(content_type.content_items.new)
  @wizard = wizard_decorator(@content_item.content_type)

  add_breadcrumb content_type.name.pluralize, :content_type_content_items_path
  add_breadcrumb 'New'

  render :new
end

#render_update_content_item_errorObject



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

def render_update_content_item_error
  @content_item = content_item_reload(content_type.content_items.find_by_id(params[:id]))
  @wizard = wizard_decorator(@content_item.content_type)

  title = @content_item.field_items.find { |field_item| field_item.field.name == 'Title' }.data['text'] # TODO: refactor this hardcoded Field reference
  add_breadcrumb content_type.name.pluralize, :content_type_content_items_path
  add_breadcrumb title
  add_breadcrumb 'Edit'

  render :edit
end

#sanitize_parameters(permitted_keys) ⇒ Object



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

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



11
12
13
# File 'app/helpers/cortex/content_item_helper.rb', line 11

def update_content_item
  UpdateContentItemTransaction.new
end