-
Notifications
You must be signed in to change notification settings - Fork 6
Fs_Challenge/ views and migrations #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
kinhosz
wants to merge
11
commits into
hent-dev:master
Choose a base branch
from
kinhosz:master
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b1a1cb9
add migrations
kinhosz b0b0349
create User controller
kinhosz a79abf2
add pages for user
kinhosz 4559c4a
add new user route and upd db:schema
kinhosz 50c3cee
add some validations
kinhosz 3e59b43
fix routes
kinhosz ff13d37
fix webpack and node
kinhosz 15b63cc
fix validations
kinhosz 9acd35b
refactoring
kinhosz a57334d
fix invalid email
kinhosz ba08fe4
fix rubocop issues
kinhosz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ | |
/yarn-error.log | ||
yarn-debug.log* | ||
.yarn-integrity | ||
.rubocop.yml |
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,62 @@ | ||
class UsersController < ApplicationController | ||
before_action :set_user, only: %i[ edit update show ] | ||
|
||
def index | ||
@users = User.all | ||
end | ||
|
||
def show | ||
@user = User.find(params[:id]) | ||
kinhosz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def new | ||
@user = User.new | ||
end | ||
|
||
def create | ||
@user = User.new(user_params) | ||
|
||
respond_to do |format| | ||
if @user.update(user_params) | ||
format.html { redirect_to @user, notice: 'User was successfuly created!' } | ||
format.json { render :show, status: :ok, location: @user } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @user.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
|
||
respond_to do |format| | ||
if @user.update(user_params) | ||
format.html { redirect_to @user, notice: 'User was successfuly updated!!' } | ||
format.json { render :show, status: :ok, location: @user } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @user.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
@user.destroy | ||
respond_to do |format| | ||
format.html { redirect_to users_url, notice: 'user was successfully destroyed.' } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
def set_user | ||
@user = User.find(params[:id]) | ||
end | ||
|
||
def user_params | ||
params.fetch(:user, {}).permit(:user_id, :name, :password, :email, :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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module UsersHelper | ||
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
class Book < ApplicationRecord | ||
has_many :lends | ||
|
||
validates :name, uniqueness: true | ||
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
class Lend < ApplicationRecord | ||
belongs_to :book | ||
end | ||
belongs_to :user | ||
|
||
validates_with LoanDevolutionValidator | ||
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,20 @@ | ||
class LoanDevolutionValidator < ActiveModel::Validator | ||
def validate(record) | ||
unless record.devolution.nil? | ||
months = ((record.devolution.year * 12 + record.devolution.month) - | ||
(record.created_at.year * 12 + record.created_at.month)) | ||
|
||
if record.devolution < record.created_at | ||
record.erros[:base] << 'The return date cannot be less than loan date' | ||
elsif months >= 6 | ||
record.erros[:base] << 'The datetime cannot be greater of six months' | ||
end | ||
end | ||
kinhosz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
lends = Lend.where({ user_id: record.user_id, devolution: nil }) | ||
|
||
if lends.size >= 2 # check if exists two or more lends not closed | ||
record.errors[:base] << 'The user has more than 2 loans not closed' | ||
end | ||
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
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.
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.
Como vocês faria isso utilizando uma abordagem de serviço?
Outra coisa, não vamos poder mergiar isso nesse repo, por que é dos proximos candidatos 🙄