Skip to content

Commit

Permalink
添加分类的friendly id
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarck committed Apr 9, 2017
1 parent 249fdd8 commit 6926d47
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@

/public/uploads/*
/public/assets/*

config/config.yml
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ gem 'redis-namespace'
# Redis 保存阅读量
gem 'redis-objects'

# friendly id
gem 'friendly_id', '~> 5.1.0'

# 队列
gem 'mina-sidekiq', :require => false
gem 'sidekiq'
Expand All @@ -75,6 +78,7 @@ group :development, :test do
gem 'byebug', platform: :mri

gem 'rspec-rails', '~> 3.5.2'
gem 'rspec-collection_matchers'
gem 'rails-controller-testing'
gem 'factory_girl_rails'
end
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ GEM
faker (1.7.3)
i18n (~> 0.5)
ffi (1.9.18)
friendly_id (5.1.0)
activerecord (>= 4.0.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
http_accept_language (2.1.0)
Expand Down Expand Up @@ -177,6 +179,8 @@ GEM
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
rolify (5.1.0)
rspec-collection_matchers (1.1.3)
rspec-expectations (>= 2.99.0.beta1)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
Expand Down Expand Up @@ -271,6 +275,7 @@ DEPENDENCIES
devise
factory_girl_rails
faker
friendly_id (~> 5.1.0)
http_accept_language
jbuilder (~> 2.5)
jquery-rails
Expand All @@ -287,6 +292,7 @@ DEPENDENCIES
redis-namespace
redis-objects
rolify
rspec-collection_matchers
rspec-rails (~> 3.5.2)
sass-rails (~> 5.0)
selenium-webdriver
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class NodesController < ApplicationController
def show
node_id = params[:id]

@topics = Topic.where(node_id: node_id).paginate(page: params[:page], :per_page => 10).order('created_at DESC')
@topics = Node.friendly.find(node_id).topics.paginate(page: params[:page], :per_page => 10).order('created_at DESC')
render '/topics/index'
end

Expand Down
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ApplicationMailer < ActionMailer::Base
default from: Setting.email_sender
default from: Setting.email_form
default charset: 'utf-8'
default content_type: 'text/html'

Expand Down
4 changes: 4 additions & 0 deletions app/models/node.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class Node < ApplicationRecord
extend FriendlyId

has_many :topics

friendly_id :name, use: :slugged
end
1 change: 1 addition & 0 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Topic < ApplicationRecord

include Redis::Objects
include MarkdownBody
include SoftDelete
Expand Down
22 changes: 0 additions & 22 deletions config/config.yml

This file was deleted.

1 change: 1 addition & 0 deletions config/config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defaults: &defaults
port: 3000

# 发送邮箱设置
email_form: 'Your name <info@you_site.com>'
email_address: 'smtp.mxhichina.com'
email_port: 587
email_domain: 'qiye.aliyun.com/alimail'
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
}
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

# sidekiq管理界面
require 'sidekiq/web'
authenticate :user, ->(u) { u.has_role? :admin} do
mount Sidekiq::Web => '/sidekiq'
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20170409074656_add_slug_to_nodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSlugToNodes < ActiveRecord::Migration[5.0]
def change
add_column :nodes, :slug, :string
add_index :nodes, :slug, unique: true
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170325075536) do
ActiveRecord::Schema.define(version: 20170409074656) do

create_table "nodes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name", comment: "英文名"
t.string "title", comment: "显示名称"
t.boolean "publish", default: false, comment: "是否公开,默认false"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.index ["slug"], name: "index_nodes_on_slug", unique: true, using: :btree
end

create_table "pictures", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
Expand Down
10 changes: 5 additions & 5 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
])

Node.create!([
{name: 'private', title: '私有', publish: false},
{name: 'ruby', title: 'Ruby', publish: true},
{name: 'rails', title: 'Rails', publish: true},
{name: 'linux', title: 'Linux', publish: true},
{name: 'elasticsearch', title: 'Elasticsearch', publish: true}
{name: 'private', slug: 'private', title: '私有', publish: false},
{name: 'ruby', slug: 'ruby', title: 'Ruby', publish: true},
{name: 'rails', slug: 'rails', title: 'Rails', publish: true},
{name: 'linux', slug: 'linux', title: 'Linux', publish: true},
{name: 'elasticsearch', slug: 'elasticsearch', title: 'Elasticsearch', publish: true}
])

puts "seed end"
7 changes: 7 additions & 0 deletions spec/models/topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@
describe 'data valiad' do
it 'is valid with a user_id, title and body' do
topic = FactoryGirl.build(:topic)

expect(topic).to be_valid
end

it 'is invalid without a user_id' do
topic = FactoryGirl.build(:topic, user_id: nil)

expect(topic).not_to be_valid
expect(topic).to have(1).error_on(:user_id)
end

it 'is invalid without a title' do
topic = FactoryGirl.build(:topic, title: nil)

expect(topic).not_to be_valid
expect(topic).to have(1).error_on(:title)
end

it 'is invalid without a body' do
topic = FactoryGirl.build(:topic, body: nil)

expect(topic).not_to be_valid
expect(topic).to have(1).error_on(:body)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Add additional requires below this line. Rails is not loaded until this point!

require 'devise'
require 'rspec/collection_matchers'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand Down

0 comments on commit 6926d47

Please sign in to comment.