Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rendezvous
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
VeNtura
rendezvous
Commits
0d77b39b
Commit
0d77b39b
authored
Dec 01, 2013
by
tady
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
post_tags, author associations
parent
ed1d811a
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
63 additions
and
15 deletions
+63
-15
app/models/post.rb
+3
-0
app/models/post_tag.rb
+4
-0
app/models/tag.rb
+2
-0
app/models/user.rb
+1
-4
app/views/posts/show_fragment.html.erb
+4
-4
db/migrate/20131130112531_create_posts.rb
+1
-0
db/migrate/20131130184007_create_post_tag.rb
+13
-0
db/schema.rb
+12
-1
db/seeds.rb
+23
-6
No files found.
app/models/post.rb
View file @
0d77b39b
class
Post
<
ActiveRecord
::
Base
has_many
:post_tags
has_many
:tags
,
through: :post_tags
belongs_to
:author
,
class_name:
'User'
end
app/models/post_tag.rb
0 → 100644
View file @
0d77b39b
class
PostTag
<
ActiveRecord
::
Base
belongs_to
:post
belongs_to
:tag
end
app/models/tag.rb
View file @
0d77b39b
class
Tag
<
ActiveRecord
::
Base
has_many
:post_tags
has_many
:posts
,
through: :post_tags
end
app/models/user.rb
View file @
0d77b39b
require
'pp'
class
User
<
ActiveRecord
::
Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
...
...
@@ -7,14 +5,13 @@ class User < ActiveRecord::Base
:recoverable
,
:rememberable
,
:trackable
,
:validatable
devise
:omniauthable
,
:omniauth_providers
=>
[
:google_oauth2
]
has_many
:posts
# Device
def
self
.
find_for_google_oauth2
(
access_token
,
signed_in_resource
=
nil
)
data
=
access_token
.
info
user
=
User
.
where
(
:email
=>
data
[
"email"
]).
first
pp
data
unless
user
user
=
User
.
create
(
name:
data
[
"name"
],
image_url:
data
[
"image"
],
...
...
app/views/posts/show_fragment.html.erb
View file @
0d77b39b
<div
class=
"text-box title"
>
<%=
@post
.
title
%>
</div>
<div
class=
"text-box meta"
>
<
span
class=
"label label-info"
>
#Rails (4.x.x)
</span
>
<span
class=
"label label-info"
>
#ruby
</span>
<
span
class=
"label label-info"
>
#日報
</span
>
<span
class=
"label label-success"
>
@
tady
</span>
<
%
@post
.
tags
.
each
do
|
tag
|
%
>
<span
class=
"label label-info"
>
#
<%=
tag
.
name
%>
</span>
<
%
end
%
>
<span
class=
"label label-success"
>
@
<%=
@post
.
author
.
name
%>
</span>
<span
class=
"label label-danger"
>
<%=
@post
.
updated_at
.
strftime
(
'%Y-%m-%d'
)
%>
</span>
</div>
<div
class=
"text-box body"
>
<%=
h_application_format_markdown
(
@post
.
body
)
%>
</div>
...
...
db/migrate/20131130112531_create_posts.rb
View file @
0d77b39b
...
...
@@ -3,6 +3,7 @@ class CreatePosts < ActiveRecord::Migration
create_table
:posts
do
|
t
|
t
.
string
:title
t
.
text
:body
t
.
integer
:author_id
t
.
timestamps
end
...
...
db/migrate/20131130184007_create_post_tag.rb
0 → 100644
View file @
0d77b39b
class
CreatePostTag
<
ActiveRecord
::
Migration
def
change
create_table
:post_tags
do
|
t
|
t
.
integer
:post_id
,
null:
false
t
.
integer
:tag_id
,
null:
false
t
.
timestamps
end
add_index
:post_tags
,
:post_id
add_index
:post_tags
,
:tag_id
end
end
db/schema.rb
View file @
0d77b39b
...
...
@@ -11,11 +11,22 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
20131130112531
)
do
ActiveRecord
::
Schema
.
define
(
version:
20131130184007
)
do
create_table
"post_tags"
,
force:
true
do
|
t
|
t
.
integer
"post_id"
,
null:
false
t
.
integer
"tag_id"
,
null:
false
t
.
datetime
"created_at"
t
.
datetime
"updated_at"
end
add_index
"post_tags"
,
[
"post_id"
],
name:
"index_post_tags_on_post_id"
add_index
"post_tags"
,
[
"tag_id"
],
name:
"index_post_tags_on_tag_id"
create_table
"posts"
,
force:
true
do
|
t
|
t
.
string
"title"
t
.
text
"body"
t
.
integer
"author_id"
t
.
datetime
"created_at"
t
.
datetime
"updated_at"
end
...
...
db/seeds.rb
View file @
0d77b39b
...
...
@@ -7,17 +7,34 @@
# Mayor.create(name: 'Emanuel', city: cities.first)
# Tag
tags
=
[]
%w(JavaScript HTML CSS iPhone PHP Ruby Android Qiita Java Objective-C Vim Python ShellScript C++ C CoffeeScript Emacs git Perl jQuery
)
.
each
do
|
tag
|
puts
"[Seed Tag]
#{
tag
}
"
Tag
.
find_or_create_by
(
name:
tag
)
)
.
each
do
|
tag
|
puts
"[Seed Tag]
#{
tag
}
"
tags
<<
Tag
.
find_or_create_by
(
name:
tag
)
end
# User
user
=
User
.
find_or_initialize_by
(
name:
'山田太郎'
)
user
.
update_attributes!
(
email:
"
#{
Devise
.
friendly_token
[
0
,
20
]
}
@example.com"
,
password:
Devise
.
friendly_token
[
0
,
20
])
user
=
User
.
find_or_initialize_by
(
name:
'鈴木二郎'
)
user
.
update_attributes!
(
email:
"
#{
Devise
.
friendly_token
[
0
,
20
]
}
@example.com"
,
password:
Devise
.
friendly_token
[
0
,
20
])
user
=
User
.
find_or_initialize_by
(
name:
'田中三郎'
)
user
.
update_attributes!
(
email:
"
#{
Devise
.
friendly_token
[
0
,
20
]
}
@example.com"
,
password:
Devise
.
friendly_token
[
0
,
20
])
# Post
users
=
User
.
all
Dir
.
glob
(
Rails
.
root
.
join
(
'db'
,
'seeds'
).
to_s
+
'/*'
).
each
do
|
file_name
|
puts
"[Post Tag]
#{
file_name
}
"
title
=
file_name
.
split
(
'/'
).
last
Post
.
find_or_create_by
(
title:
title
)
do
|
post
|
post
.
body
=
File
.
read
(
file_name
)
end
post
=
Post
.
find_or_initialize_by
(
title:
title
)
post
.
body
=
File
.
read
(
file_name
)
post
.
author
=
users
.
sample
post
.
tags
=
[
tags
.
sample
,
tags
.
sample
]
post
.
save!
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment