Commit 3277f8d4 by tady

select initial post after create/update.

parent 441de0a5
...@@ -22,7 +22,7 @@ gem 'coffee-rails', '~> 4.0.0' ...@@ -22,7 +22,7 @@ gem 'coffee-rails', '~> 4.0.0'
# gem 'jquery-rails' # gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks' # gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2' gem 'jbuilder', '~> 1.2'
...@@ -52,6 +52,8 @@ gem 'omniauth-google-oauth2' ...@@ -52,6 +52,8 @@ gem 'omniauth-google-oauth2'
gem 'redcarpet' gem 'redcarpet'
group :development do group :development do
gem "better_errors" gem 'better_errors'
gem "binding_of_caller" gem 'binding_of_caller'
gem 'thin'
end end
...@@ -42,6 +42,7 @@ GEM ...@@ -42,6 +42,7 @@ GEM
coffee-script-source coffee-script-source
execjs execjs
coffee-script-source (1.6.3) coffee-script-source (1.6.3)
daemons (1.1.9)
debug_inspector (0.0.2) debug_inspector (0.0.2)
devise (3.2.2) devise (3.2.2)
bcrypt-ruby (~> 3.0) bcrypt-ruby (~> 3.0)
...@@ -50,6 +51,7 @@ GEM ...@@ -50,6 +51,7 @@ GEM
thread_safe (~> 0.1) thread_safe (~> 0.1)
warden (~> 1.2.3) warden (~> 1.2.3)
erubis (2.7.0) erubis (2.7.0)
eventmachine (1.0.3)
execjs (2.0.2) execjs (2.0.2)
faraday (0.8.8) faraday (0.8.8)
multipart-post (~> 1.2.0) multipart-post (~> 1.2.0)
...@@ -125,6 +127,10 @@ GEM ...@@ -125,6 +127,10 @@ GEM
activesupport (>= 3.0) activesupport (>= 3.0)
sprockets (~> 2.8) sprockets (~> 2.8)
sqlite3 (1.3.8) sqlite3 (1.3.8)
thin (1.6.1)
daemons (>= 1.0.9)
eventmachine (>= 1.0.0)
rack (>= 1.0.0)
thor (0.18.1) thor (0.18.1)
thread_safe (0.1.3) thread_safe (0.1.3)
atomic atomic
...@@ -132,8 +138,6 @@ GEM ...@@ -132,8 +138,6 @@ GEM
treetop (1.4.15) treetop (1.4.15)
polyglot polyglot
polyglot (>= 0.3.1) polyglot (>= 0.3.1)
turbolinks (1.3.1)
coffee-rails
tzinfo (0.3.38) tzinfo (0.3.38)
uglifier (2.3.1) uglifier (2.3.1)
execjs (>= 0.3.0) execjs (>= 0.3.0)
...@@ -156,5 +160,5 @@ DEPENDENCIES ...@@ -156,5 +160,5 @@ DEPENDENCIES
sass-rails (~> 4.0.0) sass-rails (~> 4.0.0)
sdoc sdoc
sqlite3 sqlite3
turbolinks thin
uglifier (>= 1.3.0) uglifier (>= 1.3.0)
...@@ -12,80 +12,9 @@ ...@@ -12,80 +12,9 @@
// //
// require jquery // require jquery
// require jquery_ujs // require jquery_ujs
//= require turbolinks // require turbolinks
//= require_tree ./lib //= require_tree ./lib
//= require_tree . //= require_tree .
// Automaticaly change textarea height.
$(function(){
'use strict';
$('textarea.autosize').autosize();
// Preview post.
var load_preview = function(){
var text = $('#post_body').val();
var csrfToken = $("meta[name='csrf-token']").attr('content');
$.post('/posts/preview.api', {
'text': text,
'authenticity_token': csrfToken
})
.done(function(data){
$('#post_preview').html(data);
});
};
$('#post_body').on('keyup mouseup', load_preview);
load_preview();
// post list
$('.post-list').on('click', function(e){
e.preventDefault();
var $this = $(this);
$this.siblings().removeClass('active');
$this.addClass('active');
var id = $this.data('postId');
$.get('/posts/show_fragment', {
'id': id,
})
.done(function(data){
$('#list_post').html(data);
});
});
// search form
$('#app-search-form input').on('focus', function(){
$(this).parents('#app-search-form').animate({width: '600px'});
}).on('blur', function(){
$(this).parents('#app-search-form').animate({width: '200px'});
});
// disable tab key
$('.disable-tab').on('keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 9) {
e.preventDefault();
var start = $(this).get(0).selectionStart;
var end = $(this).get(0).selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start) +
'\t' +
$(this).val().substring(end));
// put caret at right position again
$(this).get(0).selectionStart =
$(this).get(0).selectionEnd = start + 1;
}
});
// new post tags
$('#post_tags').select2();
});
...@@ -91,8 +91,10 @@ ...@@ -91,8 +91,10 @@
font-weight: bold; font-weight: bold;
font-size: 25px; font-size: 25px;
} }
.input-group-addon label { .input-group-addon {
margin: 0; label {
margin: 0;
}
} }
#post_body { #post_body {
......
...@@ -52,6 +52,7 @@ class PostsController < ApplicationController ...@@ -52,6 +52,7 @@ class PostsController < ApplicationController
# PATCH/PUT /posts/1 # PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json # PATCH/PUT /posts/1.json
def update def update
respond_to do |format| respond_to do |format|
if @post.update(post_params) if @post.update(post_params)
format.html { redirect_to root_path(id: @post.id), notice: 'Post was successfully updated.' } format.html { redirect_to root_path(id: @post.id), notice: 'Post was successfully updated.' }
...@@ -81,6 +82,18 @@ class PostsController < ApplicationController ...@@ -81,6 +82,18 @@ 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
params.require(:post).permit(:title, :body) @post_params ||= begin
_param_hash = params.require(:post).permit(:title, :body, :tags).to_hash
# tags_text == 'Javascript,Ruby'
tags_text = _param_hash.delete('tags')
tags = tags_text.split(',').map do |_tag_name|
Tag.find_or_create_by(name: _tag_name)
end
_param_hash["tag_ids"] = tags.map(&:id)
_param_hash
end
end end
end end
...@@ -29,9 +29,83 @@ ...@@ -29,9 +29,83 @@
<hr> <hr>
<footer> <footer>
<p>&copy; tady</p>
</footer> </footer>
</div><!--/.container--> </div><!--/.container-->
</div> </div>
<% content_for :footer_js do %>
<script type="text/javascript">
$(function(){
'use strict';
// Preview post.
var load_preview = function(){
var text = $('#post_body').val();
var csrfToken = $("meta[name='csrf-token']").attr('content');
$.post('/posts/preview.api', {
'text': text,
'authenticity_token': csrfToken
})
.done(function(data){
$('#post_preview').html(data);
});
};
$('#post_body').on('keyup mouseup', load_preview);
// load_preview();
// post list
$('.post-list').on('click', function(e){
e.preventDefault();
var $this = $(this);
$this.siblings().removeClass('active');
$this.addClass('active');
var id = $this.data('postId');
$.get('/posts/show_fragment', {
'id': id,
})
.done(function(data){
$('#list_post').html(data);
});
});
// initial post selection.
(function(){
// open post when `id` parameter set.
// /?a=b&c=123 ==> {"a": "b", "c": "123"}
var getQueryParams = function() {
var query = location.search.split("+").join(" "),
params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(query)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
var id_param = getQueryParams()["id"];
if(id_param) {
$(".post-list[data-post-id='" + id_param + "']").addClass('active');
$.get('/posts/show_fragment', {
'id': id_param,
})
.done(function(data){
$('#list_post').html(data);
});
}
})();
// search form
$('#app-search-form input').on('focus', function(){
$(this).parents('#app-search-form').animate({width: '600px'});
}).on('blur', function(){
$(this).parents('#app-search-form').animate({width: '200px'});
});
});
</script>
<% end %>
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<head> <head>
<title>Rendezvous</title> <title>Rendezvous</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= stylesheet_link_tag "application", media: "all" %>
<!-- Latest compiled and minified CSS --> <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<!-- Optional theme --> <!-- Optional theme -->
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript --> <!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= javascript_include_tag "application" %>
<%= yield :footer_js %>
</body> </body>
</html> </html>
...@@ -25,15 +25,7 @@ ...@@ -25,15 +25,7 @@
<div class="field"> <div class="field">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon"><%= f.label :tags %></span> <span class="input-group-addon"><%= f.label :tags %></span>
<select id="post_tags" multiple> <%= hidden_field :post, :tags, id: 'post_tags', style: 'width:300px', value: @post.tags.pluck(:name).join(',') %>
<% Tag.all.each do |tag| %>
<% if @post.tag_ids.include?(tag.id) %>
<option value="<%= tag.name %>" selected><%= tag.name %></option>
<% else %>
<option value="<%= tag.name %>"><%= tag.name %></option>
<% end %>
<% end %>
</select>
</div> </div>
</div> </div>
</div> </div>
...@@ -71,5 +63,41 @@ ...@@ -71,5 +63,41 @@
</div><!--/row--> </div><!--/row-->
<% end %>
<% content_for :footer_js do %>
<script type="text/javascript">
$(function(){
'use strict';
// Automaticaly change textarea height.
$('textarea.autosize').autosize();
// disable tab key
$('.disable-tab').on('keydown', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 9) {
e.preventDefault();
var start = $(this).get(0).selectionStart;
var end = $(this).get(0).selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start) +
'\t' +
$(this).val().substring(end));
// put caret at right position again
$(this).get(0).selectionStart =
$(this).get(0).selectionEnd = start + 1;
}
});
// new post tags
$('#post_tags').select2({
tags: <%= raw Tag.all.pluck(:name).to_json %>
});
});
</script>
<% end %> <% end %>
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
<hr> <hr>
<footer> <footer>
<p>&copy; tady</p>
</footer> </footer>
</div> </div>
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
<hr> <hr>
<footer> <footer>
<p>&copy; tady</p>
</footer> </footer>
</div> </div>
......
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