sequenced icon indicating copy to clipboard operation
sequenced copied to clipboard

add support for secondary sequences

Open ivandenysov opened this issue 5 years ago • 5 comments

I needed a way to generate invoice number with user-defined prefix. Thought that 'secondary sequences' can do that and still be useful for other use-cases.

Example

acts_as_sequenced column: :invoice_number_sequential_id,
  secondary: { column: :invoice_number, value: -> (record, next_id) { "#{record.invoice_number_prefix}#{next_id}" } }

@derrickreimer I'm happy to update the README and add tests if you are ok with merging this feature. Feedback is welcome 🙂

ivandenysov avatar Aug 25 '20 10:08 ivandenysov

I like the idea here, but it seems like it almost needs two separate lines.

acts_as_sequenced column: :invoice_number_sequential_id,
acts_as_sequenced column: :invoice_number, sequence: -> (record, next_id) { "#{record.invoice_number_prefix}#{next_id}" } }

I like the idea of being able to customize the sequence with a lambda. Thoughts?

excid3 avatar Aug 15 '22 19:08 excid3

I like the idea here, but it seems like it almost needs two separate lines.

acts_as_sequenced column: :invoice_number_sequential_id,
acts_as_sequenced column: :invoice_number, sequence: -> (record, next_id) { "#{record.invoice_number_prefix}#{next_id}" } }

I like the idea of being able to customize the sequence with a lambda. Thoughts?

Love it. Not sure about the final interface though. I have several suggestions:

1. Your suggestion

acts_as_sequenced column: :invoice_number, sequence: -> (record, next_id) { "#{record.invoice_number_prefix}#{next_id}" } }

2. Unnamed lambda

acts_as_sequenced column: :invoice_number, -> (record, next_id) { "#{record.invoice_number_prefix}#{next_id}" } }

3. Block. This looks like the most idiomatic ruby approach.

acts_as_sequenced column: :invoice_number do |record, next_id|
  "#{record.invoice_number_prefix}#{next_id}"
end

I'll go with option 3. Any objections?

ivandenysov avatar Aug 15 '22 19:08 ivandenysov

I like the block. One of the benefits of doing a option would be that you could pass a lambda or a symbol to have it call a method. A lot of the Rails methods support either an option with a lambda or a block, so we could support both and it would feel natural.

excid3 avatar Aug 15 '22 20:08 excid3

The biggest challenge is probably allowing multiple sequenced ids on the same model.

excid3 avatar Aug 15 '22 20:08 excid3

any idea on when this can be merged with the master?

henrydjacob avatar Jun 21 '23 09:06 henrydjacob