Class: ConstructorPages::PagesController

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

Instance Method Summary collapse

Instance Method Details

#create ⇒ Object



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

def create
  @page = Page.new params[:page]

  if @page.save
    redirect_to pages_url, :notice => "Страница «#{@page.name}» успешно добавлена."
  else
    render :action => "new"
  end
end

#destroy ⇒ Object



174
175
176
177
178
179
# File 'app/controllers/constructor_pages/pages_controller.rb', line 174

def destroy
  @page = Page.find(params[:id])
  title = @page.name
  @page.destroy
  redirect_to pages_url, :notice => "Страница «#{title}» успешно удалена."
end

#edit ⇒ Object



116
117
118
119
120
121
122
# File 'app/controllers/constructor_pages/pages_controller.rb', line 116

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

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

#index ⇒ Object

TODO



16
17
18
# File 'app/controllers/constructor_pages/pages_controller.rb', line 16

def index
  @user_signed_in = user_signed_in?
end

#move_down ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'app/controllers/constructor_pages/pages_controller.rb', line 191

def move_down
  from = Page.find(params[:id])
  rs = from.right_sibling
  if not rs.nil? and from.move_possible?(rs)
    from.move_right
  end

  redirect_to :back
end

#move_up ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'app/controllers/constructor_pages/pages_controller.rb', line 181

def move_up
  from = Page.find(params[:id])
  ls = from.left_sibling
  if not ls.nil? and from.move_possible?(ls)
    from.move_left
  end

  redirect_to :back
end

#new ⇒ Object



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

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

  if params[:page]
    @parent = Page.find(params[:page])
    @page.parent_id = @parent.id

    if @parent.template.child_id.nil? and !@parent.template.leaf?
      @template = @parent.template.descendants.first.id
    else
      @template = @parent.template.child_id
    end
  end
end

#search ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/constructor_pages/pages_controller.rb', line 70

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

  instance_variable_set('@'+@page.template.code_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)

  @children_of_current_root = Page.children_of(@page.root)
  @children_of_current_page = Page.children_of(@page)

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

#show ⇒ Object



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
67
68
# File 'app/controllers/constructor_pages/pages_controller.rb', line 37

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

  if @page.nil? or !@page.active
    render :action => "error_404", :layout => false
    return
  end

  if @page.url != @page.link and !@page.link.empty?
    redirect_to @page.link
  end

  instance_variable_set('@'+@page.template.code_name.to_s, @page)

  @children_of_current_root = Page.children_of(@page.root)
  @children_of_current_page = Page.children_of(@page)

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

      render :json => @page, :self_and_ancestors => @page.self_and_ancestors.map{|a| a.id}, :template => _template.gsub(/\n/, '\\\\n'), :js => _js
    }
  end
end

#update ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/constructor_pages/pages_controller.rb', line 134

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

  if @page.template.id != params[:page][:template_id].to_i
    @page.template.fields.each do |field|
      "constructor_pages/types/#{field.type_value}_type".classify.constantize.destroy_all(
          :field_id => field.id,
          :page_id => @page.id)
    end
  end

  if @page.update_attributes params[:page]

    @page.template.fields.each do |field|
      f = "constructor_pages/types/#{field.type_value}_type".classify.constantize.where(
          :field_id => field.id,
          :page_id => @page.id).first_or_create

      if params[:fields]
        f.value = 0 if field.type_value == 'boolean'

        if params[:fields][field.type_value]
          if field.type_value == "date"
            value = params[:fields][field.type_value][field.id.to_s]
            f.value = Date.new(value["date(1i)"].to_i, value["date(2i)"].to_i, value["date(3i)"].to_i).to_s
          else
            f.value = params[:fields][field.type_value][field.id.to_s]
          end
        end

        f.save
      end
    end

    redirect_to pages_url, :notice => "Страница «#{@page.name}» успешно обновлена."
  else
    render :action => "edit"
  end
end