Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a value to always be at the end of sorted list #60

Closed
moretoend opened this issue Jun 4, 2016 · 2 comments
Closed

Set a value to always be at the end of sorted list #60

moretoend opened this issue Jun 4, 2016 · 2 comments

Comments

@moretoend
Copy link

Sometimes, we need to keep a value at the end of a sorted list regardless its value.
For example, I have a list of movie types like ['comedy', 'romance', 'thiller', 'other'] and I need to keep the value 'other' always at the end even if I sort.

@lucascaton
Copy link
Owner

lucascaton commented Jun 4, 2016

Hi @dfmoreto,

You can solve this using the following code:

class MovieType < EnumerateIt::Base
  associate_values(
    :comedy,
    :romance,
    :thiller,
    :other,
    :zzz
  )

  class << self
    alias original_list list

    def list
      original_list - [MovieType::OTHER] + [MovieType::OTHER]
    end
  end
end
MovieType.original_list
# [
#   "comedy",
#   "romance",
#   "thiller",
#   "other",
#   "zzz"
# ]
MovieType.list
# [
#   "comedy",
#   "romance",
#   "thiller",
#   "zzz",
#   "other"
# ]

@moretoend
Copy link
Author

Thanks a lot, @lucascaton !! 😄
So, before your answer, I forked and worked on a solution in the gem itself. It's working for me, but I'm having some problems when I try to execute RSpec. If you can help me with this, I can commit it.

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

No branches or pull requests

2 participants