Skip to content

Commit

Permalink
posts, topics, forums tests, controllers, models, views
Browse files Browse the repository at this point in the history
  • Loading branch information
ashalaev committed Apr 8, 2013
1 parent ac53773 commit ed054cb
Show file tree
Hide file tree
Showing 49 changed files with 16,060 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Rakefile
@@ -1 +1 @@
require "bundler/gem_tasks"
require 'bundler/gem_tasks'
4 changes: 1 addition & 3 deletions app/assets/javascripts/ruboard/application.js
Expand Up @@ -10,6 +10,4 @@
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require rails-timeago
2 changes: 2 additions & 0 deletions app/assets/javascripts/ruboard/forums.js
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/ruboard/posts.js
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/ruboard/topics.js
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/ruboard/forums.css
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
4 changes: 4 additions & 0 deletions app/assets/stylesheets/ruboard/posts.css
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
4 changes: 4 additions & 0 deletions app/assets/stylesheets/ruboard/topics.css
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
15 changes: 15 additions & 0 deletions app/controllers/ruboard/forums_controller.rb
@@ -0,0 +1,15 @@
require_dependency 'ruboard/application_controller'

module Ruboard
class ForumsController < Ruboard::ApplicationController

def index
@forums = Ruboard::Forum.with_topics
end

def show
@forum = Ruboard::Forum.with_topics.find(params[:id])
end

end
end
6 changes: 6 additions & 0 deletions app/controllers/ruboard/posts_controller.rb
@@ -0,0 +1,6 @@
require_dependency "ruboard/application_controller"

module Ruboard
class PostsController < ApplicationController
end
end
19 changes: 19 additions & 0 deletions app/controllers/ruboard/topics_controller.rb
@@ -0,0 +1,19 @@
require_dependency 'ruboard/application_controller'

module Ruboard
class TopicsController < Ruboard::ApplicationController

before_filter :find_forum

def show
@topic = @forum.topics.find(params[:id])
@posts = @topic.posts.includes(:user)
end

private
def find_forum
@forum = Ruboard::Forum.find(params[:forum_id])
end

end
end
6 changes: 6 additions & 0 deletions app/models/ruboard/forum.rb
Expand Up @@ -14,5 +14,11 @@ class Forum < ActiveRecord::Base

friendly_id :title, :use => :slugged

class << self
def with_topics
includes(:topics)
end
end

end
end
11 changes: 8 additions & 3 deletions app/models/ruboard/post.rb
@@ -1,8 +1,13 @@
module Ruboard
class Post < ActiveRecord::Base
attr_accessible :body, :user

belongs_to :topic
belongs_to :user
attr_accessible :body, :user, :body

belongs_to :topic, :counter_cache => true
belongs_to :user, :class_name => Ruboard.user_class.to_s

validates :body, :presence => true

delegate :name, :to => :user, :prefix => true, :allow_nil => true
end
end
35 changes: 33 additions & 2 deletions app/models/ruboard/topic.rb
@@ -1,9 +1,40 @@
require 'friendly_id'

module Ruboard
class Topic < ActiveRecord::Base
extend FriendlyId

attr_accessible :title, :forum, :user

has_many :posts, :dependent => :destroy
belongs_to :user
has_many :posts, :dependent => :destroy, :order => 'ruboard_posts.created_at DESC'
has_many :views, :as => :viewable
belongs_to :user, :class_name => Ruboard.user_class.to_s
belongs_to :forum

validates :title, :presence => true, :length => { :minimum => 3, :maximum => 250 }

friendly_id :title, :use => :slugged

accepts_nested_attributes_for :posts

before_save :set_first_post_user

def register_view_by(user)
return unless user

view = self.views.find_or_create_by_user_id(user.id)
view.increment!(:count)
self.increment!(:views_count)
end

def view_for(user)
self.views.find_by_user_id(user.id)
end

private
def set_first_post_user
post = self.posts.first
post.user = self.user
end
end
end
10 changes: 10 additions & 0 deletions app/models/ruboard/view.rb
@@ -0,0 +1,10 @@
module Ruboard
class View < ActiveRecord::Base
attr_accessible :user, :count

