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



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 54

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

#change_element_type(element, klass) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 99

def change_element_type(element, klass)
  # build the new element
  self.editable_elements.build({}, klass).tap do |new_element|
    # copy the most important mongoid internal attributes
    %w{_id _index new_record}.each do |attr|
      new_element.send(:"#{attr}=", element.send(attr.to_sym))
    end

    # copy the main attributes from the previous version
    new_element.attributes = element.attributes.reject { |attr| !%w(slug block from_parent).include?(attr) }

    # remove the old one
    self.editable_elements.delete_one(element)
  end
end

#disable_all_editable_elementsObject



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

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

#disable_parent_editable_elements(block) ⇒ Object



16
17
18
19
20
21
22
23
# 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 do |el|
    if el.from_parent? && (el.block == block || el.block =~ %r(^#{block}/))
      el.disabled = true
    end
  end
end

#editable_element_blocksObject



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

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

#editable_elements_grouped_by_blocksObject



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

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



68
69
70
71
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 68

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



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

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

#find_editable_element(block, slug) ⇒ Object



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

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

#find_editable_elements(block) ⇒ Object



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

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

#find_editable_filesObject



50
51
52
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 50

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

#merge_editable_elements_from_page(source) ⇒ Object



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/models/locomotive/extensions/page/editable_elements.rb', line 73

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)
    elsif existing_el.from_parent? # it inherits from a parent page
      existing_el.disabled = false

      # same type ? if not, convert it
      if existing_el._type != el._type
        existing_el = self.change_element_type(existing_el, el.class)
      end

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

      # copy _type, hint, fixed, priority and locales + type custom attributes
      existing_el.copy_default_attributes_from(el)
    end
  end
end

#remove_disabled_editable_elementsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/locomotive/extensions/page/editable_elements.rb', line 115

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.find(self.atomic_selector).update('$pull' => { 'editable_elements' => { '_id' => { '$in' => ids } } })

  # mark them as destroyed
  self.editable_elements.each do |el|
    next unless ids.include?(el._id)
    el.destroyed = true
  end
end