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.



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

def create
  @page = Page.find(params[:element][:page_id])
  Element.transaction do
    if @paste_from_clipboard = params[:paste_from_clipboard].present?
      @element = paste_element_from_clipboard
    else
      @element = Element.create(create_element_params)
    end
    if @page.definition["insert_elements_at"] == "top"
      @insert_at_top = true
      @element.move_to_top
    end
  end
  if @element.valid?
    render :create
  else
    @element.page = @page
    @elements = @page.available_element_definitions
    @clipboard = get_clipboard("elements")
    @clipboard_items = Element.all_from_clipboard_for_page(@clipboard, @page)
    render :new
  end
end

#foldObject



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

def fold
  @page = @element.page
  @element.folded = !@element.folded
  @element.save
end

#indexObject



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

def index
  @page = Page.find(params[:page_id])
  @elements = @page.all_elements.not_nested.unfixed.not_trashed.includes(*element_includes)
  @fixed_elements = @page.all_elements.fixed.not_trashed.includes(*element_includes)
end

#newObject



15
16
17
18
19
20
21
22
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 15

def new
  @page = Page.find(params[:page_id])
  @parent_element = Element.find_by(id: params[:parent_element_id])
  @elements = @page.available_elements_within_current_scope(@parent_element)
  @element = @page.elements.build
  @clipboard = get_clipboard("elements")
  @clipboard_items = Element.all_from_clipboard_for_page(@clipboard, @page)
end

#orderObject



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

def order
  @trashed_element_ids = Element.trashed.where(id: params[:element_ids]).pluck(:id)
  @parent_element = Element.find_by(id: params[:parent_element_id])
  Element.transaction do
    params.fetch(:element_ids, []).each_with_index do |element_id, idx|
      # Ensure to set page_id and parent_element_id to the current
      # because of trashed elements could still have old values
      Element.where(id: element_id).update_all(
        page_id: params[:page_id],
        parent_element_id: params[:parent_element_id],
        position: idx + 1,
      )
    end
    @parent_element.try!(:touch)
  end
end

#publishObject



64
65
66
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 64

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

#trashObject

Trashes the Element instead of deleting it.



69
70
71
72
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 69

def trash
  @page = @element.page
  @element.trash!
end

#updateObject

Updates the element.

And update all contents in the elements by calling update_contents.



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

def update
  if @element.update_contents(contents_params)
    @page = @element.page
    @element_validated = @element.update(element_params)
  else
    @element_validated = false
    @notice = Alchemy.t("Validation failed")
    @error_message = "<h2>#{@notice}</h2><p>#{Alchemy.t(:content_validations_headline)}</p>".html_safe
  end
end