Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few fixes and features #596

Open
wants to merge 12 commits into
base: rails4
Choose a base branch
from
36 changes: 36 additions & 0 deletions app/assets/javascripts/forem.js.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
//= require select2
//= require jquery

var Forem = {};
Forem.routes = {};

var textareaSelector = 'textarea.new-post';
var lastRenderedMarkdown = "";
var previewRefreshTimeout = 100;

$(function() {
if($(textareaSelector).length) {
fetch_preview();
}
});

function fetch_preview()
{
markdownText = $(textareaSelector).val();
if(markdownText == lastRenderedMarkdown) {
setTimeout(fetch_preview, previewRefreshTimeout);
return;
}

lastRenderedMarkdown = markdownText;
$.post(
$(textareaSelector).data("previewUrl"),
{ markdown : markdownText },
function(result) {
update_preview(result);
setTimeout(fetch_preview, previewRefreshTimeout);
}
);
}

function update_preview(data)
{
selector = $(textareaSelector).data("previewElementSelector");
$(selector).html(data.html);
}
17 changes: 16 additions & 1 deletion app/controllers/forem/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Forem
class PostsController < Forem::ApplicationController
before_filter :authenticate_forem_user, except: :show
before_filter :find_topic
before_filter :find_topic, except: [:preview]
before_filter :reject_locked_topic!, :only => [:create]
before_filter :block_spammers, :only => [:new, :create]
before_filter :authorize_reply_for_topic!, :only => [:new, :create]
Expand All @@ -26,6 +26,10 @@ def new
flash[:notice] = t("forem.post.cannot_quote_deleted_post")
redirect_to [@topic.forum, @topic]
end

if(@reply_to_post)
@post.reply_to = @reply_to_post
end
end

def create
Expand Down Expand Up @@ -54,6 +58,17 @@ def destroy
@post.destroy
destroy_successful
end

def preview
formatted_markdown = {
markdown: params[:markdown],
html: ApplicationController.helpers.forem_format(params[:markdown])
}
respond_to do |format|
format.json { render json: formatted_markdown}
end
end


private

Expand Down
2 changes: 1 addition & 1 deletion app/models/forem/forum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def last_post_for(forem_user)
end

def last_visible_post(forem_user)
posts.approved_or_pending_review_for(forem_user).last
posts.visible.approved_or_pending_review_for(forem_user).last
end

def moderator?(user)
Expand Down
14 changes: 7 additions & 7 deletions app/models/forem/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ def visible
end

def by_pinned
order('forem_topics.pinned DESC').
order('forem_topics.id')
order('forem_topics.pinned DESC',
'forem_topics.id')
end

def by_most_recent_post
order('forem_topics.last_post_at DESC').
order('forem_topics.id')
order('forem_topics.last_post_at DESC',
'forem_topics.id')
end

def by_pinned_or_most_recent_post
order('forem_topics.pinned DESC').
order('forem_topics.last_post_at DESC').
order('forem_topics.id')
order('forem_topics.pinned DESC',
'forem_topics.last_post_at DESC',
'forem_topics.id')
end

def pending_review
Expand Down
8 changes: 7 additions & 1 deletion app/views/forem/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<%= f.input :text, :input_html => { :class => "field col-md-12" } %>
<%= render "/forem/posts/post",
post: f.object,
post_counter: 1,
show_buttons: false
%>

<%= f.input :text, :input_html => { :class => "field col-md-12 new-post", "data-preview-url" => render_post_preview_path, "data-preview-element-selector" => "#post_2 .contents .text" } %>

<% if params[:reply_to_id] %>
<%= f.hidden_field :reply_to_id, :value => params[:reply_to_id] %>
Expand Down
47 changes: 35 additions & 12 deletions app/views/forem/posts/_post.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<%
post_user = Forem::NilUser
if post.persisted?
post_user = post.user
else
post_user = forem_user
end

show_buttons = post.persisted? && local_assigns[:show_buttons].nil? ? true : show_buttons
%>

<a name='post-<%= post.id %>'></a>
<div class="row">
<div id='post_<%= post_counter + 1 %>' class='post <%= cycle('odd', 'even') -%> col-md-12'>
<% if post.pending_review? %>
<% if post.pending_review? && show_buttons %>
<div class='moderation alert'>
<%= t(".pending_review") %>
<% if forem_admin_or_moderator?(post.topic.forum) %>
Expand All @@ -16,31 +27,43 @@
</div>
<% end %>

<div class='contents'>
<div class='user col-md-2'>
<div class='username'>
<% if post.user.is_a?(Forem::NilUser) %>
<% if post_user.is_a?(Forem::NilUser) %>
<%= t(:deleted) %>
<% else %>
<%= link_to_if Forem.user_profile_links, post.user.forem_name, [main_app, post.user] %>
<%= link_to_if Forem.user_profile_links, post_user.forem_name, [main_app, post_user] %>
<% end %>
</div>
<div class='icon'><%= forem_avatar(post.user, :size => 60) %></div>
<div class='icon'><%= forem_avatar(post_user, :size => 60) %></div>
<div class='info'>
<ul>
<li class='post-count'><%= t('wrote_posts', count: post_user.forem_posts.approved.count) %></li>
<li class='member-since'><%= t('member_since', date: post_user.created_at.strftime('%e %b %y')) %></li>
</ul>
</div>

