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.



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

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(create_element_params)
      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



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

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

#indexObject



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

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



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

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



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

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



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

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



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

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

#trashObject

Trashes the Element instead of deleting it.



88
89
90
91
# File 'app/controllers/alchemy/admin/elements_controller.rb', line 88

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

#updateObject

Updates the element.

And update all contents in the elements by calling update_contents.



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

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