belongs_to :user, :class_name => Ruboard.user_class.to_s
belongs_to :viewable, :polymorphic => true

validates :viewable_id, :viewable_type, :presence => true
end
end
3 changes: 3 additions & 0 deletions app/views/ruboard/forums/_forum.html.erb
@@ -0,0 +1,3 @@
<div class="ruboardForums__forum" id="forum_<%= forum.id %>">
<%= render 'ruboard/topics/list', :forum => forum %>
</div>
3 changes: 3 additions & 0 deletions app/views/ruboard/forums/index.html.erb
@@ -0,0 +1,3 @@
<div class="ruboardForums">
<%= render @forums %>
</div>
3 changes: 3 additions & 0 deletions app/views/ruboard/forums/show.html.erb
@@ -0,0 +1,3 @@
<div class="ruboardForums">
<%= render @forum %>
</div>
10 changes: 10 additions & 0 deletions app/views/ruboard/posts/_list.html.erb
@@ -0,0 +1,10 @@
<table class="ruboardTopics__posts table table-bordered">
<thead>
<tr>
<th colspan="2" class="ruboardTopics__posts__title"><%= link_to topic.title, ruboard.forum_topic_path(forum, topic) %></th>
</tr>
</thead>
<tbody>
<%= render posts, :forum => forum, :topic => topic %>
</tbody>
</table>
30 changes: 30 additions & 0 deletions app/views/ruboard/posts/_post.html.erb
@@ -0,0 +1,30 @@
<tr>
<td><%= post.user_name %></td>
<td>
<div class="ruboardTopics__posts__info" id="post-<%= post.id %>">
<div class="ruboardTopics__posts__info__created">
<%= "#{t('activerecord.attributes.post.created_at')}: #{l(post.created_at, :format => :ruboard_full)}" %>
</div>
<div class="ruboardTopics__posts__info__number">
<%= link_to "##{post_counter + 1}", "#post-#{post.id}" %>
</div>
</div>
</td>
</tr>
<tr>
<td rowspan="3" class="ruboardTopics__posts__user">
Avatar
<p><%= t('ruboard.user.posts') %>: 0</p>
</td>
</tr>
<tr>
<td colspan="2">
<div class="ruboardTopics__posts__body">
<%= post.body %>
</div>
</td>
</tr>
<tr>
<td>Forum Sign</td>
<td>Menu</td>
</tr>
13 changes: 13 additions & 0 deletions app/views/ruboard/topics/_list.html.erb
@@ -0,0 +1,13 @@
<table class="ruboardForums__topics table table-bordered">
<thead>
<tr>
<th colspan="2"><%= link_to forum.title, ruboard.forum_path(forum) %></th>
<th><%= t('activerecord.attributes.topic.answers') %></th>
<th><%= t('activerecord.attributes.topic.views') %></th>
<th><%= t('activerecord.attributes.topic.updates') %></th>
</tr>
</thead>
<tbody>
<%= render forum.topics, :forum => forum %>
</tbody>
</table>
9 changes: 9 additions & 0 deletions app/views/ruboard/topics/_topic.html.erb
@@ -0,0 +1,9 @@
<tr>
<td>Status</td>
<td>
<%= link_to topic.title, ruboard.forum_topic_path(forum, topic) %>
</td>
<td><%= topic.posts.size %></td>
<td><%= topic.views_count %></td>
<td><%= l(topic.updated_at, :format => :ruboard_full) %></td>
</tr>
3 changes: 3 additions & 0 deletions app/views/ruboard/topics/show.html.erb
@@ -0,0 +1,3 @@
<div class="ruboardTopics">
<%= render 'ruboard/posts/list', :topic => @topic, :forum => @forum, :posts => @posts %>
</div>
12 changes: 12 additions & 0 deletions config/locales/en.yml
@@ -0,0 +1,12 @@
en:
activerecord:
models:
topic:
one: 'Topic'
other: 'Topics'
attributes:
topic:
title: 'Subject'
answers: 'Answers'
views: 'Views'
updates: 'Updates'
20 changes: 20 additions & 0 deletions config/locales/ru.yml
@@ -0,0 +1,20 @@
ru:
activerecord:
models:
topic:
one: 'Тема'
few: 'Темы'
many: 'Тем'
other: 'Темы'
attributes:
topic:
title: 'Название'
answers: 'Ответов'
views: 'Просмотров'
updates: 'Обновления'
post:
body: 'Сообщение'
created_at: 'Отправлено'
time:
formats:
ruboard_full: '%d %B %Y %H:%M'
5 changes: 5 additions & 0 deletions config/routes.rb
@@ -1,2 +1,7 @@
Ruboard::Engine.routes.draw do
root :to => 'forums#index'

