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



41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/constructor_pages/pages_controller.rb', line 41

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
    render :new
  end
end

#destroyObject



70
71
72
73
74
75
# File 'app/controllers/constructor_pages/pages_controller.rb', line 70

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



36
37
38
39
# File 'app/controllers/constructor_pages/pages_controller.rb', line 36

def edit
  @page = Page.find(params[:id])
  @parent_id, @template_id = @page.parent.try(:id), @page.template.id
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.name, p.url, 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

#new_childObject



22
23
24
25
# File 'app/controllers/constructor_pages/pages_controller.rb', line 22

def new_child
  @page, @parent_id = Page.new, params[:id]
  @template_id = Page.find(@parent_id).try(:template).child.id
end

#showObject



27
28
29
30
31
32
33
34
# File 'app/controllers/constructor_pages/pages_controller.rb', line 27

def show
  @page = Page.find_by_request_or_first("/#{params[:all]}")
  error_404 and return unless @page.try(:published?)
  redirect_to @page.link if @page.redirect?
  _code_name = @page.template.code_name
  instance_variable_set('@'+_code_name, @page)
  render template: "templates/#{_code_name}"
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/constructor_pages/pages_controller.rb', line 52

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 :edit
  end
end