</div>

<div class='contents col-md-7'>
<a href='#post-<%= post.id %>'>
<%= post_time_tag(post) %>
<a href='#post-<%= post.id %>' class='time'>
<% if post.persisted? %>
<%= post_time_tag(post) %>
<% else %>
&nbsp;
<% end %>
</a>
<%= forem_format(post.text) %>

<% if post.reply_to %>
<div class='in-reply-to'>
<%= link_to "#{t("forem.post.in_reply_to")} #{post.reply_to.user.forem_name}", "#post-#{post.reply_to.id}" %>
</div>
<% end %>

<span class="text">
<%= forem_format(post.text) if post.persisted? %>
</span>

<% if forem_user %>
<% if forem_user && show_buttons %>
<div class='btn-group'>
<% if can?(:reply, post.topic) %>
<% if post.topic.can_be_replied_to? %>
Expand All @@ -50,7 +73,7 @@
<% end %>
<% if post.owner_or_admin?(forem_user) %>
<% if can?(:edit_post, post.topic.forum) %>
<%= link_to t('edit', :scope => 'forem.post'), forem.edit_forum_topic_post_path(post.forum, post.topic, post), :class => "btn btn-info" %>
<%= link_to t('edit', :scope => 'forem.post.buttons'), forem.edit_forum_topic_post_path(post.forum, post.topic, post), :class => "btn btn-info" %>
<% end %>
<% if can?(:destroy_post, post.topic.forum) %>
<%= link_to t('delete', :scope => 'forem.topic'), forem.forum_topic_post_path(post.forum, post.topic, post), :method => :delete, data: { :confirm => t("are_you_sure") }, :class => "btn btn-danger" %>
Expand All @@ -59,6 +82,6 @@
</div>
<% end %>
</div>
</div>

</div>
</div>
14 changes: 6 additions & 8 deletions app/views/forem/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<%= render :partial => 'forem/forums/head', :locals => { :forum => @topic.forum } %>

<h2>
<%= t("forem.post.new") %>
<% if @reply_to_post %>
<%= render 'reply_to_post', :post => @reply_to_post %>
<% else %>
<em><%= @topic.subject %></em>
<% end %>
</h2>
<h2><%= t("forem.post.new") %></h2>
<% if @reply_to_post %>
<%= render "/forem/posts/post", :post => @reply_to_post, post_counter: 0, show_buttons: false %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing some indentation... just a small nitpick.

I like that you've removed the duplication in the _reply_to_post partial.

<% else %>
<em><%= @topic.subject %></em>
<% end %>

<%= simple_form_for [forem, @topic.forum, @topic, @post] do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/forem/topics/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<% end %>

<%= f.submit :class => "btn btn-primary" %>
<% end %>
<% end %>
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,5 @@ en:
by: by
started_by: "Started by "
are_you_sure: Are you sure?
wrote_posts: "Wrote %{count} posts"
member_since: "Member since %{date}"
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
get 'users/autocomplete', :to => "users#autocomplete", :as => "user_autocomplete"
end

#post preview
post '/render_preview', to: "posts#preview", as: :render_post_preview

get '/:forum_id/moderation', :to => "moderation#index", :as => :forum_moderator_tools
# For mass moderation of posts
put '/:forum_id/moderate/posts', :to => "moderation#posts", :as => :forum_moderate_posts
Expand Down
2 changes: 1 addition & 1 deletion forem.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'launchy'
s.add_development_dependency 'rspec-rails', '~> 2.14.0'
s.add_development_dependency 'capybara', '2.1.0'
s.add_development_dependency 'jquery-rails'

s.add_development_dependency 'factory_girl_rails', '~> 4.4.1'
s.add_development_dependency 'database_cleaner', '~> 1.0.0'
Expand All @@ -29,6 +28,7 @@ Gem::Specification.new do |s|
s.add_dependency 'gemoji', '= 2.1.0'
s.add_dependency 'decorators', '~> 1.0.2'
s.add_dependency 'select2-rails', '~> 3.5.4'
s.add_dependency 'jquery-rails', '~> 3.1.0'
s.add_dependency 'friendly_id', '~> 5.0.0'
s.add_dependency 'cancancan', '~> 1.7'
end
6 changes: 5 additions & 1 deletion lib/forem/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'emoji'

module ::Forem
class Engine < Rails::Engine
isolate_namespace Forem
Expand All @@ -13,17 +15,19 @@ def root
Decorators.register! Engine.root, Rails.root
end

config.assets.paths << Emoji.images_path

# Precompile any assets included straight in certain pges
initializer "forem.assets.precompile", :group => :all do |app|
app.config.assets.precompile += %w[
forem/admin/members.js
]
app.config.assets.precompile << "emoji/**/*.png"
end
end
end

require 'simple_form'
require 'emoji'
require 'select2-rails'

# We need one of the two pagination engines loaded by this point.
Expand Down