ruby on rails - With the state_machine gem, is there a way to make the events private/protected? -


i wondering if there's way make state event private when using state_machine gem?

i have 3 states

unpaid, pending, paid.

when receipt in unpaid event can fired charge user. switches receipt pending (while talks merchant service) once done, call pay event , set state paid.

the user of receipt class can technically call pay event, switch receipt paid though didn't run through merchant.

note: contrived example...

i strong believer in private , protected methods, , wondering how 1 engage them in context of state_machine implementation..

i'm assuming you're talking state_machine.

you can make event transition methods private marking them after definition, e.g.

class payment   attr_reader :state    state_machine :state, :initial => :pending     event :pay       transition [:pending] => :paid     end   end    private :pay # should do! end 

even though answers question, highly advise against it. making method private or protected, concerns method visibility, i.e. want expose in api. should looking in case, way control access feature in moment. requirement highly coupled domain logic, , not api.

besides, if mark methods private, not guarantee security because 1 can bypass restriction calling method via send, e.g payment.send(:pay).

i think better solution create sort of policy checker or filter before transactions make sure can processed, e.g.

before_transition :pending => :paid, :do => :check_merchant  def check_merchant   really_paid = ... # logic check merchant     or raise "payment haven't been processed yet. hang on sec" end 

hope helps!


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -