Skip to content

Commit

Permalink
Merge pull request #539 from ruby-china/use-activerecord
Browse files Browse the repository at this point in the history
Use ActiveRecord
  • Loading branch information
huacnlee committed Jan 22, 2016
2 parents e743374 + c838f86 commit d13f275
Show file tree
Hide file tree
Showing 101 changed files with 1,358 additions and 1,465 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ chromedriver.log
.idea/
spec/examples.txt
.vagrant
.byebug_history
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ env:
- USE_OFFICIAL_GEM_SOURCE=1

services:
- mongodb
- postgresql
- memcached
- redis-server
- elasticsearch
Expand All @@ -12,14 +12,14 @@ rvm:
- 2.3.0

before_script:
- mongo ruby_china_test --eval 'db.addUser("travis", "test");'
- psql -c "create database \"ruby-china-test\";" -U postgres
- cp config/config.yml.default config/config.yml
- cp config/mongoid.yml.default config/mongoid.yml
- cp config/database.yml.default config/database.yml
- cp config/redis.yml.default config/redis.yml
- cp config/elasticsearch.yml.default config/elasticsearch.yml
- cp config/secrets.yml.default config/secrets.yml
- sed -i "s/SETUP_DEVELOPMENT_HOST/127.0.0.1:27017/g" config/mongoid.yml
- sed -i "s/SETUP_REDIS_HOST/127.0.0.1/g" config/redis.yml
- sed -i "s/SETUP_REDIS_PORT/6379/g" config/redis.yml
- RAILS_ENV=test bundle exec rake db:migrate

script: RAILS_ENV=test bundle exec rake
15 changes: 5 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ gem 'mini_magick'
gem 'rucaptcha'
gem 'letter_avatar'

# Mongoid 辅助插件
gem 'mongoid', '5.0.0'
gem 'mongoid_auto_increment_id'
gem 'mongoid_rails_migrations'
gem 'pg'
gem 'activerecord_any_of'

# remove this after migrate MongoDB into PostgreSQL
gem 'mongo', require: false

# 用户系统
gem 'devise', '~> 3.5.1'
Expand Down Expand Up @@ -115,10 +116,6 @@ gem 'rack-utf8_sanitizer'
# Mini profiler
gem 'rack-mini-profiler', require: false

# gem 'newrelic_rpm'
# gem 'newrelic_moped'
# gem 'newrelic-grape'

gem 'oneapm_rpm'

group :development do
Expand Down Expand Up @@ -149,7 +146,5 @@ group :development, :test do
gem 'better_errors'
gem 'binding_of_caller'

gem 'tunemygc'

gem 'bundler-audit', require: false
end
38 changes: 10 additions & 28 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ GEM
activemodel (= 4.2.5)
activesupport (= 4.2.5)
arel (~> 6.0)
activerecord_any_of (1.3)
activerecord (>= 3.2.13, < 5)
activesupport (4.2.5)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
Expand All @@ -57,8 +59,6 @@ GEM
sass-rails
arel (6.0.3)
ast (2.2.0)
astrolabe (1.3.1)
parser (~> 2.2)
auto-space (0.0.4)
activesupport (> 3.0.0)
axiom-types (0.1.1)
Expand Down Expand Up @@ -241,19 +241,6 @@ GEM
minitest (5.8.3)
mongo (2.2.1)
bson (~> 4.0)
mongoid (5.0.0)
activemodel (~> 4.0)
mongo (~> 2.1)
origin (~> 2.1)
tzinfo (>= 0.3.37)
mongoid_auto_increment_id (0.8.1)
mongoid (>= 5.0.0, < 6.0.0)
mongoid_rails_migrations (1.1.0)
activesupport (>= 4.2.0)
bundler (>= 1.0.0)
mongoid (>= 4.0.0)
rails (>= 4.2.0)
railties (>= 4.2.0)
multi_json (1.11.2)
multi_xml (0.5.5)
multipart-post (2.0.0)
Expand Down Expand Up @@ -282,11 +269,11 @@ GEM
oauth2 (~> 1.0)
omniauth (~> 1.2)
oneapm_rpm (1.3.3)
origin (2.1.1)
orm_adapter (0.5.0)
parallel (1.6.1)
parser (2.2.3.0)
ast (>= 1.1, < 3.0)
parser (2.3.0.1)
ast (~> 2.2)
pg (0.18.4)
phantomjs (1.9.8.0)
posix-spawn (0.3.11)
postmark (0.9.15)
Expand Down Expand Up @@ -372,13 +359,11 @@ GEM
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
rubocop (0.35.1)
astrolabe (~> 1.3)
parser (>= 2.2.3.0, < 3.0)
rubocop (0.36.0)
parser (>= 2.3.0.0, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
tins (<= 1.6.0)
ruby-prof (0.15.9)
ruby-progressbar (1.7.5)
rucaptcha (0.3.2)
Expand Down Expand Up @@ -416,8 +401,6 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.1)
tins (1.6.0)
tunemygc (1.0.62)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (2.7.2)
Expand All @@ -444,6 +427,7 @@ PLATFORMS
DEPENDENCIES
actionpack-action_caching
active_model_serializers
activerecord_any_of
api_taster (= 0.6.0)
auto-space (= 0.0.4)
better_errors
Expand Down Expand Up @@ -485,14 +469,13 @@ DEPENDENCIES
md_emoji
message_bus
mini_magick
mongoid (= 5.0.0)
mongoid_auto_increment_id
mongoid_rails_migrations
mongo
nokogiri
omniauth (~> 1.2.2)
omniauth-github (~> 1.1.0)
oneapm_rpm
parallel
pg
postmark (= 0.9.15)
postmark-rails (= 0.4.1)
puma (~> 2.14.0)
Expand Down Expand Up @@ -520,7 +503,6 @@ DEPENDENCIES
sinatra
social-share-button (= 0.1.5)
sprockets
tunemygc
turbolinks!
uglifier
unicorn (= 5.0.0)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ApplicationController < ::ApplicationController
before_action :set_active_menu

