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



55
56
57
58
59
60
61
62
63
# File 'app/controllers/constructor_pages/pages_controller.rb', line 55

def create
  @page = Page.new page_params

  if @page.save
    redirect_to pages_path, notice: t(:page_success_added, name: @page.name)
  else
    render :new
  end
end

#destroyObject



82
83
84
85
# File 'app/controllers/constructor_pages/pages_controller.rb', line 82

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

#editObject



50
51
52
53
# File 'app/controllers/constructor_pages/pages_controller.rb', line 50

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.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

#searchObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/constructor_pages/pages_controller.rb', line 36

def search
  @page = Page.find_by_request_or_first("/#{params[:all]}")

  _params = request.query_parameters
  _params.each_pair {|k,v| v || (_params.delete(k); next)
    _params[k] = v.numeric? ? v.to_f : (v.to_boolean if v.boolean?)}

  @pages = Page.in(@page).by(_params).search(params[:what_search])

  @page.template.code_name.tap {|c| [c.pluralize, c.singularize].each {|name|
    instance_variable_set('@'+name, @pages)}
    render template: "templates/#{c}_search"}
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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/constructor_pages/pages_controller.rb', line 65

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]

    redirect_to pages_path, notice: t(:page_success_updated, name: @page.name)
  else
    render :edit
  end
end