Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions app/controllers/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,31 @@ def create
node_params = safe_params.slice(:parent_id)
cv_params = safe_params.slice(:title, :body)
author_params = { id: safe_params[:author_id].to_i, user: current_user }
tag_decl_ids = safe_params[:tag_decl_ids]

@author = Author.where(**author_params).first
@node = Node.new(node_params.merge :author => @author)
@cv = ContentVersion.new(cv_params.merge :node => @node, :author => @author)
@tags = []
if !tag_decl_ids.nil?
tag_decl_ids.each do |anchored_tag|
@anchored_tag_decl = TagDecl.find(anchored_tag)
@tag_decl = TagDecl.new(:user => @user, :target => @node, :anchored => @anchored_tag_decl, :tag => "include_" + @anchored_tag_decl.tag)
@tags << @tag_decl
end
end

respond_to do |format|
if @author.save! && @node.save! && @cv.save!
format.html { redirect_to @node, notice: "Node was successfully created." }
format.json { render :show, status: :created, location: @node }
else
begin
ActiveRecord::Base.transaction do
@author.save!
@node.save!
@cv.save!
@tags.each &:save!
format.html { redirect_to @node, notice: "Node was successfully created." }
format.json { render :show, status: :created, location: @node }
end
rescue ActiveRecord::ActiveRecordError => e
format.html { render :new, :parent_id => @parent.id }
format.json { render json: @node.errors, status: :unprocessable_entity }
end
Expand Down Expand Up @@ -109,6 +124,6 @@ def set_node_to_children_map(id = params[:id].to_i)

# Only allow a list of trusted parameters through.
def new_node_params
params.require(:node).permit(:parent_id, :title, :body, :author_id)
params.require(:node).permit(:parent_id, :title, :body, :author_id, tag_decl_ids:[])
end
end
26 changes: 26 additions & 0 deletions app/javascript/packs/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import $ from 'jquery';

function add_to_selected_tags(e) {
var text = e.target[e.target.selectedIndex].text
var id = e.target.value
if (document.getElementById(text + "-tag") == null && text) {
var tag_html = "<span type='select' class='tag is-medium' value=" + id + " id=" + text + "-tag>" +
text +
"<button type='button' class='delete is-small' id='tag-delete'></button>" +
"<input type='hidden' name='node[tag_decl_ids][]' multiple=true value=" + id + ">" +
"</span> "
$('.field#selected-tags').append(tag_html)
}
}

function remove_from_selected_tags(e) {
e.target.parentElement.remove()
}

$(() =>
$('.select#tags-select').on('change', (e) => add_to_selected_tags(e))
);

$(() =>
$('body').on('click', 'button#tag-delete', (e) => remove_from_selected_tags(e))
);
12 changes: 11 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ class User < ApplicationRecord

has_one :users_group

after_create :create_user_author, :refresh_users_groups
after_create :create_user_author, :refresh_users_groups, :add_default_tags

def create_user_author
self.authors << Author.create(user: self)
end

def add_default_tags
# default blog tag
node = Node.create!
TagDecl.find_or_create_by! :anchored => node, :target => self, :tag => :blog_index, :user => self
end

def refresh_users_groups
UsersGroup.refresh
end
Expand All @@ -34,6 +40,10 @@ def groups
self.users_group.groups_in_database
end

def get_targeting_tags
self.targeting_tags
end

def groups_arel
throw "deprecated? replaced with user_groups view"
uts = UserTag.table
Expand Down
23 changes: 19 additions & 4 deletions app/views/nodes/_form_new_topic.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= javascript_pack_tag 'tags' %>
<%= form_with(model: node) do |form| %>
<% if node.errors.any? %>
<div id="message is-danger">
Expand All @@ -18,11 +19,25 @@
<%= form.text_field :title, class: "input" %>
</div>
<%= form.label :author_id, class: "label" %>
<div class="select">
<%= form.select :author_id, [["Public", true], ["Pseudonyms", false]].to_h {|p|
[p[0], @user.authors.where(public: p[1]).collect {|a| [a.formatted_name, a.id]}]
}, class: "" %>

<div class="field">
<div class="select">
<%= form.select :author_id, [["Public", true], ["Pseudonyms", false]].to_h {|p|
[p[0], @user.authors.where(public: p[1]).collect {|a| [a.formatted_name, a.id]}]
}, class: "" %>
</div>
</div>

<%= form.label :targeting_tags, class:"label" %>
<div class="field" id="selected-tags">
</div>

<div class="field">
<div class="select" id="tags-select" >
<%= form.select :tag_decl_id, @user.get_targeting_tags.collect { |t| [t.tag, t.id]}, :include_blank => true %>
</div>
</div>

<div class="field">
<%= form.label :body, class: "label" %>
<%= form.text_area :body, class: "textarea", size: "80x10" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/nodes/_preview_topic.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</p>
<nav class="breadcrumb">
<ul>
<li><%= link_to node.author.formatted_name, author_path(node.author_id) %></li>
<li><%= link_to node.author.formatted_name, author_path(node.author.id) %></li>
<li><%= link_to node.user.username, user_path(node.user) %></li>
<li class="is-active">
<a><%= node.created_at %> / <%= time_ago_in_words(node.created_at || DateTime.now) %> ago</a>
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"name": "cfforum",
"private": true,
"dependencies": {
"@babel/core": "^7.13.1",
"@babel/preset-env": "^7.13.5",
"@hotwired/turbo-rails": "^7.0.0-beta.4",
"@rails/actioncable": "^6.0.0",
"@rails/activestorage": "^6.0.0",
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "5.2.1",
"jquery": "^3.5.1",
"stimulus": "^2.0.0"
},
"version": "0.1.0",
Expand Down