def require_admin
render_404 unless Setting.admin_emails.include?(current_user.email)
render_404 unless current_user.admin?
end

def set_active_menu
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/applications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cpanel
class ApplicationsController < ApplicationController
def index
@applications = Doorkeeper::Application.desc('_id').paginate(page: params[:page], per_page: 20)
@applications = Doorkeeper::Application.order(id: :desc).paginate(page: params[:page], per_page: 20)

respond_to do |format|
format.html # index.html.erb
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/exception_logs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cpanel
class ExceptionLogsController < ApplicationController
def index
@exception_logs = ExceptionLog.desc('_id').paginate(page: params[:page], per_page: 20)
@exception_logs = ExceptionLog.order(id: :desc).paginate(page: params[:page], per_page: 20)

respond_to do |format|
format.html # index.html.erb
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cpanel
class PagesController < ApplicationController
def index
@pages = Page.unscoped.desc(:_id).paginate page: params[:page], per_page: 30
@pages = Page.unscoped.order(id: :desc).paginate page: params[:page], per_page: 30
end

def show
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/replies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cpanel
class RepliesController < ApplicationController
def index
@replies = Reply.unscoped.desc(:_id).includes(:topic, :user).paginate page: params[:page], per_page: 30
@replies = Reply.unscoped.order(id: :desc).includes(:topic, :user).paginate page: params[:page], per_page: 30
end

def show
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/site_configs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cpanel
class SiteConfigsController < ApplicationController
def index
@site_configs = SiteConfig.desc(:_id)
@site_configs = SiteConfig.order(id: :desc)
end

def edit
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/site_nodes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cpanel
class SiteNodesController < ApplicationController
def index
@site_nodes = SiteNode.desc('_id').paginate(page: params[:page], per_page: 20)
@site_nodes = SiteNode.order(id: :desc).paginate(page: params[:page], per_page: 20)

respond_to do |format|
format.html # index.html.erb
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/topics_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Cpanel
class TopicsController < ApplicationController
def index
@topics = Topic.unscoped.desc(:_id).includes(:user).paginate page: params[:page], per_page: 30
@topics = Topic.unscoped.order(id: :desc).includes(:user).paginate page: params[:page], per_page: 30
end

