Class: Cms::PageComponent

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
app/models/cms/page_component.rb

Overview

Handles the conversion from Mercury editor (i.e. the JSON hash it submits) to a Cms Page and blocks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_id, params) ⇒ PageComponent

Returns a new instance of PageComponent.



9
10
11
12
13
14
# File 'app/models/cms/page_component.rb', line 9

def initialize(page_id, params)
  params = HashWithIndifferentAccess.new(params)
  self.page_title = params[:page_title]
  self.blocks = params[:blocks] ? params[:blocks] : []
  self.page_id = page_id
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



7
8
9
# File 'app/models/cms/page_component.rb', line 7

def blocks
  @blocks
end

#page_idObject

Returns the value of attribute page_id.



7
8
9
# File 'app/models/cms/page_component.rb', line 7

def page_id
  @page_id
end

#page_titleObject

Returns the value of attribute page_title.



7
8
9
# File 'app/models/cms/page_component.rb', line 7

def page_title
  @page_title
end

Instance Method Details

#convert_mercury_params_to_assignment_hash(block_id, block_type) ⇒ Object



34
35
36
37
38
39
40
41
# File 'app/models/cms/page_component.rb', line 34

def convert_mercury_params_to_assignment_hash(block_id, block_type)
  block_attribute_names = block_type[1][block_id].keys
  assignment_hash = {}
  block_attribute_names.each do |attr_name|
    assignment_hash[attr_name] = block_type[1][block_id][attr_name][:value]
  end
  assignment_hash
end

#saveObject

Save the change to the underlying page (and its content)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/cms/page_component.rb', line 18

def save
  @page = Page.find(@page_id)
  @page.title = page_title[:value]
  blocks.each do |block_type|
    content_block_class = block_type[0]
    content_ids = block_type[1].keys

    content_ids.each do |block_id|
      block = content_block_class.constantize.find(block_id)
      assignment_hash = convert_mercury_params_to_assignment_hash(block_id, block_type)
      block.update_attributes(assignment_hash)
    end
  end
  @page.save
end