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.



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



109
110
111
112
113
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 109

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

#indexObject



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

#listObject



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

#newObject



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

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

#orderObject



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

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, cell_id and parent_element_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],
        parent_element_id: params[:parent_element_id],
        position: idx + 1
      )
    end
    @parent_element.try!(:touch)
  end
end

#publishObject



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

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

#trashObject

Trashes the Element instead of deleting it.



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

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
# 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)
  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