Class: ConstructorPages::PagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/constructor_pages/pages_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

movable, #move_to

Instance Method Details

#createObject


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/constructor_pages/pages_controller.rb', line 44

def create
  @page = Page.new page_params

  if @page.save
    @page.touch_branch
    redirect_to pages_path, notice: t(:page_success_added, name: @page.name)
  else
    if @page.template_id
      _template = Template.find(@page.template_id)
      _code_name = _template.code_name.pluralize if _template
      render "#{_code_name}/new" rescue render :new
    else
      render :new
    end
  end
end

#destroyObject


79
80
81
82
83
84
# File 'app/controllers/constructor_pages/pages_controller.rb', line 79

def destroy
  @page = Page.find(params[:id])
  @page.touch_branch
  @page.destroy
  redirect_to pages_path, notice: t(:page_success_removed, name: @page.name)
end

#editObject


37
38
39
40
41
42
# File 'app/controllers/constructor_pages/pages_controller.rb', line 37

def edit
  @page = Page.find(params[:id])
  @parent_id, @template_id = @page.parent.try(:id), @page.template.id
  _code_name = @page.template.code_name.pluralize
  render "#{_code_name}/edit" rescue render :edit
end

#indexObject


11
12
13
14
15
16
# File 'app/controllers/constructor_pages/pages_controller.rb', line 11

def index
  @pages = Page.includes(:template).load
  @pages_cache = Digest::MD5.hexdigest(@pages.map{|p| [p.id, p.name, p.full_url, p.in_url, p.template.lft, p.lft, p.template_id]}.join)
  @template_exists = Template.count != 0
  flash[:notice] = 'Create at least one template' unless @template_exists
end

#newObject


18
19
20
# File 'app/controllers/constructor_pages/pages_controller.rb', line 18

def new
  @page = Page.new
end

#showObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/constructor_pages/pages_controller.rb', line 22

def show
  _request = "/#{params[:all]}"
  @page = Page.find_by_request_or_first(_request)
  error_404 and return if @page.nil? or (!@page.published? and _request != '/')
  redirect_to(@page.redirect) && return if @page.redirect?
  _code_name = @page.template.code_name
  instance_variable_set('@'+_code_name, @page)

  respond_to do |format|
    format.html { render "#{_code_name.pluralize}/show" rescue render "templates/#{_code_name}"}
    format.json { render "#{_code_name.pluralize}/show.json", layout: false rescue render json: @page }
    format.xml  { render "#{_code_name.pluralize}/show.xml",  layout: false rescue render xml: @page }
  end
end

#updateObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/constructor_pages/pages_controller.rb', line 61

def update
  @page = Page.find params[:id]

  _template_changed = @page.template.id != params[:page][:template_id].to_i

  @page.remove_fields_values if _template_changed

  if @page.update page_params
    @page.create_fields_values if _template_changed
    @page.update_fields_values params[:fields]
    @page.touch_branch

    redirect_to pages_path, notice: t(:page_success_updated, name: @page.name)
  else
    render "#{@page.template.code_name.pluralize}/new" rescue render :edit
  end
end