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

#create ⇒ Object

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



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

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

#fold ⇒ Object



101
102
103
104
105
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 101

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

#index ⇒ Object



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

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

#list ⇒ Object



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

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

#new ⇒ Object



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

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

#order ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 86

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

#trash ⇒ Object

Trashes the Element instead of deleting it.



81
82
83
84
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 81

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

#update ⇒ Object

Updates the element.

And update all contents in the elements by calling update_contents.



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

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