resources :forums, :only => [ :index, :show ] do
resources :topics, :only => [ :show ]
end
end
3 changes: 3 additions & 0 deletions db/migrate/20130317093433_create_ruboard_topics.rb
@@ -1,15 +1,18 @@
class CreateRuboardTopics < ActiveRecord::Migration
def change
create_table :ruboard_topics do |t|
t.string :title, :null => false
t.integer :forum_id, :null => false
t.integer :user_id, :null => false
t.boolean :closed, :null => false, :default => false
t.boolean :pinned, :null => false, :default => false
t.string :slug

t.timestamps
end

add_index :ruboard_topics, :forum_id
add_index :ruboard_topics, :user_id
add_index :ruboard_topics, :slug, :unique => true
end
end
24 changes: 24 additions & 0 deletions db/migrate/20130408083406_create_ruboard_views.rb
@@ -0,0 +1,24 @@
class CreateRuboardViews < ActiveRecord::Migration
def self.up

add_column :ruboard_topics, :views_count, :integer, :default => 0

create_table :ruboard_views do |t|
t.integer :user_id
t.integer :viewable_id
t.string :viewable_type
t.datetime :current_viewed_at
t.datetime :past_viewed_at
t.integer :count, :default => 0
t.timestamps
end

add_index :ruboard_views, [:viewable_id, :viewable_type], :name => 'ruboard_views_viewable_idx'
add_index :ruboard_views, :user_id
end

def self.down
remove_column :ruboard_topics, :views_count
drop_table :ruboard_views
end
end
14 changes: 14 additions & 0 deletions db/migrate/20130408103422_add_cache_counter_to_topics.rb
@@ -0,0 +1,14 @@
class AddCacheCounterToTopics < ActiveRecord::Migration
def self.up
add_column :ruboard_topics, :posts_count, :integer, :default => 0

Ruboard::Topic.reset_column_information
Ruboard::Topic.find_each do |t|
Ruboard::Topic.update_counters t.id, :posts_count => t.posts.length
end
end

def self.down
remove_column :ruboard_topics, :posts_count
end
end
14 changes: 12 additions & 2 deletions lib/ruboard.rb
@@ -1,6 +1,16 @@
require "ruboard/version"
require 'ruboard/version'
require 'ruboard/engine'

module Ruboard
# Your code goes here...
mattr_accessor :user_class

class << self
def user_class
begin
Object.const_get(@@user_class)
rescue NameError
@@user_class.constantize
end
end
end
end
5 changes: 5 additions & 0 deletions ruboard.gemspec
Expand Up @@ -19,7 +19,12 @@ Gem::Specification.new do |gem|

gem.add_development_dependency 'rspec-rails', '~> 2.6'
gem.add_development_dependency 'factory_girl_rails', '~> 1.7'
gem.add_development_dependency 'database_cleaner'
gem.add_development_dependency 'devise'
gem.add_development_dependency 'jquery-rails'
gem.add_development_dependency 'russian'

gem.add_dependency 'rails', ['>= 3.1.10', '< 3.3'] | 0.upto(10).map{|i| "!= 3.2.#{i}"}
gem.add_dependency 'friendly_id', '~> 4.0', '>= 4.0.9'
gem.add_dependency 'rails-timeago', '~> 2.0'
end

0 comments on commit ed054cb

Please sign in to comment.