Class: Alchemy::Admin::ElementsController

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

Instance Method Summary collapse

Methods included from Clipboard

#clipboard_empty?

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



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

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
# 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, status: 201
  else
    @element.page_version = @page_version
    @elements = @page.available_element_definitions
    render :new, status: 422
  end
end

#destroyObject



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

def destroy
  @element.destroy

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

#expandObject

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



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

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.folded_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



14
15
16
17
18
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 14

def index
  elements = @page_version.elements.order(:position).includes(*element_includes)
  @elements = elements.not_nested.unfixed
  @fixed_elements = elements.not_nested.fixed
end

#newObject



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

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

#orderObject



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

def order
  @element = Element.find(params[:element_id])

  # Update position
  @element.parent_element_id = params[:parent_element_id]
  @element.position = params[:position]

  # Skip validations when updating position, since new records may not yet meet all
  # validation requirements.
  @element.save(validate: false)

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

#publishObject



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

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)
  }.merge(pagePublicationData(@element.page))
end

#updateObject

Updates the element and all ingredients in the element.



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

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.filter_map do |ingredient|
        if ingredient.settings[:anchor]
          {
            ingredientId: ingredient.id,
            active: ingredient.dom_id.present?
          }
        end
      end
    }.merge(pagePublicationData(@element.page))
  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: 422
  end
end