-
Notifications
You must be signed in to change notification settings - Fork 0
Chapter 13 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
levanluu
wants to merge
1
commit into
master
Choose a base branch
from
chapter_13
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Chapter 13 #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| class MicropostsController < ApplicationController | ||
| before_action :logged_in_user, only: [:create, :destroy] | ||
| before_action :correct_user, only: :destroy | ||
|
|
||
| def create | ||
| @micropost = current_user.microposts.build micropost_params | ||
| @micropost.image.attach(params[:micropost][:image]) | ||
| if @micropost.save | ||
| flash[:success] = I18n.t("micropost.micropost_created") | ||
| redirect_to root_url | ||
| else | ||
| @feed_items = current_user.feed.paginate(page: params[:page]) | ||
| render "static_pages/home" | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| if @micropost.destroy | ||
| flash[:success] = t("micropost.micropost_deleted") | ||
| else | ||
| flash[:warning] = t("micropost.error") | ||
| end | ||
| redirect_to request.referer || root_url | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def micropost_params | ||
| params.require(:micropost).permit :content, :picture | ||
| end | ||
|
|
||
| def correct_user | ||
| @micropost = current_user.microposts.find_by id: params[:id] | ||
| return if @micropost.present? | ||
|
|
||
| flash[:warning] = t("micropost.delete_error") | ||
| redirect_to root_url | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| class StaticPagesController < ApplicationController | ||
| def home; end | ||
| def home | ||
| if logged_in? | ||
| @micropost = current_user.microposts.build | ||
| @feed_items = current_user.feed.paginate(page: params[:page]) | ||
| end | ||
| end | ||
|
|
||
| def help; end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| $("#micropost_image").bind("change", function() { | ||
| var size_in_megabytes = this.files[0].size/1024/1024; | ||
| if (size_in_megabytes > 5) { | ||
| alert("Maximum file size is 5MB. Please choose a smaller file."); | ||
| } | ||
| }); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Micropost < ApplicationRecord | ||
| belongs_to :user | ||
| # default_scope -> { order(created_at: :desc) } | ||
| scope :by_created_at, ->{order(created_at: :desc)} | ||
| has_one_attached :image | ||
| validates :user_id, presence: true | ||
| validates :content, presence: true, length: {maximum: 140} | ||
| validates :image, content_type: {in: %w[image/jpeg image/gif image/png], message: "must be a valid image format"}, size: {less_than: 5.megabytes, message: "should be less than 5MB"} | ||
|
|
||
| def display_image | ||
| image.variant(resize_to_limit: [500, 500]) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <li id="micropost-<%= micropost.id %>"> | ||
| <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %> | ||
| <span class="user"><%= link_to micropost.user.name, micropost.user %></span> | ||
| <span class="content"> | ||
| <%= micropost.content %> | ||
| <%= image_tag micropost.display_image if micropost.image.attached? %> | ||
| </span> | ||
| <span class="timestamp"> | ||
| Posted <%= time_ago_in_words(micropost.created_at) %> ago. | ||
| <% if current_user? micropost.user %> | ||
| <%= link_to t("micropost.button"), micropost, method: :delete, data: {confirm: t("micropost.confirm")} %> | ||
| <% end %> | ||
| </span> | ||
| </li> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <% if @feed_items.any? %> | ||
| <ol class="microposts"> | ||
| <%= render @feed_items %> | ||
| </ol> | ||
| <%= will_paginate @feed_items, params: { controller: :static_pages, action: :home } %> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <%= form_for(@micropost, html: { multipart: true }) do |f| %> | ||
| <%= render "shared/error_messages", object: f.object %> | ||
| <div class="field"> | ||
| <%= f.text_area :content, placeholder: "Compose new micropost..." %> | ||
| </div> | ||
| <%= f.submit "Post", class: "btn btn-primary" %> | ||
| <span class="image"> | ||
| <%= f.file_field :image %> | ||
| </span> | ||
| <% end %> | ||
|
|
||
| <%= javascript_pack_tag "micropost" %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <%= link_to gravatar_for(current_user, size: 50), current_user %> | ||
| <h1><%= current_user.name %></h1> | ||
| <span><%= link_to t("micropost.title"), current_user %></span> | ||
| <span><%= pluralize(current_user.microposts.count, "micropost") %></span> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indent