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

#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



66
67
68
69
70
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 66

def destroy
  @richtext_ids = @element.richtext_ingredients_ids
  @element.destroy
  @notice = Alchemy.t("Successfully deleted element") % {element: @element.display_name}
end

#foldObject

Toggle folds the element and persists the state in the db



95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 95

def fold
  @page = @element.page
  # We do not want to trigger the touch callback or any validations
  @element.update_columns(folded: !@element.folded)
  # Fold all nested elements if folded
  if @element.folded?
    ids = collapse_nested_elements_ids(@element)
    Alchemy::Element.where(id: ids).update_all(folded: true)
  end
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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 76

def order
  @parent_element = Element.find_by(id: params[:parent_element_id])
  Element.transaction do
    params.fetch(:element_ids, []).each.with_index(1) do |element_id, position|
      # We need to set the parent_element_id, because we might have dragged the
      # element over from another nestable element
      Element.find_by(id: element_id).update_columns(
        parent_element_id: params[:parent_element_id],
        position: position
      )
    end
    # Need to manually touch the parent because Rails does not do it
    # with the update_columns above
    @parent_element&.touch
  end
end

#publishObject



72
73
74
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 72

def publish
  @element.update(public: !@element.public?)
end

#updateObject

Updates the element and all ingredients in the element.



55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 55

def update
  @page = @element.page

  if @element.update(element_params)
    @element_validated = true
  else
    element_update_error
    @error_messages = @element.ingredient_error_messages
  end
end