Class: Dashboard::PagesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/blacksand/dashboard/pages_controller.rb

Instance Method Summary collapse

Instance Method Details

#children_partialObject



40
41
42
43
44
45
46
47
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 40

def children_partial
  @page = Page.where(id: params[:parent_id]).first
  @parent_id = params[:parent_id]
  @parent_page = Page.find(params[:parent_id]) if params[:parent_id]
  @q = Page.includes(:template, :prototype).where(parent_id: params[:parent_id]).order(:position, created_at: :desc).ransack(params[:q])
  @all_children = @q.result
  @pages = @all_children.page(params[:page])
end

#createObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 117

def create
  @page = Page.new(page_params)

  respond_to do |format|
    if @page.save
      format.html { redirect_to pages_url(parent_id: @page.parent_id), notice: '页面创建成功.' }
      format.json { render :show, status: :created, location: @page }
    else
      puts @page.errors.full_messages
      format.html { render :new }
      format.json { render json: @page.errors, status: :unprocessable_entity }
    end
  end
end

#defaultObject



6
7
8
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 6

def default
  redirect_to pages_url
end

#destroyObject



144
145
146
147
148
149
150
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 144

def destroy
  @page.destroy
  respond_to do |format|
    format.html { redirect_to pages_url(parent_id: @page.parent_id), notice: '成功删除.' }
    format.json { head :no_content }
  end
end

#editObject



75
76
77
78
79
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 75

def edit
  if !@page.properties.present? && @page.prototype.present?
    @page.build_properties
  end
end

#get_prototype_idObject



111
112
113
114
115
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 111

def get_prototype_id
  respond_to do |format|
    format.json { render json: @page.prototype_id }
  end
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 10

def index
  @page = Page.where(id: params[:parent_id]).first
  @parent_id = params[:parent_id] || 0
  @all_children = Page.where(parent_id: nil).order(:position)
  @pages = @all_children.page(params[:page])

  # 编辑只能看到自己负责的板块
  tree_nodes = if false && current_user.role == 'editor'
                 Page.tree_nodes(current_user.page_ids)
               else
                 Page.tree_nodes
               end

  gon.pages = [{
               text: '网站',
               href: children_partial_pages_path(parent_id: nil),
               page_id: 0,
               nodes: tree_nodes,
               }]

  # 选中父页面
  Page.selectNode(gon.pages, @parent_id.to_i)
end

#newObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 52

def new
  @page = Page.new
  if params["parent_id"].present?
    @parent_id = params[:parent_id]
    parent_page = Page.find @parent_id
    @page.parent_id = params["parent_id"]

    # Set default template
    if parent_page.preferred_child_template_name
      @page.template = Template.find_by(name: parent_page.preferred_child_template_name)
    end

    # Set default prototype
    if parent_page.preferred_child_prototype_name
      prototype = Prototype.find_by(name: parent_page.preferred_child_prototype_name)
      @page.prototype = prototype
      prototype.fields.each do |field|
        Property.build_property(@page, field)
      end
    end
  end
end

#onchange_editObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 97

def onchange_edit
  @page.properties.destroy_all
  if params[:prototype_id].present?
    @page.prototype_id = params[:prototype_id]
    @page.update_attributes(prototype_id: params[:prototype_id])
    @page.build_properties
    @page.save(validate: false)
  else
    @page.prototype_id = nil
    @page.update_attributes(prototype_id: nil)
  end
  render 'onchange_render'
end

#onchange_newObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 81

def onchange_new
  @page = Page.new

  if params["parent_id"].present?
    @page.parent_id = params["parent_id"]
  end

  if params[:prototype_id].present?
    @page.prototype_id = params[:prototype_id]
    Prototype.find(params[:prototype_id]).fields.each do |field|
      Property.build_property(@page, field)
    end
  end
  render 'onchange_render'
end

#searchObject



34
35
36
37
38
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 34

def search
  # TODO: 查询排除自己
  @q = Page.ransack(params[:q])
  @pages = @q.result.page(params[:page])
end

#showObject



49
50
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 49

def show
end

#sortObject



152
153
154
155
156
157
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 152

def sort
  params[:page].each_with_index do |id, index|
    Page.update(id, {position: index+1})
  end
  render nothing: true
end

#updateObject



132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/blacksand/dashboard/pages_controller.rb', line 132

def update
  respond_to do |format|
    if @page.update(page_params)
      format.html { redirect_to pages_url(parent_id: @page.parent_id), notice: '页面更新成功.' }
      format.json { render :show, status: :ok, location: @page }
    else
      format.html { render :edit }
      format.json { render json: @page.errors, status: :unprocessable_entity }
    end
  end
end