geekq / workflow

Ruby finite-state-machine-inspired API for modeling workflow
MIT License
1.75k stars 207 forks source link

Delegate all with_*_state AR scopes to common method #175

Closed lowjoel closed 2 years ago

lowjoel commented 8 years ago

This helps with writing extensions to the AR adapters. For instance, I'd like to write an adapter which uses AR::Enum to store the workflow state instead of a string. Model.with_some_state will always evaluate to where(workflow_state: :some) (see lib/workflow/adapters/active_record.rb).

My suggestion is to change

            eigenclass.send(:define_method, "with_#{state}_state") do
              where("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
            end

to

            eigenclass.send(:define_method, "with_#{state}_state") do
              with_workflow_state(state)
            end

And then defining

def with_workflow_state(state)
  where("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
end

This way my custom adapter can write:

def with_workflow_state(state)
  super(statuses[state])
end

Just a suggestion.