Skip to content

Commit

Permalink
[v2] Fat model, skinny controller
Browse files Browse the repository at this point in the history
  • Loading branch information
serradura committed Dec 6, 2019
1 parent 99c0e34 commit 1038299
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 38 deletions.
10 changes: 2 additions & 8 deletions app/controllers/todos_controller.rb
Expand Up @@ -56,10 +56,7 @@ def update
def complete
todo = current_user.todos.find(params[:id])

unless todo.completed?
todo.completed_at = Time.current
todo.save
end
todo.complete!

render_json(200, todo: todo_as_json(todo))
rescue ActiveRecord::RecordNotFound
Expand All @@ -69,10 +66,7 @@ def complete
def activate
todo = current_user.todos.find(params[:id])

unless todo.active?
todo.completed_at = nil
todo.save
end
todo.activate!

render_json(200, todo: todo_as_json(todo))
rescue ActiveRecord::RecordNotFound
Expand Down
31 changes: 4 additions & 27 deletions app/controllers/users/registrations_controller.rb
Expand Up @@ -2,35 +2,12 @@ class Users::RegistrationsController < ApplicationController
def create
user_params = params.require(:user).permit(:name, :password, :password_confirmation)

password = user_params[:password].to_s.strip
password_confirmation = user_params[:password_confirmation].to_s.strip
user = User.new(user_params)

errors = {}
errors[:password] = ["can't be blank"] if password.blank?
errors[:password_confirmation] = ["can't be blank"] if password_confirmation.blank?

if errors.present?
render_json(422, user: errors)
if user.save
render_json(201, user: user.as_json(only: [:id, :name, :token]))
else
if password != password_confirmation
render_json(422, user: { password_confirmation: ["doesn't match password"] })
else
password_digest = Digest::SHA256.hexdigest(password)

user_data = {
name: user_params[:name],
token: SecureRandom.uuid,
password_digest: password_digest,
}

user = User.new(user_data)

if user.save
render_json(201, user: user.as_json(only: [:id, :name, :token]))
else
render_json(422, user: user.errors.as_json)
end
end
render_json(422, user: user.errors.as_json)
end
rescue ActionController::ParameterMissing => e
render_json(400, error: e.message)
Expand Down
20 changes: 20 additions & 0 deletions app/models/todo.rb
Expand Up @@ -31,4 +31,24 @@ def status

'active'
end

def complete
self.completed_at = Time.current unless completed?
end

def complete!
complete

self.save if completed_at_changed?
end

def activate
self.completed_at = nil unless active?
end

def activate!
activate

self.save if completed_at_changed?
end
end
38 changes: 37 additions & 1 deletion app/models/user.rb
@@ -1,9 +1,45 @@
class User < ApplicationRecord
has_many :todos

attr_accessor :password, :password_confirmation

before_validation :normalize_and_fill_attributes

with_options presence: true do
validates :name
validates :token, length: { is: 36 }, uniqueness: true
validates :password_digest, length: { is: 64 }
validates :password_digest, length: { is: 64 }, if: ->(user) { user.password_digest.present? }
end

validate :password_must_be_filled, on: :create
validate :password_must_be_confirmed

private

def normalize_and_fill_attributes
self.name.try(:squish!)
self.password.try(:strip!)
self.password_confirmation.try(:strip!)

self.token = SecureRandom.uuid if token.blank?

if password_filled? && password == password_confirmation
self.password_digest = Digest::SHA256.hexdigest(password)
end
end

def password_must_be_filled
errors.add(:password, "can't be blank") if password.blank?
errors.add(:password_confirmation, "can't be blank") if password_confirmation.blank?
end

def password_must_be_confirmed
if password_filled? && password != password_confirmation
errors.add(:password_confirmation, "doesn't match password")
end
end

def password_filled?
password.present? && password_confirmation.present?
end
end
Expand Up @@ -13,7 +13,7 @@ class Users::RegistrationsControllerCreateTest < ActionDispatch::IntegrationTest
end

test "should respond with 400 when the user password params are missing" do
post users_registrations_url, params: { user: { password: '' } }
post users_registrations_url, params: { user: { name: 'User One' } }

assert_response 422

Expand All @@ -29,7 +29,7 @@ class Users::RegistrationsControllerCreateTest < ActionDispatch::IntegrationTest
end

test "should respond with 400 when the user password params are differents" do
post users_registrations_url, params: { user: { password: '123', password_confirmation: '321' } }
post users_registrations_url, params: { user: { name: 'User Two', password: '123', password_confirmation: '321' } }

assert_response 422

Expand Down

0 comments on commit 1038299

Please sign in to comment.