Class: ConstructorPages::PagesController

Inherits:
ConstructorCore::ApplicationController
  • Object
show all
Includes:
MoveHelper
Defined in:
app/controllers/constructor_pages/pages_controller.rb

Instance Method Summary collapse

Methods included from MoveHelper

#move_to

Instance Method Details

#createObject



97
98
99
100
101
102
103
104
105
# File 'app/controllers/constructor_pages/pages_controller.rb', line 97

def create
  @page = Page.new page_params

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

#destroyObject



124
125
126
127
128
129
# File 'app/controllers/constructor_pages/pages_controller.rb', line 124

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

#editObject

def search

  if params[:all].nil?
    @page = Page.first
  else
    @request = '/' + params[:all]
    @page = Page.where(:full_url => @request).first
  end

  instance_variable_set('@'[email protected]_name.to_s, @page)

  what_search = params[:what_search]
  params_selection = request.query_parameters

  template = Template.find_by_code_name(what_search.singularize)

  if template.nil?
    render :action => "error_404", :layout => false
    return
  end

  @pages = @page.descendants.where(:template_id => template.id)

  params_selection.each_pair do |code_name, value|
    if value.numeric?
      value = value.to_f
    elsif value.boolean?
      value = value.to_bool
    end

    @pages = @pages.select do |page|
      if code_name == 'name'
        page.name == value
      else
        page.field(code_name) == value
      end
    end
  end

  instance_variable_set('@'+template.code_name.pluralize, @pages)

  render :template => "templates/#{template.code_name}_search"
end


89
90
91
92
93
94
95
# File 'app/controllers/constructor_pages/pages_controller.rb', line 89

def edit
  @page = Page.find(params[:id])
  @page.template ||= Template.first
  @template_id = @page.template.id

  @multipart = @page.fields.map{|f| f.type_value == 'image'}.include?(true) ? true : false
end

#indexObject



9
10
11
12
# File 'app/controllers/constructor_pages/pages_controller.rb', line 9

def index
  @template_exists = Template.count > 0
  flash.notice = 'Create at least one template' unless @template_exists
end

#newObject



14
15
16
17
18
19
20
# File 'app/controllers/constructor_pages/pages_controller.rb', line 14

def new
  @page, @template_id, @multipart = Page.new, Template.first.id, false

  if params[:page] and (@page.parent = Page.find(params[:page]))
    @template_id = @page.parent.template.child.id
  end
end

#showObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/constructor_pages/pages_controller.rb', line 22

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

  error_404 and return if @page.nil? or !@page.active?

  redirect_to @page.link if @page.redirect?

  _code_name = @page.template.code_name.to_s

  instance_variable_set('@'+_code_name, @page)

  respond_to do |format|
    format.html { render template: "html_templates/#{_code_name}" }
    format.json {
      _template = render_to_string partial: "json_templates/#{_code_name}.json.erb", layout: false, locals: {_code_name.to_sym => @page, page: @page}
      _js = render_to_string partial: "js_partials/#{_code_name}.js"

      render json: @page, self_and_ancestors: @page.self_and_ancestors.map(&:id), template: _template.gsub(/\n/, '\\\\n'), js: _js
    }
  end
end

#updateObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/constructor_pages/pages_controller.rb', line 107

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_url, notice: t(:page_success_updated, name: @page.name)
  else
    render action: :edit
  end
end