Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sample_app
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
1
Merge Requests
1
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
Tan Phat Nguyen
sample_app
Commits
e1383332
Commit
e1383332
authored
Nov 07, 2014
by
Tan Phat Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modify coding convention
parent
974672c5
Show whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
37 additions
and
34 deletions
+37
-34
.gitignore
+1
-0
app/controllers/application_controller.rb
+0
-0
app/controllers/microposts_controller.rb
+5
-4
app/controllers/sessions_controller.rb
+1
-0
app/controllers/static_pages_controller.rb
+5
-2
app/controllers/users_controller.rb
+8
-10
app/helpers/sessions_helper.rb
+0
-0
app/models/micropost.rb
+5
-3
app/models/user.rb
+7
-12
app/views/layouts/_header.html.erb
+1
-1
app/views/users/_fields.html.erb
+2
-1
public/uploads/tmp/1415329593-22750-2891/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415330857-23112-9064/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415330937-23112-9278/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415331043-23112-2022/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415331096-23112-2706/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415331173-23112-4424/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415331207-23319-3582/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415331242-23319-3246/_78796825_hltau_nrao.jpg
+0
-0
public/uploads/tmp/1415331446-23319-2503/_78796825_hltau_nrao.jpg
+0
-0
test/integration/microposts_interface_test.rb
+0
-0
test/models/user_test.rb
+2
-1
No files found.
.gitignore
View file @
e1383332
...
@@ -14,3 +14,4 @@
...
@@ -14,3 +14,4 @@
# Ignore all logfiles and tempfiles.
# Ignore all logfiles and tempfiles.
/log/*.log
/log/*.log
/tmp
/tmp
#/public/uploads
app/controllers/application_controller.rb
View file @
e1383332
app/controllers/microposts_controller.rb
View file @
e1383332
...
@@ -3,19 +3,20 @@ class MicropostsController < ApplicationController
...
@@ -3,19 +3,20 @@ class MicropostsController < ApplicationController
before_action
:correct_user
,
only: :destroy
before_action
:correct_user
,
only: :destroy
def
create
def
create
@feed_items
=
current_user
.
microposts
.
paginate
(
page:
params
[
:page
])
@micropost
=
current_user
.
microposts
.
build
(
micropost_params
)
@micropost
=
current_user
.
microposts
.
build
(
micropost_params
)
if
@micropost
.
save
if
@micropost
.
save
flash
[
:success
]
=
"Micropost created!"
flash
[
:success
]
=
'Micropost created!'
redirect_to
root_url
redirect_to
root_url
else
else
@feed_items
=
[]
render
'static_pages/home'
render
'static_pages/home'
end
end
end
end
def
destroy
def
destroy
@micropost
.
destroy
@micropost
.
destroy
flash
[
:success
]
=
"Micropost deleted"
flash
[
:success
]
=
'Micropost deleted'
redirect_to
request
.
referrer
||
root_url
redirect_to
request
.
referrer
||
root_url
end
end
...
@@ -27,6 +28,6 @@ class MicropostsController < ApplicationController
...
@@ -27,6 +28,6 @@ class MicropostsController < ApplicationController
def
correct_user
def
correct_user
@micropost
=
current_user
.
microposts
.
find_by
(
id:
params
[
:id
])
@micropost
=
current_user
.
microposts
.
find_by
(
id:
params
[
:id
])
redirect_to
root_url
if
@micropost
.
nil?
redirect_to
root_url
if
@micropost
end
end
end
end
app/controllers/sessions_controller.rb
View file @
e1383332
class
SessionsController
<
ApplicationController
class
SessionsController
<
ApplicationController
def
new
def
new
end
end
...
...
app/controllers/static_pages_controller.rb
View file @
e1383332
class
StaticPagesController
<
ApplicationController
class
StaticPagesController
<
ApplicationController
def
home
def
home
@micropost
=
current_user
.
microposts
.
build
if
logged_in?
if
logged_in?
@feed_items
=
current_user
.
feed
.
paginate
(
page:
params
[
:page
])
@micropost
=
current_user
.
microposts
.
build
@feed_items
=
current_user
.
microposts
.
paginate
(
page:
params
[
:page
])
end
end
end
def
help
def
help
...
...
app/controllers/users_controller.rb
View file @
e1383332
...
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
...
@@ -2,6 +2,7 @@ class UsersController < ApplicationController
before_action
:logged_in_user
,
only:
[
:index
,
:edit
,
:update
,
:destroy
]
before_action
:logged_in_user
,
only:
[
:index
,
:edit
,
:update
,
:destroy
]
before_action
:correct_user
,
only:
[
:edit
,
:update
]
before_action
:correct_user
,
only:
[
:edit
,
:update
]
before_action
:admin_user
,
only: :destroy
before_action
:admin_user
,
only: :destroy
before_action
:set_user
,
only:
[
:show
,
:edit
,
:update
,
:destroy
]
def
index
def
index
...
@@ -9,12 +10,13 @@ class UsersController < ApplicationController
...
@@ -9,12 +10,13 @@ class UsersController < ApplicationController
end
end
def
show
def
show
@user
=
User
.
find
(
params
[
:id
])
@microposts
=
@user
.
microposts
.
paginate
(
page:
params
[
:page
])
@microposts
=
@user
.
microposts
.
paginate
(
page:
params
[
:page
])
end
end
def
new
def
new
@user
=
User
.
new
@user
=
User
.
new
end
end
def
create
def
create
@user
=
User
.
new
(
user_params
)
@user
=
User
.
new
(
user_params
)
if
@user
.
save
if
@user
.
save
...
@@ -27,11 +29,9 @@ class UsersController < ApplicationController
...
@@ -27,11 +29,9 @@ class UsersController < ApplicationController
end
end
def
edit
def
edit
@user
=
User
.
find
(
params
[
:id
])
end
end
def
update
def
update
@user
=
User
.
find
(
params
[
:id
])
if
@user
.
update_attributes
(
user_params
)
if
@user
.
update_attributes
(
user_params
)
flash
[
:success
]
=
"Profile updated"
flash
[
:success
]
=
"Profile updated"
redirect_to
@user
redirect_to
@user
...
@@ -41,21 +41,19 @@ class UsersController < ApplicationController
...
@@ -41,21 +41,19 @@ class UsersController < ApplicationController
end
end
def
destroy
def
destroy
User
.
find
(
params
[
:id
])
.
destroy
@user
.
destroy
flash
[
:success
]
=
"User deleted"
flash
[
:success
]
=
"User deleted"
redirect_to
users_url
redirect_to
users_url
end
end
private
def
user_params
def
user_params
params
.
require
(
:user
).
permit
(
:name
,
:email
,
:password
,
:password_confirmation
)
params
.
require
(
:user
).
permit
(
:name
,
:email
,
:password
,
:password_confirmation
)
end
end
def
logged_in_user
def
set_user
unless
logged_in?
@user
=
User
.
find
(
params
[
:id
])
store_location
flash
[
:danger
]
=
"Please log in."
redirect_to
login_url
end
end
end
def
correct_user
def
correct_user
...
...
app/helpers/sessions_helper.rb
View file @
e1383332
app/models/micropost.rb
View file @
e1383332
class
Micropost
<
ActiveRecord
::
Base
class
Micropost
<
ActiveRecord
::
Base
belongs_to
:user
default_scope
->
{
order
(
'created_at DESC'
)
}
default_scope
->
{
order
(
'created_at DESC'
)
}
mount_uploader
:picture
,
PictureUploader
belongs_to
:user
validates
:user_id
,
presence:
true
validates
:user_id
,
presence:
true
validates
:content
,
presence:
true
,
length:
{
maximum:
140
}
validates
:content
,
presence:
true
,
length:
{
maximum:
140
}
validate
:picture_size
validate
:picture_size
mount_uploader
:picture
,
PictureUploader
private
private
def
picture_size
def
picture_size
...
@@ -13,5 +16,4 @@ class Micropost < ActiveRecord::Base
...
@@ -13,5 +16,4 @@ class Micropost < ActiveRecord::Base
errors
.
add
(
:picture
,
"should be less than 5MB"
)
errors
.
add
(
:picture
,
"should be less than 5MB"
)
end
end
end
end
end
end
app/models/user.rb
View file @
e1383332
class
User
<
ActiveRecord
::
Base
class
User
<
ActiveRecord
::
Base
has_many
:microposts
,
dependent: :destroy
attr_accessor
:remember_token
attr_accessor
:remember_token
before_save
{
self
.
email
=
email
.
downcase
}
validates
:name
,
presence:
true
,
length:
{
maximum:
50
}
VALID_EMAIL_REGEX
=
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_EMAIL_REGEX
=
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates
:email
,
presence:
true
,
length:
{
maximum:
255
},
format:
{
with:
VALID_EMAIL_REGEX
},
uniqueness:
{
case_sensitive:
false
}
has_many
:microposts
,
dependent: :destroy
has_secure_password
validates
:password
,
length:
{
minimum:
6
},
allow_blank:
true
validates
:name
,
presence:
true
,
length:
{
maximum:
50
}
validates
:email
,
presence:
true
,
length:
{
maximum:
255
},
format:
{
with:
VALID_EMAIL_REGEX
},
uniqueness:
{
case_sensitive:
false
}
validates
:password
,
length:
{
minimum:
6
},
allow_blank:
true
has_secure_password
before_save
{
self
.
email
=
email
.
downcase
}
def
self
.
digest
(
string
)
def
self
.
digest
(
string
)
cost
=
ActiveModel
::
SecurePassword
.
min_cost
?
BCrypt
::
Engine
::
MIN_COST
:
cost
=
ActiveModel
::
SecurePassword
.
min_cost
?
BCrypt
::
Engine
::
MIN_COST
:
...
@@ -28,16 +28,11 @@ class User < ActiveRecord::Base
...
@@ -28,16 +28,11 @@ class User < ActiveRecord::Base
end
end
def
authenticated?
(
remember_token
)
def
authenticated?
(
remember_token
)
return
false
if
remember_digest
.
nil?
return
false
if
remember_digest
BCrypt
::
Password
.
new
(
remember_digest
).
is_password?
(
remember_token
)
BCrypt
::
Password
.
new
(
remember_digest
).
is_password?
(
remember_token
)
end
end
def
forget
def
forget
update_attribute
(
:remember_digest
,
nil
)
update_attribute
(
:remember_digest
,
nil
)
end
end
def
feed
# This is preliminary. See "Following users" for the full implementation.
Micropost
.
where
(
"user_id = ?"
,
id
)
end
end
end
app/views/layouts/_header.html.erb
View file @
e1383332
...
@@ -24,4 +24,4 @@
...
@@ -24,4 +24,4 @@
</ul>
</ul>
</nav>
</nav>
</div>
</div>
</header>
</header>
app/views/users/_fields.html.erb
View file @
e1383332
...
@@ -9,5 +9,5 @@
...
@@ -9,5 +9,5 @@
<%=
f
.
label
:password
%>
<%=
f
.
label
:password
%>
<%=
f
.
password_field
:password
,
class:
'form-control'
%>
<%=
f
.
password_field
:password
,
class:
'form-control'
%>
<%=
f
.
label
:password_confirmation
,
"Confirmation"
%>
<%=
f
.
label
:password_confirmation
,
'Confirmation'
%>
<%=
f
.
password_field
:password_confirmation
,
class:
'form-control'
%>
<%=
f
.
password_field
:password_confirmation
,
class:
'form-control'
%>
\ No newline at end of file
public/uploads/tmp/1415329593-22750-2891/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415330857-23112-9064/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415330937-23112-9278/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415331043-23112-2022/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415331096-23112-2706/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415331173-23112-4424/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415331207-23319-3582/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415331242-23319-3246/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
public/uploads/tmp/1415331446-23319-2503/_78796825_hltau_nrao.jpg
deleted
100644 → 0
View file @
974672c5
32 KB
test/integration/microposts_interface_test.rb
View file @
e1383332
test/models/user_test.rb
View file @
e1383332
...
@@ -4,6 +4,7 @@ class UserTest < ActiveSupport::TestCase
...
@@ -4,6 +4,7 @@ class UserTest < ActiveSupport::TestCase
def
setup
def
setup
@user
=
User
.
new
(
name:
"Example User"
,
email:
"user@example.com"
,
password:
"foobar"
,
password_confirmation:
"foobar"
)
@user
=
User
.
new
(
name:
"Example User"
,
email:
"user@example.com"
,
password:
"foobar"
,
password_confirmation:
"foobar"
)
end
end
test
"should be valid"
do
test
"should be valid"
do
assert
@user
.
valid?
assert
@user
.
valid?
end
end
...
@@ -31,7 +32,7 @@ class UserTest < ActiveSupport::TestCase
...
@@ -31,7 +32,7 @@ class UserTest < ActiveSupport::TestCase
end
end
test
"email validation should accept valid addresses"
do
test
"email validation should accept valid addresses"
do
valid_addresses
=
%w[user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com]
valid_addresses
=
%w(user@example,com user_at_foo.org user.name@example. foo@bar_baz.com foo@bar+baz.com)
valid_addresses
.
each
do
|
valid_address
|
valid_addresses
.
each
do
|
valid_address
|
@user
.
email
=
valid_address
@user
.
email
=
valid_address
assert_not
@user
.
valid?
,
"
#{
valid_address
.
inspect
}
should be valid"
assert_not
@user
.
valid?
,
"
#{
valid_address
.
inspect
}
should be valid"
...
...
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