DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on • Edited on

Refactor do end block to {} for reduce scrolling in Rails

🤔 Situation

When you use aasm gem in your project, and it's grown. You might want to refactor it. There is a tip, I will write it later.

# 32 lines
include AASM
aasm column: :status do
  state :a , initial: true
  state :b
  state :c
  state :d
  state :e
  state :f
  state :g
  state :h

  event :b do
    transitions from: :a,  to: :b
  end
  event :c do
    transitions from: :b,  to: :c
  end
  event :d do
    transitions from: :b,  to: :d
  end
  event :e do
    transitions from: :d,  to: :e
  end
  event :f do
    transitions from: :e,  to: :f
  end
  event :g do
    transitions from: :e,  to: :g
  end
end
Enter fullscreen mode Exit fullscreen mode

🦄 Before the refactoring, Understanding

The curry braces is stronger(prior) than the do-end block.
Then, if you want the arguments stronger than {}, you need () for the arguments.

So, following it equal.

event :happen do
  transitions from: :sleep, to: :wakeup
end

event(:happen){transitions from: :sleep, to: :wakeup }
Enter fullscreen mode Exit fullscreen mode

👍 Reduce the lines

It is better foresight.

# 18 lies
include AASM
aasm column: :status do
  state :a , initial: true
  state :b
  state :c
  state :d
  state :e
  state :f
  state :g
  state :h

  event(:b)   {transitions from: :a,  to: :b  }
  event(:c)   {transitions from: :b,  to: :c  }
  event(:d)   {transitions from: :b,  to: :d  }
  event(:e)   {transitions from: :d,  to: :e  }
  event(:f)   {transitions from: :e,  to: :f  }
  event(:g)   {transitions from: :e,  to: :g  }
end
Enter fullscreen mode Exit fullscreen mode

🔗 Parent Note

Top comments (0)