Best practice for a fat model regenerator

I'm trying to make my fat model Userless awkward. I use value objects to represent custom values โ€‹โ€‹and operations on them, and I'm stuck with ActiveSupport::Concernand modules. I read this as an inspiration.

I put helper methods as follows:

def is_a_wizard?
  power_level >= WIZARD_POWER
end

def just_became_a_wizard?
  power_level == WIZARD_POWER
end

into modules and included them as a kind of extension. However, it is difficult to read and maintain, and I need some of them both in the views and in the controllers (for example, to authenticate the wizard). Where should I put them? Create service objects when they are used?

+4
source share
4 answers

, :

# lib/wizard_detector.rb
class WizardDetector
  def initialize(power_level)
    @power_level = power_level
  end

  def is_a_wizard?
    @power_level >= WIZARD_POWER
  end

  def just_became_a_wizard?
    @power_level == WIZARD_POWER
  end 
end

# app/models/user.rb
class User

  delegate :is_a_wizard?, :just_became_a_wizard?, to: :wizard_detector

  def wizard_detector
    @wizard_detector ||= WizardDetector.new(power_level)
  end
end

# anywhere else
WizardDetector.new(power_level_to_check).is_a_wizard?

, wizard_detector , , , . .

+1

, - .

, , -

class User < AR::Base

  def predicates
    @predicates ||= ::User::Predicates.new(self)
  end

end

/models/user/predicates.rb

class User
  class Predicates < SimpleDelegator
    def just_became_a_wizard?
      power_level == User::WIZARD_POWER
    end
  end
end

:

 user.predicates.just_became_a_wizard?

, .

- -.

0

:

#is_a_wizard? #wizard? ( )

just_became_a_wizard? = > new_wizard? (inexperienced_wizard?... )

"" : .

-

0

Concerns , . "Wizard" Concern . , , . , ?

, , , , . , . , , .

In addition, you must make sure that your problems are actually problems, and not just a general dump for the methods that you are tired of looking in your model.

0
source

All Articles