def show
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/cpanel/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def index
if params[:q]
@users = @users.where(login: /#{params[:q]}/)
end
@users = @users.desc(:_id).paginate page: params[:page], per_page: 30
@users = @users.order(id: :desc).paginate page: params[:page], per_page: 30
end

def show
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def index
@node = Node.find(Node.jobs_id)
@suggest_topics = Topic.where(node_id: @node.id).suggest.limit(3)
suggest_topic_ids = @suggest_topics.map(&:id)
@topics = @node.topics.last_actived.fields_for_list.where(:_id.nin => suggest_topic_ids).includes(:user).paginate(page: params[:page], per_page: 15)
@topics = @node.topics.last_actived.fields_for_list
@topics = @topics.where.not(id: suggest_topic_ids) if suggest_topic_ids.count > 0
@topics = @topics.includes(:user).paginate(page: params[:page], per_page: 15)
set_seo_meta("#{@node.name} &raquo; #{t('menu.topics')}", "#{Setting.app_name}#{t('menu.topics')}#{@node.name}", @node.summary)
render '/topics/index'
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def new
end

def edit
@page = Page.find(params[:id])
@page = Page.find_by_slug(params[:id])
set_seo_meta t('pages.edit_wiki_page')
end

Expand All @@ -63,7 +63,7 @@ def create
end

def update
@page = Page.find(params[:id])
@page = Page.find_by_slug(params[:id])
@page.version_enable = true
@page.user_id = current_user.id

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class SitesController < ApplicationController
load_and_authorize_resource

def index
@site_nodes = SiteNode.all.desc('sort')
@site_nodes = SiteNode.all.order(sort: :desc)
set_seo_meta("Ruby #{t('menu.sites')}")
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def index
@topics = @topics.without_hide_nodes
end
@topics = @topics.fields_for_list
@topics = @topics.paginate(page: params[:page], per_page: 22, total_entries: 1500)
@topics = @topics.paginate(page: params[:page], per_page: 22, total_entries: 1500).to_a
end
@threads.each(&:join)

Expand Down Expand Up @@ -84,12 +84,12 @@ def show
@show_raw = params[:raw] == '1'

@threads << Thread.new do
@replies = @topic.replies.unscoped.without_body.asc(:_id).all
@replies = Reply.unscoped.where(topic_id: @topic.id).without_body.order(:id).all

check_current_user_liked_replies
check_current_user_status_for_topic
end

check_current_user_status_for_topic
set_special_node_active_menu

@threads.each(&:join)
Expand All @@ -114,7 +114,7 @@ def check_current_user_status_for_topic

@threads << Thread.new do
# 通知处理
current_user.read_topic(@topic)
current_user.read_topic(@topic, replies_ids: @replies.collect(&:id))
end

# 是否关注过
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def show
end

def topics
@topics = @user.topics.unscoped.fields_for_list.recent.paginate(page: params[:page], per_page: 40)
@topics = @user.topics.fields_for_list.recent.paginate(page: params[:page], per_page: 40)
set_seo_meta("#{@user.login} 的帖子")
end

Expand All @@ -31,7 +31,7 @@ def replies

def favorites
@topic_ids = @user.favorite_topic_ids.reverse.paginate(page: params[:page], per_page: 40)
@topics = Topic.where(:_id.in => @topic_ids).fields_for_list
@topics = Topic.where(id: @topic_ids).fields_for_list
@topics = @topics.to_a.sort do |a, b|
@topic_ids.index(a.id) <=> @topic_ids.index(b.id)
end
Expand Down Expand Up @@ -60,13 +60,13 @@ def update_private_token
end

def city
@location = Location.find_by_name(params[:id])
@location = Location.location_find_by_name(params[:id])
if @location.blank?
render_404
return
end

@users = User.where(location_id: @location.id).fields_for_list.desc('replies_count').paginate(page: params[:page], per_page: 60)
@users = User.where(location_id: @location.id).fields_for_list.order(replies_count: :desc).paginate(page: params[:page], per_page: 60)

render_404 if @users.count == 0
end
Expand All @@ -86,7 +86,7 @@ def blocked
render_404
end

@blocked_users = User.where(:_id.in => current_user.blocked_user_ids).paginate(page: params[:page], per_page: 20)
@blocked_users = User.where(id: current_user.blocked_user_ids).paginate(page: params[:page], per_page: 20)
end

def follow
Expand Down
10 changes: 1 addition & 9 deletions app/models/authorization.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
class Authorization
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::BaseModel

field :provider
field :uid, type: String
embedded_in :user, inverse_of: :authorizations

class Authorization < ActiveRecord::Base
validates :uid, :provider, presence: true
validates :uid, uniqueness: { scope: :provider }
end
Loading

0 comments on commit d13f275

Please sign in to comment.