end def create_password return if plain_password.present? # I'm creating.. why would this already exist? w1 = Word.randomised.simple_word.first.word # adding dependent object to creation. self.plain_password = "#{ w1 }#{ "%02d" % rand(100) }" # !" set_password # I call another method..? end def set_password return if !changed.include?("plain_password") # active record dirty? maybe? self.password = plain_password self.password_confirmation = plain_password end # ... end
complexity to the `Student` model. - Makes the process of creating a `Student` depend on other objects (hard to know from the outside) - Can make things very hard to test. - Makes this type of functionality hard to reuse.
do set_info_student set_password end after_create do initialize_student set_access_all_areas if demo_only? end before_save do set_school_from_teacher set_inactive_if_no_teacher set_active_if_teacher end after_save do add_trial_if_home_school_linked update_country_code! flag_for_update_parent clear_cart_items if parent_id_changed? end after_destroy do flag_for_update_parent clear_cart_items end after_commit do update_parent end
within a system - A way to reuse similar steps in a complex system - Very declarative way to express business rules - Safe! Helps ensure everything is atomic*
do if number >= 1 Hopscotch::Step.success! else Hopscotch::Step.failure! end end # abc.call(100) => Hopscotch::Step.success! # abc.(0) => Hopscotch::Step.failure!
eggs = EGGS) if EggLedger.new(student).add(eggs) Hopscotch::Step.success!("Eggs were awarded successfully.") else Hopscotch::Step.failure!("Could not award eggs.") end end end
failure! - Hopscotch::StepComposer - composes steps together to create 1 function - Hopscotch::Runner - calls 1 function and calls failure/success depending on functions results