Class: Alchemy::Admin::ElementsController

Inherits:
BaseController show all
Defined in:
app/controllers/alchemy/admin/elements_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#leave

Methods included from Modules

included, #module_definition_for, register_module

Methods included from Alchemy::AbilityHelper

#current_ability

Methods included from ConfigurationMethods

#configuration, #multi_language?, #multi_site?, #prefix_locale?

Instance Method Details

#collapseObject

Collapses the element, all nested elements and persists the state in the db



117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 117

def collapse
  # We do not want to trigger the touch callback or any validations
  @element.update_columns(folded: true)
  # Collapse all nested elements
  nested_elements_ids = collapse_nested_elements_ids(@element)
  Alchemy::Element.where(id: nested_elements_ids).update_all(folded: true)

  render json: {
    nestedElementIds: nested_elements_ids,
    title: Alchemy.t(@element.folded? ? :show_element_content : :hide_element_content)
  }
end

#createObject

Creates a element as discribed in config/alchemy/elements.yml on page via AJAX.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 28

def create
  @page_version = PageVersion.find(params[:element][:page_version_id])
  @page = @page_version.page
  Element.transaction do
    @paste_from_clipboard = params[:paste_from_clipboard].present?
    @element = if @paste_from_clipboard
      paste_element_from_clipboard
    else
      Element.new(create_element_params)
    end
    if @page.definition["insert_elements_at"] == "top"
      @insert_at_top = true
      @element.position = 1
    end
  end
  if @element.save
    render :create
  else
    @element.page_version = @page_version
    @elements = @page.available_element_definitions
    load_clipboard_items
    render :new
  end
end

#destroyObject



82
83
84
85
86
87
88
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 82

def destroy
  @element.destroy

  render json: {
    message: Alchemy.t("Successfully deleted element") % {element: @element.display_name}
  }
end

#expandObject

Expands the element, all parents and persists the state in the db



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 132

def expand
  # We do not want to trigger the touch callback or any validations
  @element.update_columns(folded: false)
  # We want to expand the upper most parent first in order to prevent
  # re-painting issues in the browser
  parent_element_ids = @element.parent_element_ids.reverse
  Alchemy::Element.where(id: parent_element_ids).update_all(folded: false)

  render json: {
    parentElementIds: parent_element_ids,
    title: Alchemy.t(@element.folded? ? :show_element_content : :hide_element_content)
  }
end

#indexObject



9
10
11
12
13
14
15
16
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 9

def index
  @page_version = PageVersion.find(params[:page_version_id])
  @page = @page_version.page
  elements = @page_version.elements.order(:position).includes(*element_includes)
  @elements = elements.not_nested.unfixed
  @fixed_elements = elements.not_nested.fixed
  load_clipboard_items
end

#newObject



18
19
20
21
22
23
24
25
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 18

def new
  @page_version = PageVersion.find(params[:page_version_id])
  @page = @page_version.page
  @parent_element = Element.find_by(id: params[:parent_element_id])
  @elements = @page.available_elements_within_current_scope(@parent_element)
  @element = @page_version.elements.build
  load_clipboard_items
end

#orderObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 99

def order
  @element = Element.find(params[:element_id])
  @element.update(
    parent_element_id: params[:parent_element_id],
    position: params[:position]
  )
  if params[:parent_element_id].present?
    @parent_element = Element.find_by(id: params[:parent_element_id])
  end

  render json: {
    message: Alchemy.t(:successfully_saved_element_position),
    preview_text: @element.preview_text
  }
end

#publishObject



90
91
92
93
94
95
96
97
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 90

def publish
  @element.public = !@element.public?
  @element.save(validate: false)
  render json: {
    public: @element.public?,
    label: @element.public? ? Alchemy.t(:hide_element) : Alchemy.t(:show_element)
  }
end

#updateObject

Updates the element and all ingredients in the element.



55
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
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 55

def update
  if @element.update(element_params)
    render json: {
      notice: Alchemy.t(:element_saved),
      previewText: Rails::Html::SafeListSanitizer.new.sanitize(@element.preview_text),
      ingredientAnchors: @element.ingredients.select { |i| i.settings[:anchor] }.map do |ingredient|
        {
          ingredientId: ingredient.id,
          active: ingredient.dom_id.present?
        }
      end
    }
  else
    @warning = Alchemy.t("Validation failed")
    render json: {
      warning: @warning,
      errorMessage: Alchemy.t(:ingredient_validations_headline),
      ingredientsWithErrors: @element.ingredients_with_errors.map do |ingredient|
        {
          id: ingredient.id,
          errorMessage: ingredient.errors.messages[:value].to_sentence
        }
      end
    }, status: :unprocessable_entity
  end
end