-
Notifications
You must be signed in to change notification settings - Fork 0
Tasks 1-5 #1
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
olenaniemova
wants to merge
2
commits into
main
Choose a base branch
from
o_niemova_tasks
base: main
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
Tasks 1-5 #1
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,25 @@ | ||
| class FavoriteProductsController < ApplicationController | ||
| before_action :set_product | ||
|
|
||
| def create | ||
| if FavoriteProduct.create(product: @product, user: current_user) | ||
| flash[:success] = 'Product has been favorited' | ||
| else | ||
| flash[:error] = 'Something went wrong' | ||
| end | ||
|
|
||
| redirect_back(fallback_location: homepage_path) | ||
| end | ||
|
|
||
| def destroy | ||
| FavoriteProduct.where(product_id: @product.id, user_id: current_user.id).first.destroy | ||
| flash[:success] = 'Product has been deleted from favorites' | ||
| redirect_back(fallback_location: homepage_path) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_product | ||
| @product = Product.active.find(params[:product_id] || params[:id]) | ||
| 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,7 @@ | ||
| class FavoriteProduct < ApplicationRecord | ||
| validates_presence_of :product_id, :user_id | ||
| validates_uniqueness_of :product_id, scope: :user_id | ||
|
|
||
| belongs_to :user | ||
| belongs_to :product | ||
| 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 @@ | ||
| class UserLogin < ApplicationRecord | ||
| belongs_to :user | ||
|
|
||
| validates_presence_of :user_id, :login_at | ||
|
|
||
| scope :ordered, -> { order(login_at: :desc) } | ||
| 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,13 @@ | ||
| <div class="w-5"> | ||
| <% if signed_in? %> | ||
| <% if current_user.favorite_products.exists?(product_id: product.id) %> | ||
| <%= button_to favorite_product_path(product), method: :delete do %> | ||
| <%= image_tag "icons/heart_full.svg", class: "w-full", alt: "heart icon" %> | ||
| <% end %> | ||
| <% else %> | ||
| <%= button_to favorite_products_path(product_id: product) do %> | ||
| <%= image_tag "icons/heart.svg", class: "w-full", alt: "heart icon" %> | ||
| <% end %> | ||
| <% end %> | ||
| <% end %> | ||
| </div> |
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,15 @@ | ||
| <div | ||
| class="flex flex-row items-center my-4 rounded-lg bg-white shadow-[0_2px_15px_-3px_rgba(0,0,0,0.07),0_10px_20px_-2px_rgba(0,0,0,0.04)]" | ||
| > | ||
| <img class="w-28 rounded-l-lg" src="<%= image_url product.image_url %>" alt="Photo of <%= product.name %>" /> | ||
| <div class="flex flex-row justify-between w-full"> | ||
| <p class="text-md font-medium leading-tight text-neutral-800 ml-4"> | ||
| <a href="<%= product_path(category_slug: product.categories.first.slug, product_slug: product.slug) %>"> | ||
| <%= product.name %> | ||
| </a> | ||
| </p> | ||
| <div class="mr-4"> | ||
| <%= render "shared/favorite_button", product: product %> | ||
| </div> | ||
| </div> | ||
| </div> |
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,31 @@ | ||
| class AddUserLogins < ActiveRecord::Migration[7.0] | ||
| def up | ||
| create_table :user_logins do |t| | ||
| t.references :user | ||
| t.datetime :login_at, null: false | ||
| end | ||
|
|
||
| execute <<-SQL | ||
| INSERT INTO user_logins (user_id, login_at) | ||
| SELECT id, last_login_at FROM users; | ||
| SQL | ||
|
|
||
| remove_column :users, :last_login_at, :datetime | ||
| end | ||
|
|
||
| def down | ||
| add_column :users, :last_login_at, :datetime | ||
|
|
||
| execute <<-SQL | ||
| UPDATE users | ||
| SET last_login_at = ( | ||
| SELECT login_at FROM user_logins | ||
| WHERE user_logins.user_id = users.id | ||
| ORDER BY login_at DESC | ||
| LIMIT 1 | ||
| ); | ||
| SQL | ||
|
|
||
| drop_table :user_logins | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class AddFavoriteProducts < ActiveRecord::Migration[7.0] | ||
| def change | ||
| create_table :favorite_products do |t| | ||
| t.references :product, index: true | ||
| t.references :user, index: true | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I know, it is not the best solution to put raw SQL in the migration to migrate data from one table to another. But it works in this small project.
For real projects which have a big codebase and database, in such cases, I would use a data-migrate gem or rake task to create separate migration for data. The development should look like this: