Module: Locomotive::Extensions::Page::EditableElements

Extended by:
ActiveSupport::Concern
Included in:
Page
Defined in:
app/models/locomotive/extensions/page/editable_elements.rb

Instance Method Summary collapse

Instance Method Details

#add_or_update_editable_element(attributes, type) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 50

def add_or_update_editable_element(attributes, type)
  element = self.find_editable_element(attributes[:block], attributes[:slug])

  if element
    element.copy_attributes(attributes)
  else
    element = self.editable_elements.build(attributes, type)
  end

  element.add_current_locale

  element
end

#disable_all_editable_elementsObject



21
22
23
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 21

def disable_all_editable_elements
  self.editable_elements.each { |el| el.disabled = true }
end

#disable_parent_editable_elements(block) ⇒ Object



16
17
18
19
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 16

def disable_parent_editable_elements(block)
  Rails.logger.debug "[disable_parent_editable_elements] #{block.inspect}"
  self.editable_elements.each { |el| el.disabled = true if el.from_parent? && el.block == block }
end

#editable_element_blocksObject



25
26
27
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 25

def editable_element_blocks
  self.editable_elements.collect(&:block)
end

#editable_elements_grouped_by_blocksObject



33
34
35
36
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 33

def editable_elements_grouped_by_blocks
  groups = self.enabled_editable_elements.group_by(&:block)
  groups.delete_if { |block, elements| elements.empty? }
end

#enable_editable_elements(block) ⇒ Object



64
65
66
67
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 64

def enable_editable_elements(block)
  Rails.logger.debug "[enable_editable_elements] #{block.inspect}"
  self.editable_elements.each { |el| el.disabled = false if el.block == block }
end

#enabled_editable_elementsObject



29
30
31
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 29

def enabled_editable_elements
  self.editable_elements.by_priority.find_all(&:editable?)
end

#find_editable_element(block, slug) ⇒ Object



42
43
44
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 42

def find_editable_element(block, slug)
  self.editable_elements.detect { |el| el.block == block && el.slug == slug }
end

#find_editable_elements(block) ⇒ Object



38
39
40
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 38

def find_editable_elements(block)
  self.editable_elements.find_all { |el| el.block == block }
end

#find_editable_filesObject



46
47
48
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 46

def find_editable_files
  self.editable_elements.find_all { |el| el.respond_to?(:source) }
end

#merge_editable_elements_from_page(source) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 69

def merge_editable_elements_from_page(source)
  source.editable_elements.each do |el|
    next if el.disabled?

    existing_el = self.find_editable_element(el.block, el.slug)

    if existing_el.nil? # new one from parents
      new_el = self.editable_elements.build({}, el.class)
      new_el.copy_attributes_from(el)
    else
      existing_el.disabled = false

      # make sure the default content gets updated too
      existing_el.set_default_content_from(el)

      # only the type, hint and fixed properties can be modified from the parent element
      %w(_type hint fixed priority).each do |attr|
        existing_el.send(:"#{attr}=", el.send(attr.to_sym))
      end
    end
  end
end

#remove_disabled_editable_elementsObject



92
93
94
95
96
97
98
99
100
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 92

def remove_disabled_editable_elements
  # get only those which are fully disabled, meaning in ALL the locales
  ids = self.editable_elements.find_all { |el| el.disabled_in_all_translations? }.map(&:_id)

  return if ids.empty?

  # super fast way to remove useless elements all in once
  self.collection.update(self.atomic_selector, '$pull' => { 'editable_elements' => { '_id' => { '$in' => ids } } })
end