luisquike
9/20/2016 - 3:40 PM

This Snipp is an example of how the models work with the use of good practices

This Snipp is an example of how the models work with the use of good practices

#CONTROLLER
class ItemsController < ApplicationController
	def publish

		if @item.publish
			flash[:notice] = "Your item's published!"
		else
			flas[:notice] = "There was an error"
		end

		redirect_to @item
	end
end


#MODEL
class Item < ActiveRecord::Base
	belongs_to :user

	def publish
		if !is_approved? || user == "Juanito"
			return false
		end

		self.published_on = Time.now
		self.save
	end
end 

Encapsular el metodo simplifica el código y mejora la legibilidad

#CONTROLLER
class ItemsController < ApplicationController
	def publish
		if @item.is_approved? && @item.user != "Juanito"
			@item.published_on = Time.now

			if @item.save
				flash[:notice] = "Your item's published!"
			else
				flash[:notice] = "There was some error"
			end
		else
			flas[:notice] = "There was an error"
		end

		redirect_to @item
	end
end

Models Patterns

Estos son los problemas con los que cuenta el código.

  • Mayor dificultad de entendimiento
  • El trabajo o la logica no esta encapsulada
  • Probabilidad de conflictos
  • Nuevas correcciones dificiles de implementar

Tambien:

  • Multiples llamadas del mismo objeto
  • Se tiene que realizar una modificacion en especifico