From b6115970dd08e5d6c28a3bc1226d9d87f3b426ad Mon Sep 17 00:00:00 2001 From: jomo Date: Wed, 16 Oct 2013 00:51:50 +0200 Subject: [PATCH] more forum stuff --- app/assets/stylesheets/screen.css.scss | 30 ++++++++++---- app/controllers/blogposts_controller.rb | 2 +- app/controllers/forumgroups_controller.rb | 41 ++++++++++++++++++- app/controllers/forums_controller.rb | 27 ++++++++++++ app/controllers/youtube.regex | 6 +++ app/models/forum.rb | 4 ++ app/models/forumgroup.rb | 8 ++++ app/models/role.rb | 6 +++ app/views/blogposts/edit.html.erb | 2 +- app/views/blogposts/index.html.erb | 4 +- app/views/comments/_comment.html.erb | 2 +- app/views/comments/edit.html.erb | 2 +- app/views/forumgroups/index.html.erb | 10 ++--- app/views/forumgroups/new.html.erb | 16 ++++++++ app/views/forums/new.html.erb | 17 ++++++++ app/views/forums/show.html.erb | 2 +- app/views/layouts/_head.html.erb | 1 + app/views/sessions/new.html.erb | 8 ++-- app/views/users/edit.html.erb | 23 +++++++---- app/views/users/new.html.erb | 23 +++++++---- config/routes.rb | 9 +--- .../00000000000005_create_forumgroups.rb | 5 ++- db/migrate/00000000000006_create_forums.rb | 5 ++- ...b => 00000000000008_add_sessions_table.rb} | 0 db/schema.rb | 10 ++--- 25 files changed, 208 insertions(+), 55 deletions(-) create mode 100644 app/controllers/youtube.regex create mode 100644 app/views/forumgroups/new.html.erb create mode 100644 app/views/forums/new.html.erb rename db/migrate/{20130922181339_add_sessions_table.rb => 00000000000008_add_sessions_table.rb} (100%) diff --git a/app/assets/stylesheets/screen.css.scss b/app/assets/stylesheets/screen.css.scss index 664b1e3..530c8f7 100644 --- a/app/assets/stylesheets/screen.css.scss +++ b/app/assets/stylesheets/screen.css.scss @@ -290,9 +290,22 @@ and (min-width: 0px) //TODO &[type=submit] { margin: 0; } + option { + padding: 0.5em; + &:hover { + background: #0f0; + color: #00f; + } + &:nth-child(even) { + background: #eee; + } + &:nth-child(odd) { + background: #ccc; + } + } } - input[type=text], input[type=email], input[type=password], textarea { + input[type=text], input[type=email], input[type=password], input[type=number], textarea, select { background: #ddd; border: none; display: block; @@ -352,13 +365,13 @@ and (min-width: 0px) //TODO float: left; label { display: block; - padding: 0 0 4px 0; - margin: 0 4px 4px; - height: 17px; + padding: 0.5em; } } #form_inputs { - + input, select, textarea { + width: 400px; + } } .profile-action { @@ -397,6 +410,9 @@ and (min-width: 0px) //TODO &.red { background: #ee4040; } + &.right { + float: right; + } &:hover { box-shadow: 0 0 5px #095fb7 inset; color: #fff; @@ -468,7 +484,7 @@ and (min-width: 0px) //TODO .header { background: #ddd; border-radius: 5px 5px 0 0; - padding: 5px 10px; + padding: 0.5em; border-bottom: 1px solid #ccc; } .forums { @@ -478,7 +494,7 @@ and (min-width: 0px) //TODO .forum { display: block; font-weight: bold; - padding: 5px 10px; + padding: 0.5em; border-bottom: 1px solid #ddd; } } diff --git a/app/controllers/blogposts_controller.rb b/app/controllers/blogposts_controller.rb index ba20e5f..86adbb3 100644 --- a/app/controllers/blogposts_controller.rb +++ b/app/controllers/blogposts_controller.rb @@ -29,7 +29,7 @@ class BlogpostsController < ApplicationController def create if mod? - @post = Blogpost.new(params[:blogpost]) + @post = Blogpost.new(params[:blogpost] ? params[:blogpost].slice(:title, :content) : {}) @post.user_author = current_user if @post.save redirect_to @post, notice: 'Post has been created.' diff --git a/app/controllers/forumgroups_controller.rb b/app/controllers/forumgroups_controller.rb index 4f41a7f..bacadab 100644 --- a/app/controllers/forumgroups_controller.rb +++ b/app/controllers/forumgroups_controller.rb @@ -1,9 +1,46 @@ class ForumgroupsController < ApplicationController + def index - @groups = Forumgroup.all.sort_by{|s| s[:position]} + @groups = Forumgroup.all + if current_user + @groups.select! do |g| + g.role_read == nil || g.role_read <= current_user.role + end + else + @groups.select!{|g| g.role_read == nil} + end + @groups.sort_by{|g| g[:position]} end def show - redirect_to forumgroups_path + "#forum-#{params[:id]}" + redirect_to forumgroups_path + "#forums-#{params[:id]}" end + + def new + if admin? + @group = Forumgroup.new + else + flash[:alert] = "You are not allowed to create forums." + redirect_to forumgroups_path + end + end + + def create + if admin? + @group = Forumgroup.new(params[:forumgroup]) + if @group.save + flash[:notice] = "Forums created." + redirect_to @group + else + flash[:alert] = "Something went wrong" + render :new + end + else + flash[:alert] = "You are not allowed to create forums." + redirect_to forumgroups_path + end + end + + + end \ No newline at end of file diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index d7242cd..2ecb551 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -8,4 +8,31 @@ class ForumsController < ApplicationController @threads = @forum.forumthreads end + def new + if admin? + @group = Forumgroup.find(params[:forumgroup_id]) + @forum = Forum.new(forumgroup: @group) + else + flash[:alert] = "You are not allowed to create a forum." + redirect_to forumgroups_path + end + end + + def create + if admin? + @forum = Forum.new(params[:forum]) + @forum.forumgroup = Forumgroup.find(params[:forumgroup_id]) + if @forum.save + flash[:notice] = "Forum created." + redirect_to @forum + else + flash[:alert] = "Something went wrong" + render :new + end + else + flash[:alert] = "You are not allowed to create a forum." + redirect_to forums_path + end + end + end \ No newline at end of file diff --git a/app/controllers/youtube.regex b/app/controllers/youtube.regex new file mode 100644 index 0000000..855c624 --- /dev/null +++ b/app/controllers/youtube.regex @@ -0,0 +1,6 @@ +[yt](729o_Xwg1WU) + +\[yt\]\(([a-zA-Z0-9\-_]+)\) + + +https://www.youtube.com/embed/{id}?theme=light&iv_load_policy=3&showinfo=1&showsearch=0&rel=0&modestbranding&hd=1&autohide=1 \ No newline at end of file diff --git a/app/models/forum.rb b/app/models/forum.rb index 5011c4c..4ace5db 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -1,6 +1,10 @@ class Forum < ActiveRecord::Base belongs_to :forumgroup has_many :forumthreads + belongs_to :role_read, class_name: "Role", foreign_key: "role_read_id" + belongs_to :role_write, class_name: "Role", foreign_key: "role_write_id" + + attr_accessible :name, :position, :role_read, :role_write, :role_read_id, :role_write_id, :forumgroup, :forumgroup_id def to_s name diff --git a/app/models/forumgroup.rb b/app/models/forumgroup.rb index daca97b..55577f5 100644 --- a/app/models/forumgroup.rb +++ b/app/models/forumgroup.rb @@ -1,5 +1,13 @@ class Forumgroup < ActiveRecord::Base has_many :forums + belongs_to :role_read, class_name: "Role", foreign_key: "role_read_id" + belongs_to :role_write, class_name: "Role", foreign_key: "role_write_id" + accepts_nested_attributes_for :forums + + attr_accessible :name, :position, :role_read, :role_write, :role_read_id, :role_write_id + + validates_presence_of :name, :position + validates_length_of :name, in: 2..20 def to_s name diff --git a/app/models/role.rb b/app/models/role.rb index a01f301..7785c41 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -35,4 +35,10 @@ class Role < ActiveRecord::Base end end + def self.all_from(role) + Role.all.select do |r| + r >= role + end + end + end \ No newline at end of file diff --git a/app/views/blogposts/edit.html.erb b/app/views/blogposts/edit.html.erb index e94e620..3b85d52 100644 --- a/app/views/blogposts/edit.html.erb +++ b/app/views/blogposts/edit.html.erb @@ -6,5 +6,5 @@ <%= f.submit "Update Post", :id => "edit_create_post", class: "btn blue" %> <% end %>
- <%= button_to "Delete post", @post, :method => "delete", :confirm => "Delete post + comments forever?", class: "btn red" %> + <%= button_to "Delete post", @post, :method => "delete", :confirm => "Delete post & comments forever?", class: "btn red right" %>
diff --git a/app/views/blogposts/index.html.erb b/app/views/blogposts/index.html.erb index 3f8da5c..37a6e81 100644 --- a/app/views/blogposts/index.html.erb +++ b/app/views/blogposts/index.html.erb @@ -1,6 +1,7 @@ +<%= button_to 'Make new Post', new_blogpost_path, method: "get", class: "btn blue" if mod? %>
<% @posts.each do |p| %> -
+

