Class: Pwb::PagePartManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/pwb/page_part_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_part_key, container) ⇒ PagePartManager

Returns a new instance of PagePartManager.



5
6
7
8
9
10
11
12
13
# File 'app/services/pwb/page_part_manager.rb', line 5

def initialize(page_part_key, container)
  raise "Please provide valid container" unless container.present?
  self.page_part_key = page_part_key
  # container can be either a page or the website
  self.container = container
  self.page_part = container.get_page_part page_part_key
  # PagePart.find_by_page_part_key page_part_key
  raise "Please provide valid page_part_key" unless page_part.present?
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



3
4
5
# File 'app/services/pwb/page_part_manager.rb', line 3

def container
  @container
end

#page_partObject

Returns the value of attribute page_part.



3
4
5
# File 'app/services/pwb/page_part_manager.rb', line 3

def page_part
  @page_part
end

#page_part_keyObject

Returns the value of attribute page_part_key.



3
4
5
# File 'app/services/pwb/page_part_manager.rb', line 3

def page_part_key
  @page_part_key
end

Instance Method Details

#find_or_create_contentObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/pwb/page_part_manager.rb', line 15

def find_or_create_content
  # sets up the connection between a container and content model
  # ensuring the intermediate page_content join is created too
  page_content_join_model = find_or_create_join_model
  unless page_content_join_model.content.present?
    page_content_join_model.create_content(page_part_key: page_part_key)
    # without calling save! below, content and page_content will not be associated
    page_content_join_model.save!
  end
  page_content_join_model.content
  # just creating contents like below will result in join_model without page_part_key
  # page_fragment_content = container.contents.find_or_create_by(page_part_key: page_part_key)
end

#find_or_create_join_modelObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/pwb/page_part_manager.rb', line 29

def find_or_create_join_model
  # Particularly important for rails_parts
  # They may may not have content to seed but this
  # call is needed when seeding to set up r/n between
  # container (page or website) and content
  page_content_join_model = container.page_contents.find_or_create_by(page_part_key: page_part_key)
  if page_part.is_rails_part
    page_content_join_model.is_rails_part = true
    page_content_join_model.save!
  end
  page_content_join_model
end

#get_seed_content(locale) ⇒ Object

TODO: Use below for page_part_content_spec

Raises:

  • (Exception)


43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/pwb/page_part_manager.rb', line 43

def get_seed_content(locale)
  locale_seed_file = Pwb::Engine.root.join('db', 'yml_seeds', 'content_translations', locale.to_s + '.yml')
  raise Exception, "Contents seed for #{locale} not found" unless File.exist? locale_seed_file
  yml = YAML.load_file(locale_seed_file)

  if yml[locale] && yml[locale][container_label] && yml[locale][container_label][page_part_key]
    seed_content = yml[locale][container_label][page_part_key]
    page_part_manager.seed_container_block_content locale, seed_content
    p "#{container_label} #{page_part_key} content set for #{locale}."
  end
end

#seed_container_block_content(locale, seed_content) ⇒ Object

seed_content is …



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/pwb/page_part_manager.rb', line 56

def seed_container_block_content(locale, seed_content)
  page_part_editor_setup = page_part.editor_setup
  raise "Invalid editorBlocks for page_part_editor_setup" unless page_part_editor_setup && page_part_editor_setup["editorBlocks"].present?
  # page = page_part.page
  # page_part_key uniquely identifies a fragment
  # page_part_key = page_part.page_part_key

  # container for json to be attached to page details
  locale_block_content_json = {"blocks" => {}}
  # {"blocks"=>{"title_a"=>{"content"=>"about our agency"}, "content_a"=>{"content"=>""}}}
  page_part_editor_setup["editorBlocks"].each do |configColBlocks|
    configColBlocks.each do |configRowBlock|
      row_block_label = configRowBlock["label"]
      row_block_content = ""
      # find the content for current block from within the seed content
      if seed_content[row_block_label]
        if configRowBlock["isImage"]
          photo = seed_fragment_photo row_block_label, seed_content[row_block_label]
          if photo.present? && photo.optimized_image_url.present?
            # optimized_image_url is defined in content_photo and will
            # return cloudinary url or filesystem url depending on settings
            row_block_content = photo.optimized_image_url
          else
            row_block_content = "http://via.placeholder.com/350x250"
          end
        else
          row_block_content = seed_content[row_block_label]
        end
      end
      locale_block_content_json["blocks"][row_block_label] = {"content" => row_block_content}
    end
  end
  # # save the block contents (in associated page_part model)
  # updated_details = container.set_page_part_block_contents page_part_key, locale, locale_block_content_json
  # # retrieve the contents saved above and use to rebuild html for that page_part
  # # (and save it in associated page_content model)
  # fragment_html = container.rebuild_page_content page_part_key, locale

  update_page_part_content locale, locale_block_content_json

  p " #{page_part_key} content set for #{locale}."
end

#set_default_page_content_order_and_visibilityObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/services/pwb/page_part_manager.rb', line 108

def set_default_page_content_order_and_visibility
  join_model = get_join_model
  unless join_model
    puts "Unable to set_default_page_content_order_and_visibility for #{page_part_key}"
    return
  end
  page_part_editor_setup = page_part.editor_setup
  # page = page_part.page
  # # page_part_key uniquely identifies a fragment
  # page_part_key = page_part.page_part_key

  sort_order = page_part_editor_setup["default_sort_order"] || 1
  join_model.sort_order = sort_order

  visible_on_page = false
  if page_part_editor_setup["default_visible_on_page"]
    visible_on_page = true
  end
  join_model.visible_on_page = visible_on_page
  join_model.save!
end

#update_page_part_content(locale, fragment_block) ⇒ Object



99
100
101
102
103
104
105
106
# File 'app/services/pwb/page_part_manager.rb', line 99

def update_page_part_content(locale, fragment_block)
  # save the block contents (in associated page_part model)
  json_fragment_block = set_page_part_block_contents page_part_key, locale, fragment_block
  # retrieve the contents saved above and use to rebuild html for that page_part
  # (and save it in associated page_content model)
  fragment_html = rebuild_page_content locale
  { json_fragment_block: json_fragment_block, fragment_html: fragment_html }
end