Class: Concen::PagesController

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

Instance Method Summary collapse

Instance Method Details

#createObject



21
22
23
24
25
26
27
28
29
# File 'app/controllers/concen/pages_controller.rb', line 21

def create
  @page = Page.new(params[:concen_page])
  @page.authors = [current_concen_user.username]
  if @page.save
    redirect_to(edit_concen_page_path(@page), :notice => "Page was successfully created.")
  else
    render :new
  end
end

#destroyObject



52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/concen/pages_controller.rb', line 52

def destroy
  if current_concen_user.full_control || @page.authors_as_user.include?(current_concen_user)
    @page = Page.find(params[:id])
    @page.destroy
    redirect_to concen_pages_path
  else
    flash[:notice] = "Only author can delete page."
    redirect_to concen_pages_path
  end
end

#editObject



31
32
33
34
35
36
37
# File 'app/controllers/concen/pages_controller.rb', line 31

def edit
  @page = Page.find(params[:id])
  unless current_concen_user.full_control || @page.authors_as_user.include?(current_concen_user)
    flash[:notice] = "Only author can edit page."
    redirect_to concen_pages_path
  end
end

#indexObject



7
8
9
10
# File 'app/controllers/concen/pages_controller.rb', line 7

def index
  @page_title = "Pages"
  @page = Page.where(:level => 0).first
end

#newObject



12
13
14
15
16
17
18
19
# File 'app/controllers/concen/pages_controller.rb', line 12

def new
  if params[:parent_id]
    @page = Page.find(params[:parent_id]).children.build
  else
    @page = Page.new
  end
  @page.level = params[:level].to_i if params[:level]
end

#sortObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/concen/pages_controller.rb', line 63

def sort
  if params[:page]
    if current_concen_user.full_control
      child_count = {}
      params[:page].each do |key, value|
        if value == "root"
          root = Page.find(key)
          root.parent_id = nil
          root.level = 0
          root.save
        else
          child = Page.find(key)
          parent = Page.find(value)
          child.parent_id = parent.id
          child.level = parent.level + 1
          if child_count[value]
            child.position = child_count[value] + 1
            child_count[value] += 1
          else
            child.position = 1
            child_count[value] = 1
          end
          child.save
        end
      end
      render :json => {:success => true}
    else
      render :json => {:success => false, :message => "Only user with full control can sort pages."}
    end
  end
end

#updateObject



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

def update
  @page = Page.find(params[:id])
  if current_concen_user.full_control || @page.authors_as_user.include?(current_concen_user)
    if @page.update_attributes(params[:concen_page])
      redirect_to(edit_concen_page_path(@page), :notice => "Page was successfully created.")
    else
      render :edit
    end
  else
    render :edit, :notice => "Only author can modify page content."
  end
end