<%= link_to truncate(p.title, length: 60, omission: " …"), p %>

@@ -15,5 +16,4 @@
<% end %> - <%= button_to 'Make new Post', new_blogpost_path, method: "get", class: "btn blue" if mod? %>
diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 222807f..61afd93 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,4 +1,4 @@ -
"> +
" id="comment-<%= c.id %>"> <%= link_to c.author.name, c.author %> <%= c.created_at.strftime("%e. %b %Y, %H:%m") %> <% if mod? || c.author.is?(current_user) %> diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb index 6ffba95..4a0aac9 100644 --- a/app/views/comments/edit.html.erb +++ b/app/views/comments/edit.html.erb @@ -5,5 +5,5 @@ <%= f.submit "Update Comment", :id => "edit_create_comment", class: "btn blue" %> <% end %>
- <%= button_to "Delete comment", [@comment.blogpost, @comment] , :method => "delete", :confirm => "Delete comment forever?", class: "btn red" %> + <%= button_to "Delete comment", [@comment.blogpost, @comment] , :method => "delete", :confirm => "Delete comment forever?", class: "btn red right" %>
\ No newline at end of file diff --git a/app/views/forumgroups/index.html.erb b/app/views/forumgroups/index.html.erb index 4f2b757..b54c68b 100644 --- a/app/views/forumgroups/index.html.erb +++ b/app/views/forumgroups/index.html.erb @@ -1,15 +1,15 @@
<% @groups.each do |group| %> -
+
<%= group.name %>
- <% group.forums.sort_by{|s| s[:position]}.each do |f| %> -
- <%= link_to f.name, f %> -
+ <% group.forums.each do |f| %> +
+ <%= link_to f.name, f %> +
<% end %>
diff --git a/app/views/forumgroups/new.html.erb b/app/views/forumgroups/new.html.erb new file mode 100644 index 0000000..198db1e --- /dev/null +++ b/app/views/forumgroups/new.html.erb @@ -0,0 +1,16 @@ +

New forum group

+<%= simple_form_for @group do |f|%> +
+ <%= f.label :name %> + <%= f.label :position %> + <%= f.label :role_read_id, "Min read role" %> + <%= f.label :role_write_id, "Min write role" %> +
+
+ <%= f.input :name, placeholder: "Name" %> + <%= f.input :position, placeholder: "Position" %> + <%= f.input :role_read_id, as: :select, collection: Role.all_from(Role.get :default), include_blank: "None" %> + <%= f.input :role_write_id, as: :select, collection: Role.all_from(Role.get :default), include_blank: false %> +
+ <%= f.submit "Create group", class: "btn blue" %> +<% end %> \ No newline at end of file diff --git a/app/views/forums/new.html.erb b/app/views/forums/new.html.erb new file mode 100644 index 0000000..4813745 --- /dev/null +++ b/app/views/forums/new.html.erb @@ -0,0 +1,17 @@ +<%= link_to @group %> → New forum +

New forum

+<%= simple_form_for [@group, @forum] do |f|%> +
+ <%= f.label :name %> + <%= f.label :position %> + <%= f.label :role_read_id, "Min read role" %> + <%= f.label :role_write_id, "Min write role" %> +
+
+ <%= f.input :name, placeholder: "Name" %> + <%= f.input :position, placeholder: "Position" %> + <%= f.input :role_read_id, as: :select, collection: Role.all_from(Role.get :default), include_blank: "None" %> + <%= f.input :role_write_id, as: :select, collection: Role.all_from(Role.get :default), include_blank: false %> +
+ <%= f.submit "Create forum", class: "btn blue" %> +<% end %> diff --git a/app/views/forums/show.html.erb b/app/views/forums/show.html.erb index 69d1690..7400e8d 100644 --- a/app/views/forums/show.html.erb +++ b/app/views/forums/show.html.erb @@ -1,4 +1,4 @@ -
<%= link_to @forum.group, forumgroup_path(@forum.group) %> → <%= link_to @forum %>
+<%= link_to "Forums", forumgroups_path %> → <%= link_to @forum.group, "#{forumgroups_path}#forums-#{@forum.group.id}" %> → <%= link_to @forum %>

<%= @forum.name %>

<% @threads.each do |thread| %> diff --git a/app/views/layouts/_head.html.erb b/app/views/layouts/_head.html.erb index 1d960fa..45a5085 100644 --- a/app/views/layouts/_head.html.erb +++ b/app/views/layouts/_head.html.erb @@ -17,6 +17,7 @@