Skip to content

Commit

Permalink
Merge pull request #21 from x3igh7/news
Browse files Browse the repository at this point in the history
News
  • Loading branch information
x3igh7 committed Feb 6, 2014
2 parents 0306b7a + c23a298 commit 715adbe
Show file tree
Hide file tree
Showing 32 changed files with 216 additions and 57 deletions.
9 changes: 9 additions & 0 deletions app/assets/stylesheets/ftwgl.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,12 @@ td{
.outer-heading{
font-size:24px;
}

.headline {
padding: 5px;
display: table;
margin: 0;
font-weight: 100;
font-size: 20px;
background-color: #cc5500;
}
34 changes: 32 additions & 2 deletions app/controllers/admin/news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def create
@news.newsable_id = 0
@news.newsable_type = "General"
else
tournament = Tournament.where("name = #{@source}")
@news.newsable_id = tournament.id
@tournament = Tournament.where(name: "#{@source}")
@news.newsable_id = @tournament.last.id
@news.newsable_type = "Tournament"
end

Expand All @@ -30,4 +30,34 @@ def create

end

def edit
@news = News.find(params[:id])
@sources = ["General"]
@tournaments = Tournament.where("active = true").each { |tourny| @sources << tourny.name }
end

def update
@news = News.find(params[:id])
if @news.update_attributes(params[:news])
flash[:notice] = "News successfully added!"
redirect_to news_path(@news.id)
else
flash[:alert] = "Failed to add news."
render :edit
end

end

def destroy
@news = News.find(params[:id])

if @news.delete
flash[:notice] = "News deleted."
redirect_to admin_root_path
else
flash[:alert] = "Unable to delete News."
redirect_to admin_root_path
end
end

end
21 changes: 15 additions & 6 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
class CommentsController < ApplicationController
def create
if not params[:match_id].nil?
if not params[:news_id].nil?
@user = current_user
@news = News.find(params[:news_id])
@comment = @news.comments.build(:content => params[:comment][:content], :user_id => @user.id )
if @comment.save
flash[:notice] = "Comment accepted"
redirect_to :back
else
flash[:alert] = @comment.errors.full_messages.first
redirect_to news_path(@news)
end
elsif params[:match_id].nil?
@match = Match.find(params[:match_id])
@home_team = @match.home_team.team
@away_team = @match.away_team.team
if current_user.is_team_member?(@home_team) or current_user.is_team_member?(@away_team)
@comment = @match.comments.build( :content => params[:comment][:content], :user_id => current_user.id )
if @comment.save
flash[:notice] = "Comment accepted"
redirect_to :back
else
flash[:alert] = @comment.errors.full_messages.first
redirect_to tournament_match_path(@match.tournament, @match)
return
end
end
end

#TODO: cases for other comment types e.g. tournament, news comments
flash[:alert] = @comment.errors.full_messages.first
redirect_to tournament_match_path(@match.tournament, @match)
end
end
6 changes: 4 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class HomeController < ApplicationController

def home
@news = News.where(newsable_id: 0)
if user_signed_in?
@teams = current_user.teams
@tournaments = current_user.tournaments
@user = current_user
@teams = @user.teams
@tournaments = @user.tournaments
end
end
end
1 change: 1 addition & 0 deletions app/controllers/matches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def show
@match = Match.find(params[:id])
@home_team = @match.home_team.team
@away_team = @match.away_team.team
@comments = @match.comments
end

def edit
Expand Down
7 changes: 4 additions & 3 deletions app/controllers/news_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class NewsController < ApplicationController

def show
@news = News.find(params[:id])
end
def show
@news = News.find(params[:id])
@comments = @news.comments
end

end

1 change: 1 addition & 0 deletions app/controllers/tournaments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def show
@active_tournament_team = nil
@tournament_team = TournamentTeam.new
@matches = Match.current_week_matches(@tournament)
@news = @tournament.news
@current_user_teams = []
if user_signed_in?
current_user.teams.each do |team|
Expand Down
7 changes: 4 additions & 3 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class Comment < ActiveRecord::Base
MAX_LENGTH = 8000
attr_accessible :content, :user_id
attr_accessible :content, :user, :user_id
belongs_to :commentable, :polymorphic => true

belongs_to :user, :class_name => "User"

validates_presence_of :content, :user_id
validate :is_of_valid_length

def is_of_valid_length
if content.empty? or content.length > MAX_LENGTH
errors.add(:content, "cannot be empty")
Expand Down
4 changes: 3 additions & 1 deletion app/models/news.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class News < ActiveRecord::Base
attr_accessible :headline, :description, :content, :newsable_id
attr_accessible :headline, :description, :content, :newsable_id, :user, :user_id

belongs_to :newsable, :polymorphic => true
has_many :comments, :as => :commentable, :dependent => :destroy
belongs_to :user
end
3 changes: 2 additions & 1 deletion app/models/tournament.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Tournament < ActiveRecord::Base
attr_accessible :description, :name, :rules, :current_week_num
attr_accessible :description, :name, :rules, :current_week_num, :news

validates_presence_of :name
validates_inclusion_of :active, :in => [true, false]
Expand All @@ -8,6 +8,7 @@ class Tournament < ActiveRecord::Base
has_many :tournament_teams, :dependent => :destroy
has_many :teams, through: :tournament_teams
has_many :matches, :dependent => :destroy
has_many :news, :as => :newsable, :dependent => :destroy

def scheduler
teams = TournamentTeam.where(tournament_id: self.id).order(:rank)
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class User < ActiveRecord::Base
has_many :memberships
has_many :teams, through: :memberships
has_many :tournaments, through: :teams
has_many :news

roles_attribute :roles_mask
roles :admin, :user, :banned
Expand Down
10 changes: 5 additions & 5 deletions app/views/admin/cpanel/_admincp_manage_news.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
</tr>
<% @news.each do |news| %>
<tr>
<td><%= link_to news.title, news_path(news) %></td>
<td><%= news.source%></td>
<td><%= news.user %></td>
<td><%= link_to news.headline, news_path(news) %></td>
<td><%= news.newsable_type %></td>
<td><%= news.user.username %></td>
<td><%= news.created_at.strftime("%B %d,%Y") %></td>
<td><%= link_to "edit", edit_admin_news_path(news), :id => "edit-#{news.id}" %></td>
<td><%= link_to "delete", destroy_admin_news_path(news), method: :delete, data: { confirm: "Are you sure?" }%></td>
<td><%= link_to "edit", edit_admin_news_path(news), :id => "edit-news-#{news.id}" %></td>
<td><%= link_to "delete", admin_news_path(news), method: :delete, id: "delete-news-#{news.id}", data: { confirm: "Are you sure?" }%></td>
</tr>
<% end %>
<% paginate @news, :remote => true %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/admin/news/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>edit news</h1>
<%= render 'admin_news_form' %>
8 changes: 1 addition & 7 deletions app/views/admin/news/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
<h1>add news</h1>
<%= simple_form_for([:admin, @news]) do |f| %>
<%= f.input :newsable_id, collection: @sources, label: "Source" %>
<%= f.input :headline %>
<%= f.input :description %>
<%= f.input :content, as: :text, :input_html => { :rows => "15", :class =>"span5"} %>
<%= f.submit "Save" %>
<% end %>
<%= render 'admin_news_form' %>
2 changes: 1 addition & 1 deletion app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<%= f.input :email, as: :email %>
<%= f.input :password, as: :password %>
<%= f.input :password_confirmation, as: :password %>
<p>Current Role <%= @user.role %></p>
<p>Current Roles: <%= @user.roles %></p>
<%= f.input :roles, collection: [:user, :admin], include_blank: false %>
<%= f.submit "Save" %>
<% end %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/application/_admin_news_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%= simple_form_for([:admin, @news]) do |f| %>
<% if @news.newsable_type.nil? %>
<%= f.input :newsable_id, collection: @sources, label: "Source", include_blank: true %>
<% end %>
<%= f.input :headline %>
<%= f.input :description %>
<%= f.input :content, as: :text, :input_html => { :rows => "15", :class =>"span5"} %>
<%= f.submit "Save" %>
<% end %>
5 changes: 2 additions & 3 deletions app/views/application/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<% @user = User.find(comment.user_id) %>
<div class="comments-inner">
<div>
<span class="important"><%= link_to @user.username, user_path(@user) %></span>
<span class="important"><%= link_to comment.user.username, user_path(comment.user) %></span>
<div style="float:right;" class="faded">
<%= comment.updated_at %>
</div>
</div>
<div>
<%= comment.content %>
</div>
</div>
</div>
14 changes: 14 additions & 0 deletions app/views/application/_index_news.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% if @news.any? %>
<% @news.each do |news| %>
<div class="content-inner-inner">
<p class="headline"><%= news.headline %></p>

<p style="font-size:10px">Created at <%= news.created_at %> by <%= news.user.username %></p>

<p><%= news.description %></p>
<p><%= link_to "read more...", news_path(news)%></a></p>
</div>


<% end %>
<% end %>
13 changes: 6 additions & 7 deletions app/views/application/_show_comments.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<div class="content-outer comments" style="width:500px;">
<div class="content-inner-inner" id="comments">
<h1>comments</h1>
<% @comments = @match.comments %>
<% if @comments.any? %>
<% @comments.each do |comment| %>
<%= render :partial => "comment", :locals => { :comment => comment } %>
<% end %>
<% @comments.each do |comment| %>
<%= render :partial => "comment", :locals => { :comment => comment } %>
<% end %>
<% else %>
No comments yet.
<% end %>
No comments yet.
<% end %>
</div>
</div>
</div>
12 changes: 12 additions & 0 deletions app/views/application/_show_news.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1><%= @news.headline %></h1>
<p style="font-size:10px">Created at <%= @news.created_at %> by <%= @news.user.username %></p>
<div class="content-inner">
<div class="content-inner-inner">
<p><%=raw @news.content %></p>
</div>
<%= render('show_comments') %>
<% if user_signed_in? %>
<%= render('submit_news_comment') %>
<% end %>
</div>

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
<%= f.input :content, :label => false, :as => :text, :input_html => { :cols => 50, :rows => 5 } %>
<%= f.submit "Submit" %>
<% end %>
</div>
</div>
9 changes: 9 additions & 0 deletions app/views/application/_submit_news_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="content-outer comments" style="width:500px;">
<div class="content-inner-inner">
<h1>submit comment</h1>
<%= simple_form_for [ @news, Comment.new ], :id => :comment do |f| %>
<%= f.input :content, :label => false, :as => :text, :input_html => { :cols => 50, :rows => 5 } %>
<%= f.submit "Submit" %>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/home/_home_signed_in.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<% else %>
<div class="tournaments-list">
<% if @tournaments.any? %>
You are participating in
You are participating in
<% num_tournaments = @tournaments.length %>
<% if num_tournaments == 1 %>
<% tournament = @tournaments.first %>
Expand Down
7 changes: 7 additions & 0 deletions app/views/home/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@
<% end %>
</div>
</div>
</div>
<div class="spacer"></div>
<div class="content-outer">
<h1>general news</h1>
<div class="content-inner">
<%= render 'index_news' %>
</div>
4 changes: 3 additions & 1 deletion app/views/matches/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

<%= render 'show_comments' %>

<%= render 'submit_comment' %>
<% if user_signed_in? %>
<%= render 'submit_match_comment' %>
<% end %>

</div>
</div>
10 changes: 3 additions & 7 deletions app/views/news/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
<h1><%= @news.headline %></h1>
<div class="content-inner">
<p><%= @news.description %></p>
<div class="content-inner-inner">
<p><%=raw @news.content %></p>
</div>
</div>
<%= render 'show_news' %>
<%= link_to "Back", :back %>
<div class="spacer"></div>
5 changes: 5 additions & 0 deletions app/views/tournaments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<% end %>
<% end %>
<div class="spacer"></div>
<div class="content-inner-inner clear">
<h1>news</h1>
<div class="spacer"></div>
<%= render('index_news')%>
</div>
<div class="content-inner-inner clear">
<h1>rankings</h1>
<% if @teams.any? %>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Ftwgl::Application.routes.draw do
devise_for :users
resources :news, :only => [:show]
resources :news, :only => [:show] do
resources :comments, :only => [ :create ]
end
resources :user, :only => [:show, :index]
resources :teams, :only => [:new, :create, :show, :edit, :update, :index]
resources :memberships, :only => [:create, :update, :destroy, :index]
Expand Down
Loading

0 comments on commit 715adbe

Please sign in to comment.