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 ConfigurationMethods

#configuration, #multi_language?, #multi_site?

Instance Method Details

#createObject

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 35

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
      @cell = @element.cell
    else
      @element = Element.new_from_scratch(params[:element])
      if @page.can_have_cells?
        @cell = find_or_create_cell
        @element.cell = @cell
      end
      @element.save
    end
    if @page.definition['insert_elements_at'] == 'top'
      @insert_at_top = true
      @element.move_to_top
    end
  end
  @cell_name = @cell.nil? ? "for_other_elements" : @cell.name
  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



105
106
107
108
109
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 105

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

#indexObject



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

def index
  @page = Page.find(params[:page_id])
  @cells = @page.cells
  if @cells.blank?
    @elements = @page.elements.not_trashed
  else
    @elements = @page.elements_grouped_by_cells
  end
end

#listObject



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

def list
  @page_id = params[:page_id]
  if @page_id.blank? && !params[:page_urlname].blank?
    @page_id = Language.current.pages.find_by(urlname: params[:page_urlname]).id
  end
  @elements = Element.published.where(page_id: @page_id)
end

#newObject



26
27
28
29
30
31
32
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 26

def new
  @page = Page.find_by_id(params[:page_id])
  @element = @page.elements.build
  @elements = @page.available_element_definitions
  @clipboard = get_clipboard('elements')
  @clipboard_items = Element.all_from_clipboard_for_page(@clipboard, @page)
end

#orderObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 90

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

#trashObject

Trashes the Element instead of deleting it.



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

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

#updateObject

Updates the element.

And update all contents in the elements by calling update_contents.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 70

def update
  if @element.update_contents(contents_params)
    @page = @element.page
    @element_validated = @element.update_attributes!(element_params)

    Rails.logger.info "[ALCHEMY_LOCALE] updating contents for element in elements_controller#update: #{@element}"
    update_translations(@element) if @element.page.language.language_code == 'en'
  else
    @element_validated = false
    @notice = _t('Validation failed')
    @error_message = "<h2>#{@notice}</h2><p>#{_t(:content_validations_headline)}</p>".html_safe
  end
end