Commit 87add773 by tady

Merge pull request #67 from tadyjp/wip/140318_draft

[WIP] 下書き保存機能
parents 3f0451f8 afe9a937
...@@ -2,6 +2,6 @@ class FlowController < ApplicationController ...@@ -2,6 +2,6 @@ class FlowController < ApplicationController
before_action :require_login before_action :require_login
def show def show
@posts = Post.order(updated_at: :desc).limit(20).decorate @posts = Post.where(is_draft: false).order(updated_at: :desc).limit(20).decorate
end end
end end
...@@ -132,7 +132,7 @@ class PostsController < ApplicationController ...@@ -132,7 +132,7 @@ class PostsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def post_params def post_params
@post_params ||= begin @post_params ||= begin
_param_hash = params.require(:post).permit(:title, :body, :tags).to_hash _param_hash = params.require(:post).permit(:title, :body, :tags, :is_draft).to_hash
# tags_text == 'Javascript,Ruby' # tags_text == 'Javascript,Ruby'
tags_text = _param_hash.delete('tags') tags_text = _param_hash.delete('tags')
......
class UserDecorator < Draper::Decorator class UserDecorator < Draper::Decorator
delegate_all delegate_all
def draft_count
model.posts.where(is_draft: true).count
end
end end
...@@ -4,30 +4,40 @@ class Post < ActiveRecord::Base ...@@ -4,30 +4,40 @@ class Post < ActiveRecord::Base
belongs_to :author, class_name: 'User' belongs_to :author, class_name: 'User'
has_many :comments has_many :comments
default_scope { order(:updated_at => :desc) } # default_scope { where(is_draft: false).order(:updated_at => :desc) }
######################################################################
# validations
######################################################################
validates :title, presence: true
validates :body, presence: true
######################################################################
# Named scope # Named scope
######################################################################
scope :search, (lambda do |query| scope :search, (lambda do |query|
_where_list = includes(:author, :tags) _where_list = includes(:author, :tags)
# Convert spaces to one space. # Convert spaces to one space.
query_list = query.gsub(/[\s ]+/, ' ').split(' ') query_list = query.split(/[\s ]+/)
query_list.each do |_query| query_list.each do |_query|
case _query case _query
when /^id:(.+)/ when /\Aid:(.+)/
_where_list = _where_list.where(id: Regexp.last_match[1]) _where_list = _where_list.where(id: Regexp.last_match[1])
when /^title:(.+)/ when /\Atitle:(.+)/
_where_list = _where_list.where('title LIKE ?', "%#{Regexp.last_match[1]}%") _where_list = _where_list.where('title LIKE ?', "%#{Regexp.last_match[1]}%")
when /^body:(.+)/ when /\Abody:(.+)/
_where_list = _where_list.where('body LIKE ?', "%#{Regexp.last_match[1]}%") _where_list = _where_list.where('body LIKE ?', "%#{Regexp.last_match[1]}%")
when /^@(.+)/ when /\A@(.+)/
_where_list = _where_list.where(users: { name: Regexp.last_match[1] }) _where_list = _where_list.where(users: { name: Regexp.last_match[1] })
when /^#(.+)/ when /\A#(.+)/
_where_list = _where_list.where(tags: { name: Regexp.last_match[1] }) _where_list = _where_list.where(tags: { name: Regexp.last_match[1] })
when /^date:(\d+)-(\d+)-(\d+)/ when /\Adate:(\d+)-(\d+)-(\d+)/
_date = Time.new(Regexp.last_match[1], Regexp.last_match[2], Regexp.last_match[3]) _date = Time.new(Regexp.last_match[1], Regexp.last_match[2], Regexp.last_match[3])
_where_list = _where_list.where('updated_at > ? AND updated_at < ?', _date, _date + 1.day) _where_list = _where_list.where('updated_at > ? AND updated_at < ?', _date, _date + 1.day)
when /\Adraft:1/
_where_list = _where_list.where(is_draft: true)
else else
_where_list = _where_list.where('title LIKE ? OR body LIKE ?', "%#{_query}%", "%#{_query}%") _where_list = _where_list.where('title LIKE ? OR body LIKE ?', "%#{_query}%", "%#{_query}%")
end end
......
nav.navbar.navbar-default.navbar-fixed-top role="navigation" nav.navbar.navbar-default.navbar-fixed-top role="navigation"
.container .container
.navbar-header .navbar-header
a.navbar-brand href=root_path Rendezvous a.navbar-brand href=root_path Rendezvous
...@@ -18,7 +19,6 @@ nav.navbar.navbar-default.navbar-fixed-top role="navigation" ...@@ -18,7 +19,6 @@ nav.navbar.navbar-default.navbar-fixed-top role="navigation"
a href=flow_path title="Frow" a href=flow_path title="Frow"
| Flow | Flow
ul.nav.navbar-nav.navbar-right ul.nav.navbar-nav.navbar-right
li li
form form
...@@ -29,10 +29,13 @@ nav.navbar.navbar-default.navbar-fixed-top role="navigation" ...@@ -29,10 +29,13 @@ nav.navbar.navbar-default.navbar-fixed-top role="navigation"
a.dropdown-toggle data-toggle="dropdown" a.dropdown-toggle data-toggle="dropdown"
= current_user.name = current_user.name
img src=current_user.image_url width="20" height="20"/ img src=current_user.image_url width="20" height="20"/
span.badge = current_user.decorate.draft_count
b.caret b.caret
ul.dropdown-menu ul.dropdown-menu
li li
a Account (todo) a href=search_path(q: "@#{current_user.name} draft:1")
| 下書き
span.badge.pull-right = current_user.decorate.draft_count
li li
a Settings (todo) a Settings (todo)
li.divider li.divider
......
...@@ -14,17 +14,18 @@ ...@@ -14,17 +14,18 @@
.input-group .input-group
span.input-group-addon= f.label :title span.input-group-addon= f.label :title
= f.text_field :title, class: 'form-control mod-mdEditor-title' = f.text_field :title, class: 'form-control mod-mdEditor-title'
.col-xs-2
.actions
= f.submit class: 'btn btn-primary js-disable-confirm-unload', id: 'save_button'
.row
.col-xs-8
.field .field
.input-group .input-group
span.input-group-addon= f.label :tags span.input-group-addon= f.label :tags
= hidden_field :post, :tags, class: 'mod-mdEditor-tags', style: 'width:300px', value: @post.tags.map{ |_tag| _tag.name }.join(',') = hidden_field :post, :tags, class: 'mod-mdEditor-tags', style: 'width:300px', value: @post.tags.map{ |_tag| _tag.name }.join(',')
.col-xs-4
.col-xs-2
p.actions
= f.submit class: 'btn btn-primary js-disable-confirm-unload', id: 'save_button'
p.actions
= f.check_box :is_draft
= f.label :is_draft, "下書き保存"
br/ br/
.row .row
...@@ -36,6 +37,7 @@ ...@@ -36,6 +37,7 @@
.text-box.body.viewer.github.mod-mdEditor-preview .text-box.body.viewer.github.mod-mdEditor-preview
input#fileupload data-url="/apis/file_receiver" multiple="" name="files[]" style="display:none" type="file" / input#fileupload data-url="/apis/file_receiver" multiple="" name="files[]" style="display:none" type="file" /
- content_for :footer_js do - content_for :footer_js do
javascript: javascript:
$.setConfirmUnload(); $.setConfirmUnload();
...@@ -46,3 +48,10 @@ input#fileupload data-url="/apis/file_receiver" multiple="" name="files[]" style ...@@ -46,3 +48,10 @@ input#fileupload data-url="/apis/file_receiver" multiple="" name="files[]" style
$input: $('#fileupload'), $input: $('#fileupload'),
$textarea: $('.mod-mdEditor-body') $textarea: $('.mod-mdEditor-body')
}); });
// 下書き保存
$('.btn-save-draft').on('click', function(e){
e.preventDefault();
var val = $('.mod-mdEditor-body').val();
console.log(val);
});
class AddIsDraftToPosts < ActiveRecord::Migration
def change
add_column :posts, :is_draft, :boolean, default: false
add_index :posts, :is_draft
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140302053916) do ActiveRecord::Schema.define(version: 20140318040809) do
create_table "comments", force: true do |t| create_table "comments", force: true do |t|
t.integer "author_id" t.integer "author_id"
...@@ -40,8 +40,11 @@ ActiveRecord::Schema.define(version: 20140302053916) do ...@@ -40,8 +40,11 @@ ActiveRecord::Schema.define(version: 20140302053916) do
t.integer "author_id" t.integer "author_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.boolean "is_draft", default: false
end end
add_index "posts", ["is_draft"], name: "index_posts_on_is_draft", using: :btree
create_table "tags", force: true do |t| create_table "tags", force: true do |t|
t.string "name" t.string "name"
t.datetime "created_at" t.datetime "created_at"
......
...@@ -45,6 +45,7 @@ describe Post do ...@@ -45,6 +45,7 @@ describe Post do
@post1 = Post.create id: 1001, title: 'ruby rspec', body: 'This is first espec test: ruby' @post1 = Post.create id: 1001, title: 'ruby rspec', body: 'This is first espec test: ruby'
@post2 = Post.create id: 1002, title: 'php test', body: 'PHP is very easy', author_id: @alice.id @post2 = Post.create id: 1002, title: 'php test', body: 'PHP is very easy', author_id: @alice.id
@post3 = Post.create id: 1003, title: 'java java...', body: 'Java is not ruby...', updated_at: Time.new(1989, 2, 25, 5, 30, 0) @post3 = Post.create id: 1003, title: 'java java...', body: 'Java is not ruby...', updated_at: Time.new(1989, 2, 25, 5, 30, 0)
@post4 = Post.create id: 1004, title: 'about ruby TDD', body: 'test is the best ....', is_draft: true
@tag_java = Tag.create(name: 'java') @tag_java = Tag.create(name: 'java')
@post3.tags << @tag_java @post3.tags << @tag_java
end end
...@@ -55,7 +56,7 @@ describe Post do ...@@ -55,7 +56,7 @@ describe Post do
end end
it 'by title' do it 'by title' do
expect(Post.search('title:ruby')).to have(1).items expect(Post.search('title:ruby')).to have(2).items
expect(Post.search('title:ruby')).to include(@post1) expect(Post.search('title:ruby')).to include(@post1)
end end
...@@ -79,8 +80,13 @@ describe Post do ...@@ -79,8 +80,13 @@ describe Post do
expect(Post.search('date:1989-2-25')).to include(@post3) expect(Post.search('date:1989-2-25')).to include(@post3)
end end
it 'by draft' do
expect(Post.search('ruby')).to have(3).items
expect(Post.search('ruby draft:1')).to have(1).items
end
it 'by else' do it 'by else' do
expect(Post.search('ruby')).to have(2).items expect(Post.search('ruby')).to have(3).items
expect(Post.search('ruby')).to include(@post1) expect(Post.search('ruby')).to include(@post1)
expect(Post.search('ruby')).to include(@post3) expect(Post.search('ruby')).to include(@post3)
end end
......
...@@ -5,9 +5,9 @@ describe Tag do ...@@ -5,9 +5,9 @@ describe Tag do
before :each do before :each do
@tag_ruby = Tag.create(name: 'ruby') @tag_ruby = Tag.create(name: 'ruby')
@tag_java = Tag.create(name: 'java') @tag_java = Tag.create(name: 'java')
@post1 = Post.create id: 1001, title: 'ruby rspec', tags: [@tag_ruby] @post1 = Post.create id: 1001, title: 'ruby rspec', body: 'hoge', tags: [@tag_ruby]
@post2 = Post.create id: 1002, title: 'ruby is better than java', tags: [@tag_ruby, @tag_java] @post2 = Post.create id: 1002, title: 'ruby is better than java', body: 'hoge', tags: [@tag_ruby, @tag_java]
@post3 = Post.create id: 1003, title: 'java java...', tags: [@tag_java] @post3 = Post.create id: 1003, title: 'java java...', body: 'hoge', tags: [@tag_java]
end end
it 'successfully moved' do it 'successfully moved' do
......
...@@ -118,4 +118,8 @@ describe User do ...@@ -118,4 +118,8 @@ describe User do
end end
end end
describe 'draft' do
pending 'draft'
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment