Skip to content

Conversation

ivandenysov
Copy link

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 🙂

@excid3
Copy link
Collaborator

excid3 commented Aug 15, 2022

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?

@ivandenysov
Copy link
Author

ivandenysov commented Aug 15, 2022

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?

@excid3
Copy link
Collaborator

excid3 commented Aug 15, 2022

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
Copy link
Collaborator

excid3 commented Aug 15, 2022

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

@henrydjacob
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants