Skip to content

Commit

Permalink
add summary for the nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
brickgao committed Aug 13, 2016
1 parent a66b403 commit 9fe5483
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
9 changes: 8 additions & 1 deletion app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ button {
}
}

.nodes-summary {
.nodes-list {
font-size: 80%;
ul {
text-align: left;
Expand All @@ -277,6 +277,13 @@ button {
}
}

.node {
.node-summary {
margin: 10px 0;
font-size: 80%;
}
}

.control-pad {
display: block;
text-align: center;
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ class PostsController < ApplicationController

def index
@query_page = (params[:page] || '1').to_i
@query_node = params[:node_id].to_i if params[:node_id]
conditions = {}
conditions[:user] = User.find(params[:user_id]) if params[:user_id]
conditions[:node] = Node.find(params[:node_id]) if params[:node_id]
@query_user = conditions[:user] = User.find(params[:user_id]) if params[:user_id]
@query_node = conditions[:node] = Node.find(params[:node_id]) if params[:node_id]
@pages_total = (Post.where(conditions).count / posts_limit.to_f).ceil
@posts = Post.order(id: :desc).where(conditions).limit(posts_limit).offset((@query_page - 1) * posts_limit)
@nodes = Node.all
Expand Down
1 change: 1 addition & 0 deletions app/models/node.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Node < ActiveRecord::Base
has_many :posts
validates :name, presence: true
validates :summary, presence: true
end
28 changes: 17 additions & 11 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
<div class="posts">
<div class="nodes-summary">
<ul>
<% @nodes.each do |node| %>
<% if node.id == @query_node %>
<li class="selected">
<% else %>
<li>
<% if @query_user.nil? && @query_node.nil? %>
<div class="nodes-list">
<ul>
<% @nodes.each do |node| %>
<li>
<span><a href="/?node_id=<%= node.id %>"><%= node.name %></a></span>
</li>
<% end %>
<span><a href="/?node_id=<%= node.id %>"><%= node.name %></a></span>
</li>
</ul>
</div>
<% else %>
<% if !@query_node.nil? %>
<div class="node">
<div class="node-name"><%= @query_node.name %></div>
<div class="node-summary"><%= @query_node.summary %></div>
</div>
<% end %>
</ul>
</div>
<% end %>
<% @posts.each do |post| %>
<div class="post-summary">
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160813062205_add_summary_to_nodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSummaryToNodes < ActiveRecord::Migration
def change
add_column :nodes, :summary, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160724083228) do
ActiveRecord::Schema.define(version: 20160813062205) do

create_table "comments", force: :cascade do |t|
t.text "body"
Expand Down Expand Up @@ -40,6 +40,7 @@
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "summary"
end

create_table "posts", force: :cascade do |t|
Expand Down

0 comments on commit 9fe5483

Please sign in to comment.